加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

GoLang语言:邮件群发器

发布时间:2020-12-16 18:47:19 所属栏目:大数据 来源:网络整理
导读:好久没有更新博客了,这次又带来了一段GO语言的代码(没办法,只会写代码了)。 Go语言邮件群发器 main.go //Multi-SendEmailprojectmain.gopackagemainimport("bufio""errors""fmt""io""io/ioutil""net/smtp""os""strings""time")funcSendMail(user,passwor

好久没有更新博客了,这次又带来了一段GO语言的代码(没办法,只会写代码了)。

Go语言邮件群发器 main.go

//Multi-SendEmailprojectmain.go
packagemain

import(
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"net/smtp"
"os"
"strings"
"time"
)

funcSendMail(user,password,host,to,subject,body,mailtypestring)error{
hp:=strings.Split(host,":")
auth:=smtp.PlainAuth("",user,hp[0])
varcontent_typestring
ifmailtype=="html"{
content_type="Content-Type:text/html;charset=UTF-8"
}else{
content_type="Content-Type:text/plain;charset=UTF-8"
}
msg:=[]byte("To:"+to+"rnFrom:"+user+"<"+user+">rnSubject:"+subject+"rn"+content_type+"rnrn"+body)
send_to:=strings.Split(to,";")
err:=smtp.SendMail(host,auth,send_to,msg)
returnerr
}

funcreadLine2Array(filenamestring)([]string,error){
result:=make([]string,0)
file,err:=os.Open(filename)
iferr!=nil{
returnresult,errors.New("Openfilefailed.")
}
deferfile.Close()
bf:=bufio.NewReader(file)
for{
line,isPrefix,err1:=bf.ReadLine()
iferr1!=nil{
iferr1!=io.EOF{
returnresult,errors.New("ReadLinenofinish")
}
break
}
ifisPrefix{
returnresult,errors.New("Lineistoolong")
}
str:=string(line)
result=append(result,str)
}
returnresult,nil
}

funcmain(){
fmt.Println("start...")
user:="邮箱@qq.com"
password:="密码"
host:="smtp.qq.com:25"//QQ为例
subject:="邮件标题"

sendTo,err:=readLine2Array("send.txt")
iferr!=nil{
fmt.Println(err)
return
}

content,err:=ioutil.ReadFile("email.txt")
iferr!=nil{
fmt.Println(err)
return
}
body:=string(content)

fori:=0;i<len(sendTo);i++{
to:=sendTo[i]
fmt.Println("Sendemailto"+to)
err=SendMail(user,"html")
iferr!=nil{
fmt.Println("sendmailerror!")
fmt.Println(err)
i--
time.Sleep(600*time.Second)
}else{
fmt.Println("sendmailsuccess!")
}
}
}


使用说明:

编译出exe文件,把要发送的邮箱一行一个放入send.txt中,邮件内容放入email.txt中(HTML格式),运行。

这代码我也有在用,有很多不灵活的地方,仅供参考。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读