Go language SMTP instruction send mail (instance)

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

Simple Mail Transfer Protocol (SMTP) is a text-based e-mail Transfer protocol that is used in the Internet to exchange messages between mail servers. SMTP is an application-tier service that can be adapted to a variety of network systems. The commands and responses for SMTP are text-based, command-line units, and cr/lf (\ r \ n). The response information is usually only one line, starting with a 3-digit code, followed by a brief textual description.

SMTP has 3 stages of establishing connections, sending messages, and releasing connections. The specific:

(1) Establish a TCP connection.
(2) The client sends a Hello command to the server to identify the sender's own identity, and then the client sends the Mail command. (EHLO is an extension instruction, different servers have different instructions)
(3) The server side responds with OK, indicating that it is ready to receive.
(4) The client sends the RCPT command.
(5) The server side indicates whether it is willing to receive mail for the recipient.
(6) End of negotiation, send mail, send input with command data.


The following is an example of a smtp.163.com server:

Package Mailimport ("Encoding/base64" "FMT" "Net" "Time") const (TB string = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/") type Sender struct {UserName Stringpassword Stringfrom stringto stringsubject stringtext stringhost string}func newsender (host, userName, password String, to string, subject, text string) (R_sender sender) {R_sender. UserName = Usernamer_sender. Password = Passwordr_sender. from = Usernamer_sender. to = Tor_sender. Subject = Subjectr_sender. Text = Textr_sender. Host = Hostreturn}func (this *sender) SendMail () (e error) {var (deadline time). Duration = 5 * time. Secondencoding *base64. encodingbuf []byte = make ([]byte, MB) r int) encoding = base64. Newencoding (TB) conn, E: = Net. Dial ("TCP", this. Host) If E! = nil {Return}defer Conn. Close () Conn. Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte ("EHLO juxuny\r\n")) Conn. Setdeadline (time. Now (). ADD (Deadline)) R, E= Conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte ("AUTH login\r\n")) Conn. Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte (Encoding. Encodetostring ([]byte (this. UserName)) (conn) + "\ r \ n"). Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte (Encoding. Encodetostring ([]byte (this. Password)) (conn) + "\ r \ n"). Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte ("MAIL from: <" + this.) From + ">" + "\ r \ n") Conn. Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte ("RCPT to <" + this.) to + ">\r\n")) Conn. Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {return}fmt. Println (String (buf[:r-1])) Conn. Write ([]byte ("data\r\n")) Conn. Write ([]byte ("\ r \ n")) conN.write ([]byte ("Message-id: <" + this. From + ">\r\n")) Conn. Write ([]byte ("X-mailer: <mmail 1.0>")) Conn. Write ([]byte ("mime-version:1.0")) Conn. Write ([]byte ("Content-type:text/plain")) Conn. Write ([]byte ("From: <" + this.) From + ">\r\n")) Conn. Write ([]byte ("To: <" + this.) to + ">\r\n")) Conn. Write ([]byte ("Subject:" + this.) Subject + "\ r \ n")) Conn. Write ([]byte ("\ r \ n")) Conn. Write ([]byte (this. TEXT)) Conn. Write ([]byte ("\r\n.\r\n")) time. Sleep (5E9) Conn. Write ([]byte ("quit\r\n")) Conn. Setdeadline (time. Now (). ADD (Deadline)) R, E = conn. Read (BUF) if E! = nil {fmt. Println (e) return}fmt. Println (String (buf[:r-1))) return}


Package Mainimport ("./mail" "FMT")//newsender (host, userName, password string, to []string, subject, text string) func mai N () {s: = mail. Newsender ("smtp.163.com:25", "killuaxyz@163.com", "**********", "976813280@qq.com", "Hello Mail", "Test Mail") e: = S. SendMail () if E! = nil {fmt. Println (e)}}

Output Result:
163.com Anti-Spam GT for Coremail System (163com[20121016]) 250-mail250-pipelining250-auth LOGIN Plain250-auth=login Plain250-coremail 1uxr2xkj7kg0xki17xgru7i0s8fy2u3uj8cz28x1uuuuu7ic2i0y2urz5c00uca0xdruuuuj250-starttls250 8bitmime334 dxnlcm5hbwu6334 ugfzc3dvcmq6235 authentication successful250 mail OK250 mail OK354 End data with <cr>< ; lf>.<cr><lf>250 Mail OK Queued as Smtp10,dscowebjw2q4bqjtgf3hba--. 94s2 1392641336421 closing Transmission Channel

The connection is successful and returns:
163.com Anti-Spam GT for Coremail System (163com[20121016])

Conn. Write([]byte("AUTHLOGIN\ r \ n")) after that, the server returns:334 Dxnlcm5hbwu6

(using Base64decode, to decrypt "Dxnlcm5hbwu6" is actually "username:")

Send a Base64 encrypted username here

Corresponding, after receiving 334 Ugfzc3dvcmq6 , it is necessary to send Base64 encrypted account password in the past, if the authentication is successful, the server returns:235 authentication Successful

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.