• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

Golang: 简单smtp邮件发送样例

go 水墨上仙 2672次浏览

用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


喜欢 (0)
加载中……