gomail发送附件

采用github.com/go-gomail/gomail/ 的邮件功能,可以发送附件 以及html文档,下面是其给出的demo,测试通过。

 

package main

//cmd:  go get gopkg.in/gomail.v1

import (
    "gopkg.in/gomail.v1"
)

func main() {
    msg := gomail.NewMessage()
    msg.SetHeader("From", "xxx@163.com")
    msg.SetHeader("To", "zzzzzzz@qq.com", "xxx@163.com")
    //msg.SetAddressHeader("Cc", "dan@example.com", "Dan")
    msg.SetHeader("Subject", "Hello!")
    msg.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")

    f, err := gomail.OpenFile("C:/Documents and Settings/yan/My Documents/My Pictures/old/bg.jpg")
    if err != nil {
        panic(err)
    }
    msg.Attach(f)

    //smtp.163.com:25
    mailer := gomail.NewMailer("smtp.163.com", "xxx@163.com", "xxxxxxx", 25)
    if err := mailer.Send(msg); err != nil {
        panic(err)
    }
}

 

posted on 2015-04-02 09:47  rojas  阅读(1628)  评论(0编辑  收藏  举报