Golang writing SMTP Mailbox service side

Source: Internet
Author: User
Package Mainimport ("FMT", "Net" "OS") type Mail struct {clientName string from string to String Data string Recivedata Bool}func main () {listen, err: = Net. LISTENTCP ("TCP", &net. Tcpaddr{net. Parseip ("127.0.0.1"), +, ""}) if err! = Nil {fmt. PRINTLN ("Listener Port failed:", err.) Error ()) return} defer listen. Close () recive (listen)}func recive (Listen *net. TcpListener) {for {conn, err: = Listen. ACCEPTTCP ()//Receive connection defer Conn. Close () if err! = Nil {fmt. PRINTLN ("Client connection exception", err.) Error ())//Client connection exception continue} conn. Write ([]byte ("Gomailsever")) fmt. PRINTLN ("New Client", Conn. Remoteaddr (). String ()) go func () {tmpbuffer: = make ([]byte, 0)//Create pending byte slice readerchannel: = Mak E (Chan []byte, 16)//Create channel communication pipeline _mail: = &mail{} Go Reader (Readerchannel, Conn, _mail)//chan Nel information receiver data: = Make ([]byte, 1024)//Create output buffer tile for {i, err: = conn. Read//reads data from the socket if err! = Nil {fmt. PRINTLN ("Read error occurred", err.) Error ())//Read failure errors occurred return} Tmpbuffer = Depackmail (Append (tmpbuffer, dat A[:I], Readerchannel)//handle byte//fmt with a custom protocol. Println (data, String)}} ()}}func reader (Readerchannel Chan []byte, Conn *net. Tcpconn, _mail *mail) {for {select {case data: = <-readerchannel:fmt.  Println (String (data[0:4))) switch string (Data[0:4]) {case "HELO": _mail.clientname = String (data[4+2:]) Conn. Write ([]byte ("Give Me Mail")) Break Case "Mail": _mail.from = String (data[4+2 :]) Conn. Write ([]byte ("Give I AM Fine")) Break Case "RCPT": _mail.to = String (data[4+2:]) Conn. Write ([]byte ("Give I AM Fine")) Break Case "DATA": Conn. Write ([]byte ("354 Give I AM Fine")) Break Case "QUIT": Conn. Write ([]byte ("221 Byebye")) file, _: = OS. Create ("./mail/3.eml") file. WriteString (_mail.data) file. Close () fmt.                Println (_mail) Break default: _mail.data = _mail.data + string (data) Fmt. Println (String (_mail.data[len (_mail.data)-1:])) If String (_mail.data[len (_mail.data)-1:]) = = "." {FMT. Println ("111") Conn. Write ([]byte ("Give I AM Fine")} Break}}}}func Depackmail (Buffe R []byte, Readerchannel Chan []byte] []byte {length: = Len (buffer) var i int for i = 0; i < length;        {If length < i+4 {//2-bit for tail mark break} var sindex int for dump: = i; Dump < length-1;            dump++ {if buffer[dump] = = Byte (all) && buffer[dump+1] = = Byte (Ten) {Sindex = dump  }} if Sindex <= 0 {break} data: = Buffer[i:sindex] Readerchannel <-data//sent to the pipeline i = Sindex + 2} if i = = length {//All processing complete return empty package returns make ([]byte, 0)} RE Turn buffer[i:]//Not processed finished return remaining packages}

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.