如何通过 SMTP 方式发送带附件的邮件?
本文介绍如何通过SMTP方式发送带附件的邮件。
通过 SMTP 的方式发送带附件的邮件的方法就是:构建一封 MIME 格式的邮件内容。
MIME 基础知识
MIME 表示多用途 Internet 邮件扩允协议。MIME 扩允了基本的面向文本的 Internet 邮件系统,以便可以在消息中包含二进制附件。
MIME 信息由正常的 Internet 文本邮件组成,文本邮件拥有符合 RFC 2822/5322 的信息头和格式化过的信息体。
MIME 协议的 RFC 地址:https://www.ietf.org/rfc/rfc2045.txt 。
MIME 信息剖析
一封普通的文本邮件的信息包含一个头部分(例如:From、To、Subject 等等)和一个体部分。体部分通常为单体类型(例如:text、image、audio、video、application 等等)或是复合类型(即:multipart)。头部分和体部分之间用一个空行进行分隔,并且体部分的类型由信头内容类型字段 Content-Type 描述。
信头含义 (Headers)
域名 | 含义 |
---|---|
Received | 传输路径 |
Return-Path | 回复地址 |
Delivered-To | 发送地址 |
Reply-To | 回复地址 |
From | 发件人地址 |
To | 收件人地址 |
Cc | 抄送地址 |
Bcc | 暗送地址 |
Date | 日期和时间 |
Subject | 主题 |
Message-ID | 消息 ID |
MIME-Version | MIME 版本 |
Content-Type | 内容的类型 |
Content-Transfer-Encoding | 内容的传输编码方式 |
内容类型(Content-Type),表现形式为:Content-Type: [type]/[subtype]。
其中 type 的形式为:
text:用于标准化地表示的文本信息,文本消息可以是多种字符集和或者多种格式的。
Image:用于传输静态图片数据。
Audio:用于传输音频或者音声数据。
Video:用于传输动态影像数据,可以是与音频编辑在一起的视频数据格式。
Application:用于传输应用程序数据或者二进制数据。
Message:用于包装一个 E-mail 消息。
Multipart:用于连接消息体的多个部分构成一个消息,这些部分可以是不同类型的数据。
其中 subtype 用于指定 type 的详细形式,常用的 subtype 如下所示:
text/plain(纯文本)
text/html(HTML 文档)
application/xhtml+xml(XHTML 文档)
image/gif(GIF 图像)
image/jpeg(JPEG 图像)
image/png(PNG 图像)
video/mpeg(MPEG 动画)
application/octet-stream(任意的二进制数据)
message/rfc822(RFC 822 形式)
multipart/alternative(HTML 邮件的 HTML 形式和纯文本形式,相同内容使用不同形式表示。)
内容传输编码(Content-Transfer-Encoding),指定内容区域使用的字符编码方式。通常为:7bit,8bit,binary,quoted-printable,base64。
MIME 的信体部分
邮件中常见的简单类型有 text/plain(纯文本)和 text/html(超文本)。
复杂的邮件内容格式采用 multipart 类型,可以包括纯文本/超文本、内嵌资源(图片)、附件类型等等。
multipart 类型的邮件体被分为多个段,每个段又包含段头和段体两部分,这两部分之间也以空行分隔。
段头含义:
域名 | 含义 |
---|---|
Content-Type | 段体的类型 |
Content-Transfer-Encoding | 段体的传输编码方式 |
Content-Disposition | 段体的安排方式 |
Content-ID | 段体的 ID |
Content-Location | 段体的位置(路径) |
Content-Base | 段体的基位置 |
常见的 multipart 类型有三种:multipart/mixed, multipart/related 和 multipart/alternative。
复合类型层次关系示例图:

