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

Golang: 简单smtp邮件发送样例

发布时间:2020-12-16 19:00:51 所属栏目:大数据 来源:网络整理
导读:用GO语言写邮件发送发现也是件很简单和方便的事,可能最关键还是对字符处理和业务逻辑上的很麻烦,不过主体的smtp邮件发送功能是很简单的。最近在思考一个邮件发送路由,主要为的是解决分布式邮件报警和保证邮件送达率为100%. 样例参考:http://code.google.c

用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: %srnrn",*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
看看是不是收到邮件了。发送到多个人就是这样方便。

(编辑:李大同)

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

    推荐文章
      热点阅读