feat(Go-StudyExample):2020/12/29:新增【Go每日一库】emial发送邮件

This commit is contained in:
Huangzj
2020-12-29 11:55:19 +08:00
parent ac81840d80
commit 20c2c7fab0
7 changed files with 164 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
/*
* @Author : huangzj
* @Time : 2020/12/29 11:24
* @Description
*/
package email
import (
"github.com/jordan-wright/email"
"log"
"net/smtp"
"testing"
)
func TestSendAnnex(t *testing.T) {
e := email.NewEmail()
e.From = "hzj <你的邮箱@qq.com>"
e.To = []string{"hzj <接受者的邮箱@qq.com>"}
e.Subject = "附件"
e.Text = []byte("附件")
_, _ = e.AttachFile("囚徒健身2翻译版 修改.pdf")
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "你的邮箱@qq.com", "你的stmp的密码", "smtp.qq.com"))
if err != nil {
log.Fatal(err)
}
}
+55
View File
@@ -0,0 +1,55 @@
/*
* @Author : huangzj
* @Time : 2020/12/29 11:32
* @Description
*/
package email
import (
"fmt"
"github.com/jordan-wright/email"
"log"
"net/smtp"
"os"
"sync"
"testing"
"time"
)
func TestEmailPool(t *testing.T) {
ch := make(chan *email.Email, 4)
//创建2个连接的邮件池
p, err := email.NewPool("smtp.qq.com:587", 2, smtp.PlainAuth("", "你的邮箱@qq.com", "你的stmp的密码", "smtp.qq.com"))
if err != nil {
log.Fatal("failed to create pool:", err)
}
//进行邮件发送
var wg sync.WaitGroup
wg.Add(2)
for i := 0; i < 2; i++ {
go func() {
defer wg.Done()
for e := range ch {
err := p.Send(e, 1*time.Second)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "email:%v sent error:%v\n", e, err)
}
}
}()
}
//发送四个邮件
for i := 0; i < 4; i++ {
e := email.NewEmail()
e.From = "hzj <你的邮箱@qq.com>"
e.To = []string{"hzj <接受者的邮箱@qq.com>"}
e.Subject = "Awesome web"
e.Text = []byte(fmt.Sprintf("Awesome Web %d", i+1))
ch <- e
}
close(ch)
wg.Wait()
}
+40
View File
@@ -0,0 +1,40 @@
/*
* @Author : huangzj
* @Time : 2020/12/29 11:19
* @Description 测试发送html,包含抄送对象
*/
package email
import (
"github.com/jordan-wright/email"
"log"
"net/smtp"
"testing"
)
func TestSendHtml(t *testing.T) {
e := email.NewEmail()
e.From = "hzj <你的邮箱@qq.com>"
e.To = []string{"hzj <接受者的邮箱@qq.com>"}
//设置抄送目标
e.Cc = []string{"hzj <抄送邮箱@qq.com>"}
e.Subject = "参考:Go 每日一库"
e.HTML = []byte(`
<H1>参考:Go 每日一库 的email库</H1>
<ul>
<li><a "https://darjun.github.io/2020/01/10/godailylib/flag/">Go 每日一库之 flag</a></li>
<li><a "https://darjun.github.io/2020/01/10/godailylib/go-flags/">Go 每日一库之 go-flags</a></li>
<li><a "https://darjun.github.io/2020/01/14/godailylib/go-homedir/">Go 每日一库之 go-homedir</a></li>
<li><a "https://darjun.github.io/2020/01/15/godailylib/go-ini/">Go 每日一库之 go-ini</a></li>
<li><a "https://darjun.github.io/2020/01/17/godailylib/cobra/">Go 每日一库之 cobra</a></li>
<li><a "https://darjun.github.io/2020/01/18/godailylib/viper/">Go 每日一库之 viper</a></li>
<li><a "https://darjun.github.io/2020/01/19/godailylib/fsnotify/">Go 每日一库之 fsnotify</a></li>
<li><a "https://darjun.github.io/2020/01/20/godailylib/cast/">Go 每日一库之 cast</a></li>
</ul>
`)
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "你的邮箱@qq.com", "你的stmp的密码", "smtp.qq.com"))
if err != nil {
log.Fatal(err)
}
}
+27
View File
@@ -0,0 +1,27 @@
/*
* @Author : huangzj
* @Time : 2020/12/29 11:10
* @Description
*/
package email
import (
"log"
"net/smtp"
"testing"
"github.com/jordan-wright/email"
)
func TestSimpleSend(t *testing.T) {
e := email.NewEmail()
e.From = "hzj <你的邮箱@qq.com>"
e.To = []string{"接受者的邮箱@qq.com"}
e.Subject = "测试"
e.Text = []byte("Text Body is, of course, supported!")
err := e.Send("smtp.qq.com:587", smtp.PlainAuth("", "你的邮箱@qq.com", "你的stmp的密码", "smtp.qq.com"))
if err != nil {
log.Fatal(err)
}
}
+13
View File
@@ -0,0 +1,13 @@
获取库命令:go get github.com/jordan-wright/email
参考地址:https://segmentfault.com/a/1190000021761747
参考地址:https://github.com/darjun/go-daily-lib
使用说明:
先要打开邮箱,通过上方的设置找到STMP服务,打开该服务,获取对应的密码
搜索对应邮箱的STMP地址和端口号,这个百度就可以找到
填写对应的发送邮箱和接受邮箱,并设置STMP密码即可
Binary file not shown.
+2
View File
@@ -41,6 +41,8 @@ json与struct之间转换处理工具使用示例
2020/12/29:新增【Go每日一库】copier复制不同结构体的同名属性
2020/12/29:新增【Go每日一库】emial发送邮件
# mod vendor模式加载包
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到