您的位置:首页 > 编程语言 > Go语言

用go的net/smtp来发邮件

2018-11-16 19:54 183 查看
版权声明:本文为博主原创文章,转载时请务必注明本文地址, 禁止用于任何商业用途, 否则会用法律维权。 https://blog.csdn.net/stpeace/article/details/84144953

        玩了一下, 发送邮件OK, 代码:

[code]package main

import (
"fmt"
"net/smtp"
"strings"
)

func main() {
senderEmail := "xxx@gmail.com"
port := ":25"
password := "yyyyyy"
host := "smtp.gmail.com"
auth := smtp.PlainAuth("", senderEmail, password, host)
to := []string{"zzz@gmail.com"}
nickname := "sender"
user := senderEmail

subject := "this is title"
content_type := "Content-Type: text/plain; charset=UTF-8"
body := "this is content"
msg := []byte("To: " + strings.Join(to, ",") + "\r\nFrom: " + nickname +
"<" + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
err := smtp.SendMail(host + port, auth, user, to, msg)
if err != nil {
fmt.Printf("send mail error: %v", err)
}
}

        不多说。

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: