内容简介:SSL协议发送邮件的方法
SSL协议发送邮件的方法
package utils import ( "crypto/tls" "fmt" "log" "net/smtp" "strings" ) const Email_User = "*******@163.com" const Email_Password = "***********" const Email_Host = "smtp.163.com" const Email_Port = "465" const Email_Name = "测试" const Email_Subject = "测试验证码" type Mail struct { senderId string toIds []string subject string body string } type SmtpServer struct { host string port string } func EmailSendCode(nickname, to, code string) error { if nickname != "" { nickname = nickname + "," } body := ` <html> <body> <h3> `+nickname+`您好: </h3> 非常感谢您使用`+Email_Name+`,您的邮箱验证码为:<br/> <b>`+code+`</b><br/> 此验证码有效期30分钟,请妥善保存。<br/> 如果这不是您本人的操作,请忽略本邮件。<br/> </body> </html> ` return SendToMail(to, Email_Subject, body) } func (s *SmtpServer) ServerName() string { return s.host + ":" + s.port } func (mail *Mail) BuildMessage() string { message := "" message += fmt.Sprintf("From: %s<%s>\r\n", Email_Name, mail.senderId) if len(mail.toIds) > 0 { message += fmt.Sprintf("To: %s\r\n", strings.Join(mail.toIds, ";")) } message += fmt.Sprintf("Subject: %s\r\n", mail.subject) message += "Content-Type: text/html; charset=UTF-8" message += "\r\n\r\n" + mail.body return message } func SendToMail(to, subject, body string) error { mail := Mail{} mail.senderId = Email_User mail.toIds = strings.Split(to, ";") mail.subject = Email_Subject mail.body = body messageBody := mail.BuildMessage() smtpServer := SmtpServer{host: Email_Host, port: Email_Port} //build an auth auth := smtp.PlainAuth("", mail.senderId, Email_Password, smtpServer.host) // Gmail will reject connection if it's not secure // TLS config tlsconfig := &tls.Config{ InsecureSkipVerify: false, ServerName: smtpServer.host, } conn, err := tls.Dial("tcp", smtpServer.ServerName(), tlsconfig) if err != nil { return err } client, err := smtp.NewClient(conn, smtpServer.host) if err != nil { return err } // step 1: Use Auth if err = client.Auth(auth); err != nil { return err } // step 2: add all from and to if err = client.Mail(mail.senderId); err != nil { return err } for _, k := range mail.toIds { if err = client.Rcpt(k); err != nil { return err } } // Data w, err := client.Data() if err != nil { return err } _, err = w.Write([]byte(messageBody)) if err != nil { return err } err = w.Close() if err != nil { return err } client.Quit() log.Println("Mail sent successfully") return nil }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- SpringBoot系列(十四)集成邮件发送服务及邮件发送的几种方式
- Android 快速发送邮件
- Python 发送邮件
- Python邮件发送指南
- 使用go发送邮件
- Laravel - 上手实现 - 邮件发送
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Thirty-three Miniatures
Jiří Matoušek / American Mathematical Socity / 2010-6-18 / USD 24.60
This volume contains a collection of clever mathematical applications of linear algebra, mainly in combinatorics, geometry, and algorithms. Each chapter covers a single main result with motivation and......一起来看看 《Thirty-three Miniatures》 这本书的介绍吧!