社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
之前写过一个发邮件的,不过没带附档,今天再看了下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>rnTo: %srnCC: %srnSubject: %srnMIME-Version: 1.0rn", emlUser, emlUser, to, cc, subject))
mime.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%srn", boundary))
mime.WriteString("Content-Description: 这是一封带附档的邮件rn")
//邮件普通Text正文
mime.WriteString(fmt.Sprintf("--%srn", boundary))
mime.WriteString("Content-Type: text/plain; charset=utf-8rn")
mime.WriteString("This is a multipart message in MIME format.")
//邮件HTML正文
mime.WriteString(fmt.Sprintf("n--%srn", boundary))
boundaryHtml := "boundaryHtml"
mime.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=%srn", boundaryHtml))
mime.WriteString("Content-Description: Message in alternative text and HTML formsrn")
mime.WriteString(fmt.Sprintf("n--%srn", boundaryHtml))
mime.WriteString(fmt.Sprintf("Content-Type: %s; charset=utf-8rn", "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--rnrn", boundaryHtml))
// 第一个附件
attaFile := "/Users/xcl/xclcode/tconv.go"
attaFileName := "tconv.go"
mime.WriteString(fmt.Sprintf("n--%srn", boundary))
mime.WriteString("Content-Type: application/octet-streamrn")
mime.WriteString("Content-Description: 附一个Go文件rn")
mime.WriteString("Content-Transfer-Encoding: base64rn")
mime.WriteString("Content-Disposition: attachment; filename="" + attaFileName + ""rnrn")
//读取并编码文件内容
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("rn--%srn", boundary))
mime.WriteString("Content-Type: text/plainrn")
mime.WriteString("Content-Description: 附一个Text文件rn")
mime.WriteString("Content-Disposition: attachment; filename="test.txt"rnrn")
mime.WriteString("this is the attachment text")
//邮件结束
mime.WriteString("rn--" + boundary + "--rnrn")
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
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!