Go using SMTP to send mail

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

Core code: SMTP. SendMail (host, auth, user, send_to, msg), auth: = SMTP. Plainauth ("", User, password, hp[0])

Possible scenario: Use the Enterprise mailbox SMTP send mail, Host: = "smtp.exmail.qq.com:25", so need a business mailbox account and password

Directly on the code:

Main.go

package mainimport  ("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 stringif 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: "  +  "\ 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 : =&nBSP; " yang** @yun *.com "password := " * * * * "host := " smtp.exmail.qq.com:25 "to := " 397685131 @qq. com "subject := " uses Golang to send mail "body :=   

Problems encountered:

1, Body: = ", here is the anti-single quotation marks, rather than single quotation marks;

2, Err! = nil,Golang nil in the concept and other languages null, none, nil, NULL, all refer to 0 value or null value. Nil is a pre-stated identifier, also a keyword in the usual sense. In Golang, nil can only be assigned to variables of the pointer, channel, Func, interface, map, or slice type. If this rule is not followed, panic is raised. The official has a clear explanation:http://pkg.golang.org/pkg/builtin/#Type

3, Parameter auth,auth: = SMTP. Plainauth ("", User, password, hp[0])

4. Forced type conversion, msg: = []byte ("to:" + to + "\r\nfrom:" + user + ">\r\nsubject:" + "\ r \ n" + content_type + "\r\n\r\n" + body )

5, multiple mailboxes: send_to: = Strings. Split (To, ";")

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.