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

来源:互联网 发布:linux echo输出到文件 编辑:程序博客网 时间:2024/05/22 03:28

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