This is a creation in Article, where the information may have evolved or changed.
Package Main
Import (
"Net/smtp"
"FMT"
"Strings"
)
/*
* user:example@example.com Login SMTP Server user
* PASSWORD:XXXXX Login SMTP server password
* Host:smtp.example.com:port smtp.163.com:25
* to:example@example.com;example1@163.com;example2@sina.com.cn, .....
* Subject:the subject of mail
* Body:the content of mail
* Mailtyoe:mail type HTML or text
*/
Func SendMail (user, password, host, to, subject, body, mailtype string) error{
HP: = Strings. Split (Host, ":")
Auth: = SMTP. Plainauth ("", User, password, 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 + "<" + 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: = "xxx@163.com"
Password: = "xxx"
Host: = "smtp.163.com:25"
To: = "xxx@qq.com"
Subject: = "Test send email by Golang"
Body: = '
<body>
"Test Send email by Golang"
</body>
`
Fmt. PRINTLN ("Send Email")
ERR: = SendMail (user, password, host, to, subject, body, "html")
If err! = Nil {
Fmt. PRINTLN ("Send mail error!")
Fmt. PRINTLN (ERR)
}else{
Fmt. PRINTLN ("Send mail success!")
}
}