Golang Full Step (TLS +ssl) to send mail via QQ

Source: Internet
Author: User
Tags convert net sprintf starttls account security
This is a creation in Article, where the information may have evolved or changed.

Open Mail First

2 "Settings" account tab found

This will be based on your account security to add a secret insurance, add a secret will give you a key

(2) Verification of the secret warranty

(3) Obtaining the Authorization code

This authorization code will be used.

Advance notice: SSL, TLS, are different protocols

First we use the STARTTLS protocol to write a simple example 1

============= =                    1        &NBSP ;       start     =====================

Package Mainimport ("NET/SMTP" "Strings" "FMT") func sendtomail (user, PWD, host, to, subject, body, mailtype string) error{HP: = Strings. Split (Host, ":") Auth: = SMTP. Plainauth ("", User, PWD, hp[0]) var content_type string if Mailtype = = "html" {content_type = "Content-type:tex t/"+ 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: = "2677031999@qq.com"//The address of the mailbox where you opened the SMTP mailbox PWD: = "GV**************JFH"//here fill in your own authorization code host: = "smtp.qq.com:25" To: = "2677031999@qq.com"//Destination Address Subject: =      "Send mail using Golang" body: = '  

======================= 1 End ====================

Okay, because the SSL protocol is needed for a lot of time.

= = 2 Start ==========

Package Mainimport ("FMT" "Net/smtp" "Crypto/tls" "Net" "Log") func main () {host: = "smtp.qq.com" Port: =465 Email: = "2677031999@qq.com" pwd: = "gvd************fh"//fill in your authorization code here Toemail: = "2677031999@qq.com"//Destination Address Hea Der: = Make (map[string]string) header["from"] = "test" + "<" +email+ ">" header["to"] = Toemail header["Subjec T "] =" message header 11111 "header[" content-type "] =" Text/html;chartset=utf-8 "Body: =" This is a message sent by Golang "message: =" "fo R k,v: =range header{message + = FMT. Sprintf ("%s:%s\r\n", k,v)} message + = "\ r \ n" +body Auth: =smtp. Plainauth ("", email, pwd, host,) Err: = Sendmailusingtls (FMT.      Sprintf ("%s:%d", host, Port), auth, email, []string{toemail}, []byte (Message),) if Err!=nil{ Panic (ERR)}}//return a SMTP Clientfunc Dial (addr string) (*SMTP. Client, error) {conn, err: = TLS. Dial ("TCP", addr, nil) if err! = Nil {log. PANICLN ("Dialing Error:", err)     return nil, err}//Decomposition host port string Host, _, _: = Net. Splithostport (addr) return SMTP. Newclient (conn, host)}//refer to Net/smtp's func SendMail ()//using NET. Dial the TLS (SSL) port is connected, SMTP. When Newclient () is stuck and does not prompt for Err//len (to) >1, To[1] begins with a hint of Bcc sendmailusingtls (addr string, auth SMTP. Auth, from string, to []string, MSG []byte) (err error) {//create SMTP client c, err: = Dial (addr) If err! = Nil {log. Println ("Create smpt Client Error:", err) return err} defer c.close () if auth! = nil {If ok, _: = C.exte Nsion ("AUTH"); OK {if Err = C.auth (Auth); Err! = nil {log. Println ("Error during AUTH", err) Return err}}} If Err = C.mail (from);      Err! = Nil {return Err} for _, Addr: = range to {if Err = c.rcpt (addr); Err! = Nil {return err  }} W, err: = C.data () if err! = Nil {return Err} _, Err = W.write (msg) if err! = Nil {return ERR} err = W.close () if err! = Nil {RETurn err} return C.quit ()} 

======================= 2 End ==============================

Results

In fact, you observe the two methods very similar to the Golang official library NET/SMTP support is STARTTLS,, to support SSL will be the above example is just to convert net Dial to TLS.

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.