GO语言net实现SMTP

Posted sunansheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GO语言net实现SMTP相关的知识,希望对你有一定的参考价值。

直接上代码:

package main

import (
    "encoding/base64"
    "fmt"
    "net"
    "time"
)

type Sender struct 
    UserName string
    Password string
    From     string
    To       string
    Subject  string
    Text     string
    Host     string


func NewSender(host, userName, password string, to string, subject, text string) (r_sender Sender) 
    r_sender.UserName = userName
    r_sender.Password = password
    r_sender.From = userName
    r_sender.To = to
    r_sender.Subject = subject
    r_sender.Text = text
    r_sender.Host = host
    return


func (this *Sender) SendMail() (e error) 
    var (
        deadline time.Duration = 5 * time.Second
        encoding *base64.Encoding
        buf      []byte = make([]byte, 512)
        r        int
    )
    encoding = base64.StdEncoding
    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]))

    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[0:r]))

    fmt.Println(encoding.EncodeToString([]byte(this.UserName)))

    conn.Write([]byte(encoding.EncodeToString([]byte(this.UserName)) + "\\r\\n"))
    conn.SetDeadline(time.Now().Add(deadline))
    r, e = conn.Read(buf)
    if e != nil 
        return
    
    fmt.Println(string(buf[:r]))

    fmt.Println(encoding.EncodeToString([]byte(this.Password)))

    conn.Write([]byte(encoding.EncodeToString([]byte(this.Password)) + "\\r\\n"))
    conn.SetDeadline(time.Now().Add(deadline))
    r, e = conn.Read(buf)
    if e != nil 
        return
    
    fmt.Println(string(buf[:r]))

    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]))

    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]))

    conn.Write([]byte("DATA\\r\\n"))
    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]))

    return


func main() 

    s := NewSender("smtp.qq.com:25",
        "a@qq.com",
        "pwd",
        "abc@qq.com",
        "主题",
        "内容")
    e := s.SendMail()
    if e != nil 
        fmt.Println(e)
    


声明:Nansheng.Su 发表于 2019-04-28 16:55:33 ,共计9字。

转载请署名:GO语言net实现SMTP | www.sunansheng.com

以上是关于GO语言net实现SMTP的主要内容,如果未能解决你的问题,请参考以下文章

go smtp示例

golang 使用Go(Golang)通过GMail发送电子邮件与net / smtp

Go发送Email

Go语言基础之net/http

go语言实现 tcp客户端/服务端

Go语言HTTPServer开发的六种实现