Golang: 简单smtp邮件发送样例

前端之家收集整理的这篇文章主要介绍了Golang: 简单smtp邮件发送样例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用GO语言写邮件发送发现也是件很简单和方便的事,可能最关键还是对字符处理和业务逻辑上的很麻烦,不过主体的smtp邮件发送功能是很简单的。最近在思考一个邮件发送路由,主要为的是解决分布式邮件报警和保证邮件送达率为100%.

样例参考:http://code.google.com/p/go-wiki/wiki/SendingMail

具体其它用法可以参考pkg/net/smtp

sendMail.go 基于Go Version 1

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
看看是不是收到邮件了。发送到多个人就是这样方便。 原文链接:https://www.f2er.com/go/191360.html

猜你在找的Go相关文章