当前位置:网站首页>Golang sends email to the mail Library
Golang sends email to the mail Library
2022-07-27 04:05:00 【Crisp_ LF】
Golang Send mail Library email
- golang There is one in itself SMTP agreement , But the configuration of this protocol is more complex , So we use a third-party library to realize .
Humanized golang Email Library
email The packaging design is easy to use , Flexible enough . No restrictions . It is a very humanized golang Mail Library
email The library now supports the following
- Sender , The recipient , BCC and CC fields
- “[email protected]” and “[email protected]” Format email address
- The text and HTML Message body
- The attachment
- Reading receipt
Install and use
go get github.com/jordan-wright/email
- ️:email The version of this library >1 need Go1.5 Or later
- ️: If you need to work with previous Go Version is compatible with , have access to
gopkg.in/jordan-wright/email.v1The previous package in

Case study
Use QQ Send email
package main
import (
"github.com/jordan-wright/email"
"net/smtp"
)
func main() {
// Instantiation returns a structure
e := email.NewEmail()
//From: Who sent it
e.From = "Crisp <[email protected]>"
//To: To whom
e.To = []string{
"[email protected]"}
// CC , This place CC means , Who else can this email be copied to after it is sent
e.Bcc = []string{
"[email protected]"}
e.Cc = []string{
"[email protected]"}
// The theme , title
e.Subject = " test golang email library "
// General text content , Support html
e.Text = []byte(" Children !!!")
e.HTML = []byte("<h1>html Children !!!</h1>")
//send Method :smtp.qq.com:587:QQ email Relevant domain name port number smtp.PlainAuth: The first parameter is empty , The second parameter is your mailbox , The third parameter is authorization code , Here is how to get the authorization code
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "[email protected]", " Authorization code ", "smtp.qq.com"))
if err != nil {
return
}
}
How to get the authentication password


Create structure directly create email
package main
import (
"github.com/jordan-wright/email"
"net/smtp"
"net/textproto"
)
func main() {
e := &email.Email{
To: []string{
"[email protected]"},
From: "Crisp <[email protected]>",
Subject: " test golang email library ",
Text: []byte(" Children !!!"),
HTML: []byte("<h1>html Children !!!</h1>"),
Headers: textproto.MIMEHeader{
},
}
//send Method :smtp.qq.com:587:QQ email Relevant domain name port number smtp.PlainAuth: to grant authorization The third parameter authorization code needs attention , It tells how to get the authorization code
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "[email protected]", " Authorization code ", "smtp.qq.com"))
if err != nil {
return
}
}
Io.Reader Create email
o.Reader Any type that implements this interface creates an email email.NewEmailFromReader
Additional documents
e := NewEmail()
e.AttachFile("test.txt")
Use connection pooling to send large amounts of mail
(var ch <-chan *email.Email)
p := email.NewPool(
"smtp.gmail.com:587",
4,
smtp.PlainAuth("", "[email protected]", "ryqnogv3456dmaceiha", "smtp.qq.com"),
)
for i := 0; i < 4; i++ {
go func() {
for e := range ch {
err := p.Send(e, 10*time.Second)
if err != nil {
return
}
}
}()
}
边栏推荐
- Chapter 4 decision tree and random forest
- Article main content extraction software [based on NLP technology]
- Use websocket to realize a web version of chat room (fishing is more hidden)
- 小于等于K的最大子数组累加和
- 电商系统结合商品秒杀活动,VR全景不断带来收益
- Review in the sixth week
- Kettle reads file split by line
- URDF_ Xcaro
- LeetCode 第二十八天
- PSINS工具箱中轨迹生成工具详细解析
猜你喜欢

Director of meta quest content ecology talks about the original intention of APP lab design

Function pointer and callback function

Towhee weekly model

Program to change the priority of the process in LabVIEW

NFT数字藏品系统开发:小蚁数智帮助品牌一键上链发行NFT

Redis (IX) - redis distributed lock

Application, addition and deletion of B-tree

Chapter 4 决策树和随机森林

九方智投是正规公司吗?一起聊聊九方智投

Framework学习之旅:init 进程启动过程
随机推荐
The problem that prevented the installation of umi4 for one day has been solved
NFT digital collection system development: old brand literary magazines play with trendy Digital Collections
Use websocket to realize a web version of chat room (fishing is more hidden)
LeetCode 第二十七天
「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣
Do you really understand code rollback?
Redis(九) - Redis之分布式锁
路由策略第一关
Cocos game practice-04-collision detection and NPC rendering
Case when in MySQL returns multiple field processing schemes
Maximum subarray cumulative sum less than or equal to K
【无标题】
第六周复习
Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
222. 完全二叉树的节点个数
商业打假系列之第一百之--无聊的制度和管理流程真的可以扔进垃圾桶-顺便分析十几个无用的Unity游戏自检项目
【SemiDrive源码分析】【驱动BringUp】41 - LCM 驱动 backlight 背光控制原理分析
VR全景人淘金“小心机”(上)
Is it safe for tongdaxin to open an account
NFT数字藏品系统开发:老牌文学杂志玩起新潮数字藏品