• GoLang 中发送 email 邮件


    方法 1,使用官方的 net/smtp

    package main
    
    import (
    	"log"
    	"net/smtp"
    )
    
    const (
    	SMTPHost     = "smtp.gmail.com"
    	SMTPPort     = ":587"
    	SMTPUsername = "xxx@gmail.com"
    	SMTPPassword = "xxxx"
    )
    
    func sendEmail(receiver string) {
    	auth := smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
    	msg := []byte("Subject: 这里是标题内容
    
    " + "这里是正文内容
    ")
    	err := smtp.SendMail(SMTPHost+SMTPPort, auth, SMTPUsername, []string{receiver}, msg)
    	if err != nil {
    		log.Fatal("failed to send email:", err)
    	}
    }
    
    func sendHTMLEmail(receiver string, html []byte) {
    	auth := smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
    	msg := append([]byte("Subject: 这里是标题内容
    "+
    		"MIME-version: 1.0;
    Content-Type: text/html; charset="UTF-8";
    
    "), 
    		html...)
    	err := smtp.SendMail(SMTPHost+SMTPPort, auth, SMTPUsername, []string{receiver}, msg)
    	if err != nil {
    		log.Fatal("failed to send email:", err)
    	}
    }
    
    func main() {
    	sendHTMLEmail("接受者@gmail.com", []byte("<html><h2>这是网页内容</h2></html>"))
    }
    

    方法2. 使用 jordan-wright 库

    package main
    
    import (
    	"log"
    	"fmt"
    	"net/smtp"
    	"github.com/jordan-wright/email"
    )
    
    const (
    	SMTPHost     = "smtp.gmail.com"
    	SMTPPort     = ":587"
    	SMTPUsername = "xxx@gmail.com"
    	SMTPPassword = "xxxx"
    )
    
    func sendEmail(receiver string) {
    	auth := smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
    	e := &email.Email{
    		From:    fmt.Sprintf("发送者名字<%s>", SMTPUsername),
    		To:      []string{receiver},
    		Subject: "这里是标题内容",
    		Text:    []byte("这里是正文内容"),
    	}
    	err := e.Send(SMTPHost+SMTPPort, auth)
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    
    func sendHTMLEmail(receiver string, html []byte) {
    	auth := smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
    	e := &email.Email{
    		From:    fmt.Sprintf("发送者名字<%s>", SMTPUsername),
    		To:      []string{receiver},
    		Subject: "这里是标题内容",
    		HTML:    html,
    	}
    	err := e.Send(SMTPHost+SMTPPort, auth)
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    
    func main() {
    	sendHTMLEmail("接受者@gmail.com", []byte("<html><h2>这是网页内容</h2></html>"))
    }
    

    方法3. 使用 jordan-wright 库的 Pool

    package main
    
    import (
    	"log"
    	"fmt"
    	"time"
    	"net/smtp"
    	"github.com/jordan-wright/email"
    )
    
    const (
    	SMTPHost     = "smtp.gmail.com"
    	SMTPPort     = ":587"
    	SMTPUsername = "xxx@gmail.com"
    	SMTPPassword = "xxxx"
    	MaxClient    = 5
    )
    
    var pool *email.Pool
    
    func sendEmail(receiver string) {
    	var err error
    	if pool == nil {
    		pool, err = email.NewPool(SMTPHost+SMTPPort, MaxClient, smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost))
    		if err != nil {
    			log.Fatal(err)
    		}
    	}
    	e := &email.Email{
    		From:    fmt.Sprintf("发送者名字<%s>", SMTPUsername),
    		To:      []string{receiver},
    		Subject: "这里是标题内容",
    		Text:    []byte("这里是正文内容"),
    	}
    	err =  pool.Send(e, 5 * time.Second)
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    

    对比总结

    如果只是发送少量邮件,可以使用前两种方法。但是如果需要一次性发送较多邮件,需要使用第三种方法,即连接池。

  • 相关阅读:
    HDoj-2072-字数
    hibou 主界面自己侧滑的定义
    Android得到一个闹钟在第三方
    UILabel,UITextField 以及UIButton应用
    推荐几个好文章
    半年后,我还在路上。
    NGUI 3.5过程(三)Button button
    OpenGL研究2.0 计算圆
    CF 444A(DZY Loves Physics-低密度脂蛋白诱导子图)
    美日高价进口中国非转基因大豆:不仅吃还做药
  • 原文地址:https://www.cnblogs.com/flipped/p/13845690.html
Copyright © 2020-2023  润新知