Go语言发邮件
2014-03-01 21:14
344 查看
package main
import (
"fmt"
"net/smtp"
"strings"
)
/*
* user : example@example.com login smtp server user
* password: xxxxx login smtp server password
* host: smtp.example.com:port smtp.163.com:25
* to: example@example.com;example1@163.com;example2@sina.com.cn;...
* subject:The subject of mail
* body: The content of mail
* mailtyoe: mail type html or text
*/
func SendMail(user, password, host, to, subject, body, mailtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
func main() {
user := "XXXX@163.com"
password := "aaaaaaa"
host := "smtp.163.com:25"
to := "xymprivate@163.com"
subject := "Test send email by golang"
body := `
<html>
<body>
<h3>
"这是GO语言写的测试邮件。"
</h3>
</body>
</html>
`
fmt.Println("send email")
err := SendMail(user, password, host, to, subject, body, "html")
if err != nil {
fmt.Println("send mail error!")
fmt.Println(err)
} else {
fmt.Println("send mail success!")
}
}
//该代码片段来自于: http://www.sharejs.com/codes/go/4360
import (
"fmt"
"net/smtp"
"strings"
)
/*
* user : example@example.com login smtp server user
* password: xxxxx login smtp server password
* host: smtp.example.com:port smtp.163.com:25
* to: example@example.com;example1@163.com;example2@sina.com.cn;...
* subject:The subject of mail
* body: The content of mail
* mailtyoe: mail type html or text
*/
func SendMail(user, password, host, to, subject, body, mailtype string) error {
hp := strings.Split(host, ":")
auth := smtp.PlainAuth("", user, password, hp[0])
var content_type string
if mailtype == "html" {
content_type = "Content-Type: text/" + mailtype + "; charset=UTF-8"
} else {
content_type = "Content-Type: text/plain" + "; charset=UTF-8"
}
msg := []byte("To: " + to + "\r\nFrom: " + user + "<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
func main() {
user := "XXXX@163.com"
password := "aaaaaaa"
host := "smtp.163.com:25"
to := "xymprivate@163.com"
subject := "Test send email by golang"
body := `
<html>
<body>
<h3>
"这是GO语言写的测试邮件。"
</h3>
</body>
</html>
`
fmt.Println("send email")
err := SendMail(user, password, host, to, subject, body, "html")
if err != nil {
fmt.Println("send mail error!")
fmt.Println(err)
} else {
fmt.Println("send mail success!")
}
}
//该代码片段来自于: http://www.sharejs.com/codes/go/4360
相关文章推荐
- 案例分析---我是如何失去价值 5 万美元的Twitter用户名
- UVA 12260 - Free Goodies(dp+贪心)
- Google地图开发应用(一)
- SogoTrade 美股开户指南
- A good example of equals()
- golang struct tag
- Google Charts插件(二)
- Google Charts插件(一)
- USACO 3.4 American Heritage (heritage)
- USACO 3.3 A Game (game1)
- USACO 3.3 Home on the Range (range)
- 使用goto语句构成的循环求1~100之间的所有奇数之和,即求解表达式1+3+5+...+99之和。
- 题目1440:Goldbach's Conjecture
- google搜索技巧
- ThoughtWorks持续集成平台GO开源了
- NS2脚本中如何使用God
- Goldbach
- ubuntu 13 sogou input method install
- poj 1940 Wine Trading in Gergovia_贪心
- poj 1940 Wine Trading in Gergovia_贪心