【go语言发送电子邮件】go语言版发送电子邮件 - Go语言中文社区

【go语言发送电子邮件】go语言版发送电子邮件


一、实现功能
用go语言发送一封邮件

二、实现源代码

package main
import (
    "net/smtp"
    "fmt"
    "strings"
)

/*
 *  user : example@example.com login smtp server user
 *  password: xxxxx login smtp server password
 *  host: smtp.example.com:port   smtp.163.com:25
 *  to: example@example.com;example1@163.com;example2@sina.com.cn;...
 *  subject:The subject of mail
 *  body: The content of mail
 *  mailtyoe: mail type html or text
 */

func SendMail(user, password, host, to, subject, body, mailtype string) error{
    hp := strings.Split(host, ":")
    auth := smtp.PlainAuth("", user, password, hp[0])
    var content_type string
    if 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 + "rnFrom: " + user + "<"+ user +">rnSubject: " + subject + "rn" + content_type + "rnrn" + body)
    send_to := strings.Split(to, ";")
    err := smtp.SendMail(host, auth, user, send_to, msg)
    return err
}

func main() {
    user := "此处填写qq邮箱账号"
    password := "此处填写qq邮箱授权码"
    host := "smtp.qq.com:587"
    to := "18720081236m@sina.cn"
    subject := "Test send email by golang"

    body := `
`
    fmt.Println("send email")
    err := SendMail(user, password, host, to, subject, body, "text")
    if err != nil {
        fmt.Println("send mail error!")
        fmt.Println(err)
    }else{
        fmt.Println("send mail success!")
    }

}


运行效果

"D:Program Files (x86)JetBrainsGogland 171.3780.106binrunnerw.exe" D:/Gobingo.exe run D:/Go/code/src/awesomeProject/go_email.go
send email
send mail success!

Process finished with exit code 0

版本2(发送多个人 ,HTML格式):

package main
import (
    "net/smtp"
    "fmt"
    "strings"
)

/*
 *  user : example@example.com login smtp server user
 *  password: xxxxx login smtp server password
 *  host: smtp.example.com:port   smtp.163.com:25
 *  to: example@example.com;example1@163.com;example2@sina.com.cn;...
 *  subject:The subject of mail
 *  body: The content of mail
 *  mailtyoe: mail type html or text
 */

func SendMail(user, password, host, to, subject, body, mailtype string) error{
    hp := strings.Split(host, ":")
    auth := smtp.PlainAuth("", user, password, hp[0])
    var content_type string
    if 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 + "rnFrom: " + user + "<"+ user +">rnSubject: " + subject + "rn" + content_type + "rnrn" + body)
    send_to := strings.Split(to, ";")
    err := smtp.SendMail(host, auth, user, send_to, msg)
    return err
}

func main() {
    user := "1973536419@qq.com"
    password := "XXXXXXXX"
    host := "smtp.qq.com:587"
    to := "defa.lai@cgtz.com;fang.chen@lg-finance.com";


    subject := "Test send email by golang"

    body := `
    <!DOCTYPE html>
        <html>
        <body>
        <div style="text-align:center;width:78.78%;padding: 8px; line-height: 1.42857; vertical-align: top; border-top-width: 1px; border-top-color: rgb(221, 221, 221); background-color: #28a745;color:#fff"><strong>用户工资数据报表</strong></div>

            <table border="1" style="width:80%;">
          <tr style="background-color:pink;text-align:center;">
            <th>月份</th>
            <th>存款</th>
            <th>工资</th>
            <th>年薪</th>


          </tr>
          <tr  style="text-align:center;">
            <td>一月</td>
            <td>1000 元</td>
            <td>3000 元</td>
            <td>12000元</td>

          </tr>
          <tr  style="text-align:center;">
            <td>二月</td>
            <td>1500 元</td>
            <td>4000 元</td>
            <td>16000 元</td>
          </tr>
        </table>

        </body>
        </html>


    `
    fmt.Println("send email")
    err := SendMail(user, password, host, to, subject, body, "html")
    if err != nil {
        fmt.Println("send mail error!")
        fmt.Println(err)
    }else{
        fmt.Println("send mail success!")
    }

}

这里写图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u013421629/article/details/72722787
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-02-02 17:58:42
  • 阅读 ( 1113 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