multipart 诸类型的共同特征是,在段头指定 boundary 参数字符串,段体内的每个子段以此字符串定界。所有的子段都以 —boundary 行开始,父段则以 —boundary— 行结束。段与段之间也以空行分隔。
注意:
附件邮件总大小不超过15M,一次最多不超过100个附件。
补充说明:15MB是指smtp发信邮件实际总大小,由于base64编码邮件代码会膨胀1.5倍以上,总大小非客户侧看到的大小,附件限制建议按照8MB来准备。若需要发送大附件,建议内容里加超链接的方式发送。
代码示例(python 2.7)
# -*- coding:utf-8 -*-
import urllib, urllib2
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.header import Header
# 发件人地址,通过控制台创建的发件人地址
username = 'xxx@xxx.com'
# 发件人密码,通过控制台创建的发件人密码
password = 'XXXXXXXX'
# 收件人地址列表,支持多个收件人,最多30个
rcptlist = ['to1@to.com', 'to2@to.com']
receivers = ','.join(rcptlist)
# 构建 multipart 的邮件消息
msg = MIMEMultipart('mixed')
msg['Subject'] = 'Test Email'
msg['From'] = username
msg['To'] = receivers
# 构建 multipart/alternative 的 text/plain 部分
alternative = MIMEMultipart('alternative')
textplain = MIMEText('纯文本部分', _subtype='plain', _charset='UTF-8')
alternative.attach(textplain)
# 构建 multipart/alternative 的 text/html 部分
texthtml = MIMEText('超文本部分', _subtype='html', _charset='UTF-8')
alternative.attach(texthtml)
# 将 alternative 加入 mixed 的内部
msg.attach(alternative)
# 附件类型
# xlsx 类型的附件
xlsxpart = MIMEApplication(open('测试文件1.xlsx', 'rb').read())
xlsxpart.add_header('Content-Disposition', 'attachment', filename=Header("测试文件1.xlsx","utf-8").encode())
msg.attach(xlsxpart)
# jpg 类型的附件
jpgpart = MIMEApplication(open('2.jpg', 'rb').read())
jpgpart.add_header('Content-Disposition', 'attachment', filename=Header("2.jpg","utf-8").encode())
msg.attach(jpgpart)
# mp3 类型的附件
mp3part = MIMEApplication(open('3.mp3', 'rb').read())
mp3part.add_header('Content-Disposition', 'attachment', filename=Header("3.mp3","utf-8").encode())
msg.attach(mp3part)
# 发送邮件
try:
client = smtplib.SMTP()
#python 2.7以上版本,若需要使用SSL,可以这样创建client
#client = smtplib.SMTP_SSL()
client.connect('smtpdm.aliyun.com')
client.login(username, password)
#发件人和认证地址必须一致
client.sendmail(username, rcptlist, msg.as_string())
client.quit()
print '邮件发送成功!'
except smtplib.SMTPRecipientsRefused:
print '邮件发送失败,收件人被拒绝'
except smtplib.SMTPAuthenticationError:
print '邮件发送失败,认证错误'
except smtplib.SMTPSenderRefused:
print '邮件发送失败,发件人被拒绝'
except smtplib.SMTPException,e:
print '邮件发送失败, ', e.message
代码示例(GO)
package main
import (
"bytes"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"mime"
"net/smtp"
"strings"
"time"
)
// define email interface, and implemented auth and send method
type Mail interface {
Auth()
Send(message Message) error
}
type SendMail struct {
user string
password string
host string
port string
auth smtp.Auth
}
type Attachment struct {
name string
contentType string
withFile bool
}
type Message struct {
from string
to []string
cc []string
bcc []string
subject string
body string
contentType string
attachment Attachment
}
func main() {
user := "XXX@XXXXX.top"
password := "TESXXXXXX"
host := "smtpdm.aliyun.com"
port := "25"
var mail Mail
mail = &SendMail{user: user, password: password, host: host, port: port}
message := Message{from: user,
to: []string{"XXXXX@qq.com", "XX@qq.com", "XXX@163.com"},
cc: []string{},
bcc: []string{},
subject: "HELLO WORLD",
body: "哈哈哈哈哈哈哈",
contentType: "text/plain;charset=utf-8",
// attachment: Attachment{
// name: "test.jpg",
// contentType: "image/jpg",
// withFile: true,
// },
attachment: Attachment{
name: "D:\\goProjects\\src\\测试pdf.pdf",
contentType: "application/octet-stream",
withFile: true,
},
}
err := mail.Send(message)
if err != nil {
fmt.Println("Send mail error!")
fmt.Println(err)
} else {
fmt.Println("Send mail success!")
}
}
func (mail *SendMail) Auth() {
// mail.auth = smtp.PlainAuth("", mail.user, mail.password, mail.host)
mail.auth = LoginAuth(mail.user, mail.password)
}
func (mail SendMail) Send(message Message) error {
mail.Auth()
buffer := bytes.NewBuffer(nil)
boundary := "GoBoundary"
Header := make(map[string]string)
Header["From"] = message.from
Header["To"] = strings.Join(message.to, ";")
Header["Cc"] = strings.Join(message.cc, ";")
Header["Bcc"] = strings.Join(message.bcc, ";")
Header["Subject"] = message.subject
Header["Content-Type"] = "multipart/mixed;boundary=" + boundary
Header["Mime-Version"] = "1.0"
Header["Date"] = time.Now().String()
mail.writeHeader(buffer, Header)
body := "\r\n--" + boundary + "\r\n"
body += "Content-Type:" + message.contentType + "\r\n"
body += "\r\n" + message.body + "\r\n"
buffer.WriteString(body)
if message.attachment.withFile {
attachment := "\r\n--" + boundary + "\r\n"
attachment += "Content-Transfer-Encoding:base64\r\n"
attachment += "Content-Disposition:attachment\r\n"
attachment += "Content-Type:" + message.attachment.contentType + ";name=\"" + mime.BEncoding.Encode("UTF-8", message.attachment.name) + "\"\r\n"
buffer.WriteString(attachment)
defer func() {
if err := recover(); err != nil {
log.Fatalln(err)
}
}()
mail.writeFile(buffer, message.attachment.name)
}
to_address := MergeSlice(message.to, message.cc)
to_address = MergeSlice(to_address, message.bcc)
buffer.WriteString("\r\n--" + boundary + "--")
err := smtp.SendMail(mail.host+":"+mail.port, mail.auth, message.from, to_address, buffer.Bytes())
return err
}
func MergeSlice(s1 []string, s2 []string) []string {
slice := make([]string, len(s1)+len(s2))
copy(slice, s1)
copy(slice[len(s1):], s2)
return slice
}
func (mail SendMail) writeHeader(buffer *bytes.Buffer, Header map[string]string) string {
header := ""
for key, value := range Header {
header += key + ":" + value + "\r\n"
}
header += "\r\n"
buffer.WriteString(header)
return header
}
// read and write the file to buffer
func (mail SendMail) writeFile(buffer *bytes.Buffer, fileName string) {
file, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err.Error())
}
payload := make([]byte, base64.StdEncoding.EncodedLen(len(file)))
base64.StdEncoding.Encode(payload, file)
buffer.WriteString("\r\n")
for index, line := 0, len(payload); index < line; index++ {
buffer.WriteByte(payload[index])
if (index+1)%76 == 0 {
buffer.WriteString("\r\n")
}
}
}
type loginAuth struct {
username, password string
}
func LoginAuth(username, password string) smtp.Auth {
return &loginAuth{username, password}
}
func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
// return "LOGIN", []byte{}, nil
return "LOGIN", []byte(a.username), nil
}
func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
}
}
return nil, nil
}