This is a creation in Article, where the information may have evolved or changed.
Package Main
Import (
"FMT"
"Net/smtp"
"Strings"
)
Func sendtomail (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 + "\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: = "***@126.com"
Password: = "* * *"
Host: = "smtp.126.com:25"
To: = "64***266@qq.com"
Subject: = "Send mail using Golang"
Body: = '
<body>
"Test Send to emailtest send to email
</body>
`
Fmt. PRINTLN ("Send Email")
ERR: = sendtomail (user, password, host, to, subject, body, "html")
If err! = Nil {
Fmt. Println ("Send Mail error!")
Fmt. PRINTLN (ERR)
} else {
Fmt. Println ("Send Mail success!")
}
}