發送郵件是實際業務中經常會用到的一個功能,而在Go語言中實現發送郵件的庫也有很多,這篇文章將介紹go語言中如何發郵件。
1. 登錄QQ郵箱,選擇賬戶,開啟POP3/SMTP服務和IMAP/SMTP服務,並生成授權碼


2. 使用go語言的smtp包發送郵件
- go_email/email.go
func SendEduEmail(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 + "\r\nFrom: " + user + ">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)
send_to := strings.Split(to, ";")
err := smtp.SendMail(host, auth, user, send_to, msg)
return err
}
- main.go
package main
import (
"fmt"
)
func main() {
send_email_test1()
}
iunc send_email_test1() {
var to = []string{"15735177116@163.com", "201800910862@b.sxmu.edu.cn"}
from := "1271570224@qq.com"
nickname := "張亞飛"
secret := "xxxxxxxx"
host := "smtp.qq.com"
port := 25
subject := "Perfect Vue Admin 發送郵件測試"
body := "測試內容"
if err := go_email.SendEmail(from, to, secret, host, nickname, subject, body, port, true); err != nil{
fmt.Println("發送失敗: ", err)
}else {
fmt.Println("發送成功")
}
}
3. 使用"jordan-wright/email"發送郵件
關鍵參數
// Email is the type used for email messages
type Email struct {
ReplyTo []string
From string
To []string
Bcc []string
Cc []string
Subject string
Text []byte // Plaintext message (optional)
HTML []byte // Html message (optional)
Sender string // override From as SMTP envelope sender (optional)
Headers textproto.MIMEHeader
Attachments []*Attachment
ReadReceipt []string
}
- From:發件人郵箱,格式為“名稱+<郵箱>”,也可以直接寫郵箱,默認顯示的發件人為@符號前的名稱
- To:收件人郵箱地址
- Subject:郵件標題
- Text:郵件正文
調用Send方法發送郵件,第一個參數是你的發件人郵箱的SMTP域名+端口號,第二個參數用於身份認證
e.Send("smtp.163.com:25", smtp.PlainAuth("", "pingyeaa@163.com", "<你的密碼>", "smtp.163.com"))
smtp.PlainAuth
- 參數1:通常,identity應該是空字符串,以用作用戶名。
- 參數2:用戶郵箱
- 參數3:密碼,如果拿到了授權碼,則填寫授權碼
- 參數4:服務器地址,163的地址是smtp.163.com,其他平台可自行查看
CC與BCC
CC全稱是Carbon Copy,意為抄送,BCC全稱Blind Carbon Copy,意為暗抄送,收件人看不到被暗抄送給了誰。
e := email.NewEmail()
e.Cc = []string{"xxxxxxx@qq.com"}
e.Bcc = []string{"xxxxxxx@qq.com"}
另一種初始化方式
可以實例化的時候直接賦值
發送附件
發送附件非常簡單,直接傳入文件名即可
連接池
由於頻繁發送郵件會不斷的與SMTP服務器建立連接,比較影響性能,所以email提供了連接池的功能
auth := smtp.PlainAuth("", "pingyeaa@163.com", "<你的密碼>", "smtp.163.com")
p, _ := email.NewPool("smtp.163.com:25", 4, auth)
創建成功后,就可以借助連接池來發送郵件
實例
- go_email/email.go
package go_email
import (
"crypto/tls"
"fmt"
"github.com/jordan-wright/email"
"log"
"net/smtp"
"strings"
"time"
)
var (
pool *email.Pool
maxClient int = 10
)
func SendEmail(from string, to []string, secret string, host string, nickname string, subject string, body string, port int, ssl bool) error {
auth := smtp.PlainAuth("", from, secret, host)
e := email.NewEmail()
e.From = fmt.Sprintf("%s<%s>", nickname, from)
e.To = to
e.Subject = subject
e.HTML = []byte(body)
hostAddr := fmt.Sprintf("%s:%d", host, port)
if ssl {
return e.SendWithTLS(hostAddr, auth,&tls.Config{ServerName: host})
}
return e.Send(hostAddr, auth)
}
func SendEmailWithFile(from string, to []string, secret string, host string, nickname string, subject string, body string, port int, ssl bool,attach string) error {
auth := smtp.PlainAuth("", from, secret, host)
e := email.NewEmail()
e.From = fmt.Sprintf("%s<%s>", nickname, from)
e.To = to
e.Subject = subject
e.HTML = []byte(body)
if attach != ""{
_,_ = e.AttachFile(attach)
}
hostAddr := fmt.Sprintf("%s:%d", host, port)
if ssl {
return e.SendWithTLS(hostAddr, auth,&tls.Config{ServerName: host})
}
return e.Send(hostAddr, auth)
}
func SendEmailWithPool(to []string, from, secret, host, subject, body, nickname string, port int) (err error) {
hostAddr := fmt.Sprintf("%s:%d", host, port)
auth := smtp.PlainAuth("", from, secret, host)
if pool == nil {
pool, err = email.NewPool(hostAddr, maxClient, auth)
if err != nil {
log.Fatal(err)
}
}
e := &email.Email{
From: fmt.Sprintf("%s<%s>", nickname, from),
To: to,
Subject: subject,
Text: []byte(body),
}
return pool.Send(e, 5 * time.Second)
}
func SendEmailWithPoolAndFile(to []string, from, secret, host, subject, body, nickname string, port int, attach string) (err error) {
hostAddr := fmt.Sprintf("%s:%d", host, port)
auth := smtp.PlainAuth("", from, secret, host)
if pool == nil {
pool, err = email.NewPool(hostAddr, maxClient, auth)
if err != nil {
log.Fatal(err)
}
}
e := &email.Email{
From: fmt.Sprintf("%s<%s>", nickname, from),
To: to,
Subject: subject,
Text: []byte(body),
}
if attach != "" {
_, _ = e.AttachFile(attach)
}
return pool.Send(e, 5 * time.Second)
}
- main.go
package main
import (
"fmt"
go_email "go_dev/go_email/email"
)
func main() {
//send_email_test1()
//send_email_test2()
//send_email_test3()
send_email_test4()
}
func send_email_test4() {
var to = []string{"15735177116@163.com", "1271570224@qq.com"}
from := "201800910862@b.sxmu.edu.cn"
nickname := "張亞飛"
secret := "xxxxxxxx"
host := "smtp.exmail.qq.com"
port := 25
subject := "Perfect Vue Admin 發送郵件測試"
body := "測試內容"
if err := go_email.SendEmailWithPoolAndFile(to, from, secret, host, subject, body, nickname, port, "簡稱.txt"); err != nil{
fmt.Println("發送失敗: ", err)
}else {
fmt.Println("發送成功")
}
}
func send_email_test3() {
var to = []string{"15735177116@163.com", "201800910862@b.sxmu.edu.cn"}
from := "1271570224@qq.com"
nickname := "張亞飛"
secret := "vsloiltxbxyzgeii"
host := "smtp.qq.com"
port := 25
subject := "Perfect Vue Admin 發送郵件測試"
body := "測試內容"
if err := go_email.SendEmailWithPoolAndFile(to, from, secret, host, subject, body, nickname, port, "簡稱.txt"); err != nil{
fmt.Println("發送失敗: ", err)
}else {
fmt.Println("發送成功")
}
}
func send_email_test2() {
to := "1271570224@qq.com;15735177116@163.com"
user := "201800910862@b.sxmu.edu.cn"
password := "xxxxxx3"
host := "smtp.exmail.qq.com:25"
//host := "smtp.exmail.qq.com"
subject := "Perfect Vue Admin 發送郵件測試"
body := "測試內容"
if err := go_email.SendEduEmail(user, password, host, to, subject, body, "html"); err != nil{
fmt.Println("發送失敗: ", err)
}else {
fmt.Println("發送成功")
}
}
func send_email_test1() {
var to = []string{"15735177116@163.com", "201800910862@b.sxmu.edu.cn"}
from := "1271570224@qq.com"
nickname := "張亞飛"
secret := "xxxxxx"
host := "smtp.qq.com"
port := 25
subject := "Perfect Vue Admin 發送郵件測試"
body := "測試內容"
if err := go_email.SendEmail(from, to, secret, host, nickname, subject, body, port, true); err != nil{
fmt.Println("發送失敗: ", err)
}else {
fmt.Println("發送成功")
}
四、常用郵箱服務器地址與端口
163.com: POP3服務器地址:pop.163.com(端口:110) SMTP服務器地址:smtp.163.com(端口:25) 126郵箱: POP3服務器地址:pop.126.com(端口:110) SMTP服務器地址:smtp.126.com(端口:25) 139郵箱: POP3服務器地址:POP.139.com(端口:110) SMTP服務器地址:SMTP.139.com(端口:25) QQ郵箱: POP3服務器地址:pop.qq.com(端口:110) SMTP服務器地址:smtp.qq.com (端口:25) QQ企業郵箱 : POP3服務器地址:pop.exmail.qq.com (SSL啟用 端口:995) SMTP服務器地址:smtp.exmail.qq.com(SSL啟用 端口:587/465) gmail(google.com) : POP3服務器地址:pop.gmail.com(SSL啟用 端口:995) SMTP服務器地址:smtp.gmail.com(SSL啟用 端口:587) Foxmail: POP3服務器地址:POP.foxmail.com(端口:110) SMTP服務器地址:SMTP.foxmail.com(端口:25) sina.com: POP3服務器地址:pop3.sina.com.cn(端口:110) SMTP服務器地址:smtp.sina.com.cn(端口:25) sinaVIP: POP3服務器:pop3.vip.sina.com (端口:110) SMTP服務器:smtp.vip.sina.com (端口:25) sohu.com: POP3服務器地址:pop3.sohu.com(端口:110) SMTP服務器地址:smtp.sohu.com(端口:25) yahoo.com: POP3服務器地址:pop.mail.yahoo.com SMTP服務器地址:smtp.mail.yahoo.com yahoo.com.cn: POP3服務器地址:pop.mail.yahoo.com.cn(端口:995) SMTP服務器地址:smtp.mail.yahoo.com.cn(端口:587 ) HotMail : POP3服務器地址:pop3.live.com (端口:995) SMTP服務器地址:smtp.live.com (端口:587) 263.net: POP3服務器地址:pop3.263.net(端口:110) SMTP服務器地址:smtp.263.net(端口:25) 263.net.cn: POP3服務器地址:pop.263.net.cn(端口:110) SMTP服務器地址:smtp.263.net.cn(端口:25) x263.net: POP3服務器地址:pop.x263.net(端口:110) SMTP服務器地址:smtp.x263.net(端口:25) 21cn.com: POP3服務器地址:pop.21cn.com(端口:110) SMTP服務器地址:smtp.21cn.com(端口:25) china.com: POP3服務器地址:pop.china.com(端口:110) SMTP服務器地址:smtp.china.com(端口:25) tom.com: POP3服務器地址:pop.tom.com(端口:110) SMTP服務器地址:smtp.tom.com(端口:25) etang.com: POP3服務器地址:pop.etang.com SMTP服務器地址:smtp.etang.com
