Go language Send mail to

Source: Internet
Author: User
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!")
}

}

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.