golang发邮件
zb_liang
· · 3372 次点击 ·
·
开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
19 |
func SendMail(user, password, host, to, subject, body, mailtype string) error{ |
20 |
hp := strings.Split(host,
":" ) |
21 |
auth := smtp.PlainAuth( "" , user, password, hp[0]) |
22 |
var content_type string |
23 |
if
mailtype == "html"
{ |
24 |
content_type =
"Content-Type: text/" + mailtype +
"; charset=UTF-8" |
26 |
content_type =
"Content-Type: text/plain"
+ "; charset=UTF-8" |
29 |
msg := []byte( "To: "
+ to + "\r\nFrom: "
+ user + "<" + user + ">\r\nSubject: "
+ subject + "\r\n"
+ content_type + "\r\n\r\n"
+ body) |
30 |
send_to := strings.Split(to,
";" ) |
31 |
err := smtp.SendMail(host, auth, user, send_to, msg) |
36 |
user :=
"xxxx@163.com" |
38 |
host :=
"smtp.163.com:25" |
39 |
to :=
"xxxx@gmail.com;ssssss@gmail.com" |
41 |
subject :=
"Test send email by golang" |
47 |
"Test send email by golang" |
52 |
fmt.Println( "send email" ) |
53 |
err := SendMail(user, password, host, to, subject, body,
"html" ) |
55 |
fmt.Println( "send mail error!" ) |
58 |
fmt.Println( "send mail success!" ) |
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889