好久没有更新博客了,这次又带来了一段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+"\r\nFrom:"+user+"<"+user+">\r\nSubject:"+subject+"\r\n"+content_type+"\r\n\r\n"+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格式),运行。
这代码我也有在用,有很多不灵活的地方,仅供参考。
原文链接:https://www.f2er.com/go/190645.html