go语言发送邮件(带附件)

 package main

import (
	"fmt"
	"net/mail"
	"net/smtp"
	"time"

"github.com/scorredoira/email"

"github.com/Unknwon/com"
"github.com/astaxie/beego"
)

func main() {

from := beego.AppConfig.String("from")
password := beego.AppConfig.String("password")
smtpport := beego.AppConfig.String("smtpport")
smtpaddress := beego.AppConfig.String("smtpaddress")
tolist := beego.AppConfig.String("tolist")
subject := beego.AppConfig.String("subject")

body := beego.AppConfig.String("subject")
fmt.Println(body)
m := email.NewMessage(subject, body)

m.From = mail.Address{Name: "一花一世界", Address: from}
m.To = []string{tolist}
fmt.Println(m.To)

//检查附件是否存在
attachpath := beego.AppConfig.String("attachpath")
attachfilename := beego.AppConfig.String("attachfilename")
currentdate := time.Now().Format("20060102")
attachfile := attachpath + "\\" + attachfilename + "-" + currentdate + "_" + "v1.0" + ".rar"
fmt.Println("附件名:" + attachfile)

if isexist := com.IsExist(attachfile); isexist == false {
	fmt.Println("no attach")
	return
}

if err := m.Attach(attachfile); err != nil {
	fmt.Println("append attach error")
	return
}

auth := smtp.PlainAuth("", from, password, smtpaddress)
err := email.Send(smtpport, auth, m)
if err != nil {
	fmt.Println("send mail error!")
	fmt.Println(err)
} else {
	fmt.Println("send mail success!")
}

}

app.conf文件中的内容

from=888888@163.com
password=888888
smtpport=smtp.163.com:25
smtpaddress=smtp.163.com

attachpath=D:\发版相关
attachfilename=1.0.6.2版本发布

tolist=****@qq.com
subject:=“hello”

body= “云TA的升级包,具体见附件。”

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值