首先 打开邮件
2 找到 》设置 》账户 选项卡
这里会根据你的账号安全添加密保 ,添加密保 后 会给你 一个 密钥
(2)验证密保
(3)获取授权码
这个授权码 一会会用到
提前须知 :ssl, tls, 是不同的协议
首先 我们用starttls 协议 写 一个 简单的 例子 1
============= = 1 start =====================
package main import ( "net/smtp" "strings" "fmt" ) func SendToMail(user, pwd, host, to, subject, body, mailtype string) error{ hp := strings.Split(host, ":") auth := smtp.PlainAuth("", user, pwd, 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 + "\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 := "2677031999@qq.com" // 你开通smtp邮箱的 邮箱的地址 pwd := "gv**************jfh" //这里填你自己的授权码 host :="smtp.qq.com:25" to := "2677031999@qq.com" // 目标地址 subject := "使用Golang发送邮件" body := ` <html> <body> <h3> "Test send to email" </h3> </body> </html> ` fmt.Println("SEND EMAIL ...") err :=SendToMail(user,pwd,host,to,subject,body,"html") if err != nil{ fmt.Println("发送邮件错误!") fmt.Println(err); }else{ fmt.Println("发送成功") } }
======================= 1 end ====================
好了 由于 很多时侯 需要用到 ssl 协议
==== 2 start ==========
package main import ( "fmt" "net/smtp" "crypto/tls" "net" "log" ) func main(){ host :="smtp.qq.com" port :=465 email := "2677031999@qq.com" pwd := "gvd************fh" // 这里填你的授权码 toEmail := "2677031999@qq.com" // 目标地址 header := make(map[string]string) header["From"] = "test"+"<" +email+">" header["To"] = toEmail header["Subject"] = "邮件标题11111" header["Content-Type"] = "text/html;chartset=UTF-8" body := "这是一封golang 发来的邮件" message := "" for k,v :=range header{ message += fmt.Sprintf("%s:%s\r\n",k,v) } message +="\r\n"+body auth :=smtp.PlainAuth( "", email, pwd, host, ) err := SendMailUsingTLS( fmt.Sprintf("%s:%d", host, port), auth, email, []string{toEmail}, []byte(message), ) if err !=nil{ panic(err) } } //return a smtp client func Dial(addr string) (*smtp.Client, error) { conn, err := tls.Dial("tcp", addr, nil) if err != nil { log.Panicln("Dialing Error:", err) return nil, err } //分解主机端口字符串 host, _, _ := net.SplitHostPort(addr) return smtp.NewClient(conn, host) } //参考net/smtp的func SendMail() //使用net.Dial连接tls(ssl)端口时,smtp.NewClient()会卡住且不提示err //len(to)>1时,to[1]开始提示是密送 func SendMailUsingTLS(addr string, auth smtp.Auth, from string, to []string, msg []byte) (err error) { //create smtp client c, err := Dial(addr) if err != nil { log.Println("Create smpt client error:", err) return err } defer c.Close() if auth != nil { if ok, _ := c.Extension("AUTH"); ok { if err = c.Auth(auth); err != nil { log.Println("Error during AUTH", err) return err } } } if err = c.Mail(from); err != nil { return err } for _, addr := range to { if err = c.Rcpt(addr); err != nil { return err } } w, err := c.Data() if err != nil { return err } _, err = w.Write(msg) if err != nil { return err } err = w.Close() if err != nil { return err } return c.Quit() }
======================= 2 end ==============================
结果
其实你观察 两种方法 很类似 golang官方库net/smtp支持的是starttls, , 要想支持 ssl将上面例子 只是将net 的 Dial 换成 tls.
有疑问加站长微信联系(非本文作者)