这里说的发邮件,用的是“net/smtp”,不多说,看代码
package main import ( "net/smtp" "fmt" "strings" ) //对发送接口的简单封装 func SendMail(to, subject, body, mailType string) error{ //发件服务器相关配置,这里以gmail为例 u := "xxxxx@gmail.com" p := "xxxx" host := "smtp.gmail.com" port := "25" //Auth auth := smtp.PlainAuth("", u, p, host) var content_type string if mailType == "html" { content_type = "Content-Type: text/html; charset=UTF-8" }else{ content_type = "Content-Type: text/plain; charset=UTF-8" } msg := []byte("To:" + to + "\r\nForm: 老大<" + u + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body) send_to := strings.Split(to, ";") //发信 err := smtp.SendMail((host + ":" + port), auth, u, send_to, msg) return err } func main() { to := "xxxx@163.com;xxxx@gmail.com" subject := "test-go-smtpxxx" body := "<html><body><hr><h1>test-smtpxxx</h1><hr></body></html>" fmt.Println("send email") err := SendMail(to, subject, body, "html") if err != nil { fmt.Println("mail error: %v", err) }else{ fmt.Println("mail ok!") } } |