之前写过一个发邮件的,不过没带附档,今天再看了下smtp协议,做了个带附档的邮件发送例子,也就这样吧。
package main
/*
了解下smtp协议,并做了个小演示:
利用Go自带的net/smtp包,发送带附档的邮件。
Author: XCL
Date: 2016-5-7
Blog: http://blog.csdn.net/xcl168
*/
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"net/smtp"
"strings"
)
const (
emlUser = "xcl@xxx.com"
emlPwd = "-------"
emlSMTP = "smtp.xxx.com:25"
)
func main() {
err := eml()
if err != nil {
fmt.Println(" err:", err)
}
}
// 发普通文本邮件
func eml() error {
to := "xcl@xxx.com"
cc := "cc@xxx.com"
sendTo := strings.Split(to, ";")
subject := "这是一封演示用的邮件"
boundary := "next message" //boundary 用于分割邮件内容,可自定义. 注意它的开始和结束格式
mime := bytes.NewBuffer(nil)
//设置邮件
mime.WriteString(fmt.Sprintf("From: %s<%s>\r\nTo: %s\r\nCC: %s\r\nSubject: %s\r\nMIME-Version: 1.0\r\n", emlUser, emlUser, to, cc, subject))
mime.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\r\n", boundary))
mime.WriteString("Content-Description: 这是一封带附档的邮件\r\n")
//邮件普通Text正文
mime.WriteString(fmt.Sprintf("--%s\r\n", boundary))
mime.WriteString("Content-Type: text/plain; charset=utf-8\r\n")
mime.WriteString("This is a multipart message in MIME format.")
//邮件HTML正文
mime.WriteString(fmt.Sprintf("\n--%s\r\n", boundary))
boundaryHtml := "boundaryHtml"
mime.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=%s\r\n", boundaryHtml))
mime.WriteString("Content-Description: Message in alternative text and HTML forms\r\n")
mime.WriteString(fmt.Sprintf("\n--%s\r\n", boundaryHtml))
mime.WriteString(fmt.Sprintf("Content-Type: %s; charset=utf-8\r\n", "text/html"))
mime.WriteString(`<html><body>
<p><img src="https://golang.org/doc/gopher/doc.png"></p><br/>
<h1>最近有点消沉,也有点想家了.</h1>
</body></html>`)
mime.WriteString(fmt.Sprintf("\n--%s--\r\n\r\n", boundaryHtml))
// 第一个附件
attaFile := "/Users/xcl/xclcode/tconv.go"
attaFileName := "tconv.go"
mime.WriteString(fmt.Sprintf("\n--%s\r\n", boundary))
mime.WriteString("Content-Type: application/octet-stream\r\n")
mime.WriteString("Content-Description: 附一个Go文件\r\n")
mime.WriteString("Content-Transfer-Encoding: base64\r\n")
mime.WriteString("Content-Disposition: attachment; filename=\"" + attaFileName + "\"\r\n\r\n")
//读取并编码文件内容
attaData, err := ioutil.ReadFile(attaFile)
if err != nil {
return err
}
b := make([]byte, base64.StdEncoding.EncodedLen(len(attaData)))
base64.StdEncoding.Encode(b, attaData)
mime.Write(b)
//第二个附件
mime.WriteString(fmt.Sprintf("\r\n--%s\r\n", boundary))
mime.WriteString("Content-Type: text/plain\r\n")
mime.WriteString("Content-Description: 附一个Text文件\r\n")
mime.WriteString("Content-Disposition: attachment; filename=\"test.txt\"\r\n\r\n")
mime.WriteString("this is the attachment text")
//邮件结束
mime.WriteString("\r\n--" + boundary + "--\r\n\r\n")
fmt.Println(mime.String())
//发送相关
smtpHost, _, err := net.SplitHostPort(emlSMTP)
if err != nil {
return err
}
auth := smtp.PlainAuth("", emlUser, emlPwd, smtpHost)
return smtp.SendMail(emlSMTP, auth, emlUser, sendTo, mime.Bytes())
}
/*
邮件内容:
From: alert@xxx.com<alert@xxx.com>
To: xcl@xxx.com
CC: cc@xxx.com
Subject: 这是一封演示用的邮件
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=next message
Content-Description: 这是一封带附档的邮件
--next message
Content-Type: text/plain; charset=utf-8
This is a multipart message in MIME format.
--next message
Content-Type: multipart/alternative; boundary=boundaryHtml
Content-Description: Message in alternative text and HTML forms
--boundaryHtml
Content-Type: text/html; charset=utf-8
<html><body>
<p><img src="https://golang.org/doc/gopher/doc.png"></p><br/>
<h1>最近有点消沉,也有点想家了.</h1>
</body></html>
--boundaryHtml--
--next message
Content-Type: application/octet-stream
Content-Description: 附一个Go文件
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="tconv.go"
cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJzdHJjb252IgoJInN0cmluZ3MiCikKCmZ1bmMgbWFpbigpIHsKCXMgOj0gIi0xIgoKCXYsIGVyciA6PSBzdHJjb252LkF0b2kocykKCglpZiBlcnIgIT0gbmlsIHsKCQlmbXQuUHJpbnRsbigiIGVycjoiLCBlcnIpCgl9IGVsc2UgewoJCWZtdC5QcmludGxuKCIgdjoiLCB2KQoJfQoKCWFjdGxpc3QgOj0gImEsYixjIgoKCXh4IDo9IHN0cmluZ3MuU3BsaXQoYWN0bGlzdCwgIiwiKQoJZm9yIF8sIHYgOj0gcmFuZ2UgeHggewoJCWZtdC5QcmludGxuKCIgdjoiLCB2KQoJfQoKfQo=
--next message
Content-Type: text/plain
Content-Description: 附一个Text文件
Content-Disposition: attachment; filename="test.txt"
this is the attachment text
--next message--
➜ emlatt :
*/
收到的邮件:
参考URL: https://msdn.microsoft.com/en-us/library/ms526560(v=exchg.10).aspx
BLOG: http://blog.csdn.net/xcl168