p.Send(e, 10*time.Second)
实例
- 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
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 发送邮件测试"
推荐阅读
- word2007向程序发送命令时出现问题 word向程序发送命令时出现问题怎么办
- 曝福原爱常受江宏杰的语言暴力!婆婆形容她是能下蛋的鸡,剧情又反转
- 华为手机如何发送短信 手机如何发送短信
- 网络语言母鸡的意思 母鸡的意思
- 自然语言处理现如今面临着哪些挑战呢
- 6种流行的前端开发语言
- 新手适合学的四种编程语言,你pick哪一个?
- 深度NLP模型的泛化问题
- 深度学习和自然语言处理新泛化方法Work Shop
- 从语言模型到多模态,AI大模型如何当好“人类助手”引热议
