用GO语言写邮件发送发现也是件很简单和方便的事,可能最关键还是对字符处理和业务逻辑上的很麻烦,不过主体的smtp邮件发送功能是很简单的。最近在思考一个邮件发送路由,主要为的是解决分布式邮件报警和保证邮件送达率为100%.
样例参考:google.com/p/go-wiki/wiki/SendingMail
” target=”_blank”>http://code.google.com/p/go-wiki/wiki/SendingMail
具体其它用法可以参考pkg/net/smtp
sendMail.go 基于Go Version 1
转自:http://www.ohlinux.com/archives/816/
package main import ( "log" "net/smtp" "flag" "fmt" "strings" ) var ( subject = flag.String( "s","","subject of the mail" ) body= flag.String( "b","","body of themail" ) reciMail = flag.String( "m","","recipient mail address" ) ) func main() { // Set up authentication information. flag.Parse() sub := fmt.Sprintf("subject: %s\r\n\r\n",*subject) content := *body mailList := strings.Split( *reciMail,",") auth := smtp.PlainAuth( "", "smtpuser@example.com", "password", "smtp.example.com", //"smtp.gmail.com", ) // Connect to the server, authenticate, set the sender and recipient, // and send the email all in one step. err := smtp.SendMail( "smtp.example.com:25", auth, "senduser@example.com", mailList, []byte(sub+content), ) if err != nil { log.Fatal(err) } }
使用范例
./sendMail -s "发送测试邮件主题" -b "我的邮件内容 yyyyyyyy" -m user1@gmail.com,user2@qq.com