Golang:send Mail using SMTP package

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go language to send mail, you can use the SMTP package, two key functions:

Func Plainauth (identity, username, password, host string) Auth

Func SendMail (addr string, a Auth, from string, to []string, msg []byte) error

Just started to play, very curious about the mail title is how to set in, after all, from the code did not see Settitle. Later found to parse msg get title, sender, recipient, message format, message content , etc. msg needs to be written in a certain format: to: xxx\r\nfrom: xxxx\r\nSubject: xxxx\r\ncontent-type:text/html; charset=utf-8\r\n\r\n[Email Content]

Func ( This*TestController) SendMail () {subject:="This is golang test email ..."User:="xxxxx@163.com"To := []string{"xxxxx@163.com"} Nickname:="Dyao"content:="mail Body ... .."ContentType:="content-type:text/html; Charset=utf-8"msg:= []byte("To :"+ Strings. Join (To,",") +"\r\nfrom:"+ Nickname +"<"+ user +">\r\nsubject:"+ Subject +"\ r \ n"+ ContentType +"\r\n\r\n"+content) Auth:=SMTP. Plainauth ("", user,"Testpassword",        "smtp.163.com",) Err:=SMTP. SendMail ("smtp.163.com:25", auth, User, to, []byte(msg),)ifErr! =nil{Log. Fatal (ERR)}Else{log. Printf ("Mail send:\r\n%s", msg)}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.