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: 这里是标题内容\r\n\r\n" + "这里是正文内容\r\n")
	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: 这里是标题内容\r\n"+
		"MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\r\n\r\n"), 
		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)
	}
}

对比总结

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


┆凉┆暖┆降┆等┆幸┆我┆我┆里┆将┆ ┆可┆有┆谦┆戮┆那┆ ┆大┆始┆ ┆然┆
┆薄┆一┆临┆你┆的┆还┆没┆ ┆来┆ ┆是┆来┆逊┆没┆些┆ ┆雁┆终┆ ┆而┆
┆ ┆暖┆ ┆如┆地┆站┆有┆ ┆也┆ ┆我┆ ┆的┆有┆精┆ ┆也┆没┆ ┆你┆
┆ ┆这┆ ┆试┆方┆在┆逃┆ ┆会┆ ┆在┆ ┆清┆来┆准┆ ┆没┆有┆ ┆没┆
┆ ┆生┆ ┆探┆ ┆最┆避┆ ┆在┆ ┆这┆ ┆晨┆ ┆的┆ ┆有┆来┆ ┆有┆
┆ ┆之┆ ┆般┆ ┆不┆ ┆ ┆这┆ ┆里┆ ┆没┆ ┆杀┆ ┆来┆ ┆ ┆来┆
posted @ 2020-10-20 11:48  水郁  阅读(1585)  评论(0编辑  收藏  举报
……