当前位置:网站首页>Golang发送邮件库email
Golang发送邮件库email
2022-07-27 02:37:00 【Crisp_LF】
Golang发送邮件库email
- golang本身有一个SMTP协议,但是这个协议配置起来比较的复杂,所以我们使用一下第三方库来实现。
人性化的golang电子邮件库
email 包装设计的易于使用,足够的灵活。不会受到限制。是一个非常人性化的golang邮件库
email库现在支持以下内容
- 发件人,收件人,密件抄送和抄送字段
- “[email protected]”和“[email protected]”格式的电子邮件地址
- 文本和HTML消息正文
- 附件
- 阅读回执
安装使用
go get github.com/jordan-wright/email
- ️:email这个库的版本 >1需要Go1.5或更高的版本
- ️:如果您需要与以前的Go版本进行兼容,可以使用
gopkg.in/jordan-wright/email.v1中的以前的包

案例
使用QQ发送电子邮件
package main
import (
"github.com/jordan-wright/email"
"net/smtp"
)
func main() {
//实例化返回一个结构体
e := email.NewEmail()
//From:谁发来的
e.From = "Crisp <[email protected]>"
//To:发给谁的
e.To = []string{
"[email protected]"}
//抄送,这个地方抄送的意思是,这个邮件在发送后还可以抄送给谁
e.Bcc = []string{
"[email protected]"}
e.Cc = []string{
"[email protected]"}
//主题,标题
e.Subject = "测试golang email库"
//普通文本内容,支持html
e.Text = []byte("小朋友!!!")
e.HTML = []byte("<h1>html 小朋友!!!</h1>")
//send方法:smtp.qq.com:587:QQ email相关的域名端口号 smtp.PlainAuth:第一个参数为空,第二个参数为自己的邮箱,第三个参数为授权码,下面有讲如何获取授权码
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "[email protected]", "授权码", "smtp.qq.com"))
if err != nil {
return
}
}
如何获取验证密码


创建结构直接创建电子邮件
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: "测试golang email库",
Text: []byte("小朋友!!!"),
HTML: []byte("<h1>html 小朋友!!!</h1>"),
Headers: textproto.MIMEHeader{
},
}
//send方法:smtp.qq.com:587:QQ email相关的域名端口号 smtp.PlainAuth:授权 第三个参数授权码需要注意,上面有讲如何获取授权码
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "[email protected]", "授权码", "smtp.qq.com"))
if err != nil {
return
}
}
Io.Reader创建电子邮件
o.Reader 实现这个接口的任意类型创建电子邮件 email.NewEmailFromReader
附加文件
e := NewEmail()
e.AttachFile("test.txt")
使用连接池来发送大量邮件
(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
}
}
}()
}
边栏推荐
- 222. 完全二叉树的节点个数
- Maximum continuous subsequence (day 77)
- LeetCode 第二十八天
- 0726~简历梳理面试总结
- Interview question: the difference between three instantiated objects in string class
- NLP hotspots from ACL 2022 onsite experience
- Bean Validation原理篇--07
- Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
- 一维数组的应用
- Vector to SVG method
猜你喜欢

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

VR全景现在是不是刚需?看完你就明白了

函数指针与回调函数

Programming implementation of eight queens

Application of one-dimensional array

Connman introduction

次轮Okaleido Tiger即将登录Binance NFT,引发社区热议

Message queue learning -- Concepts

STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)

Maximum subarray cumulative sum less than or equal to K
随机推荐
Deployment of ruoyi's environment and operation of the system
Using redis C library, the problem of asynchronous memory leakage
03.获取网页源代码
VR全景制作在家装行业是谈单利器?这是为什么呢?
Leetcode- > dichotomy (III)
阿里云服务器域名加端口网页不能访问问题记录
C#怎么实现给Word每一页设置不同文字水印
Textbox in easyUI inserts content at the cursor position
Worthington papain dissociation system solution
FastBoot brush machine
分析一下CSDN大佬写的CAS,可重入锁, 非公平锁
flask_ Reqparse parser inheritance in restful
Share the current life -- a six week internship experience of a high school graduate in CCTV
DataX无法连接对应的数据库(windows下可以,linux下失败)
Greenplum [deployment 08] database small version upgrade process and problem handling error: open-source-greenplum-db-6 conflicts with
Number of 0 at the end of factorial
It's confirmed that the registration of soft exam in the second half of 2022 will start in August
安装umi4阻碍一天的问题解决了
NLP hotspots from ACL 2022 onsite experience
Installation and use of anti-virus software ClamAV