当前位置:网站首页>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
}
}
}()
}
边栏推荐
- Chapter 5 decision tree and random forest practice
- STM32CubeMX学习笔记(41)——ETH接口+LwIP协议栈使用(DHCP)
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- C. Cypher
- Message queue learning -- Concepts
- 【安卓小叙】Kotlin多线程编程(一)
- Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment
- Basic concept and essence of Architecture
- [tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
- Use websocket to realize a web version of chat room (fishing is more hidden)
猜你喜欢

Application of one-dimensional array

Cocos game practice-04-collision detection and NPC rendering

Characteristics and determination scheme of Worthington pectinase

手动从0搭建ABP框架-ABP官方完整解决方案和手动搭建简化解决方案实践
![[tree chain dissection] template question](/img/6b/7ec6f36d5f2373aee163c2cb766b29.png)
[tree chain dissection] template question

connman介绍

【愚公系列】2022年7月 Go教学课程 018-分支结构之switch

Interview question: the difference between three instantiated objects in string class

Plato farm is expected to further expand its ecosystem through elephant swap

科目三: 济南章丘6号线
随机推荐
Meta Quest内容生态总监谈App Lab设计初衷
GetObject call timing of factorybean
Chapter 5 决策树和随机森林实践
Digital analog 1232
路由策略第一关
NFT数字藏品系统开发:小蚁数智帮助品牌一键上链发行NFT
大家有遇到CDC读MySQL字段不全的情况吗?怎么处理的?
03. Get the web page source code
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
04. Detailed steps for installing the simulated browser chromedriver in Google browser
Two help points distribution brings to merchants
函数指针与回调函数
222. Number of nodes of complete binary tree
C language force deduction question 43 string multiplication. Optimized vertical
Specific use of multithreading
Source code analysis of openfeign
flinkSQLclient创建的job,flink重启就没了,有什么办法吗?
03.获取网页源代码
[Android synopsis] kotlin multithreaded programming (I)
Briefly sort out the dualpivotquicksort