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, ";")