【Python】发送邮件,超详细看图敲码(附完整代码)一一CSDN21天学习挑战赛

                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                <p><img src="https://img-blog.csdnimg.cn/9633f3bb7c3643d0a6989e51c0470ac6.gif#pic_center" alt="">​</p> 


本系列文章为参与【Python】CSDN21天学习挑战赛,成为更好的自己,根据自身的学习进度并记录自己的学习过程。我也是Python纯小白,和大家一起学习,保持热爱学习的好习惯😁

活动地址:CSDN21天学习挑战赛


一、邮箱注册

注册网易邮箱,其他邮箱也支持的,我这里以网易为例,都差不多


二、邮箱设置

服务开启

1.注册后登录邮箱,选择我们设置
在这里插入图片描述

2.找到我们的POP3/SMTP/IMAP
在这里插入图片描述

3.需要开启我们的IMAP/SMTP服务和POP3/SMTP服务
在这里插入图片描述
提示:如果服务默认是开启的,但是不知道授权码,可以选择新增,或者删除,关闭服务重新开启,都是可以的
在这里插入图片描述

4.首先我们开启IMAP/SMTP服务,需要发送短信验证,使用手机微信,浏览器扫码都行,发送成功后,点击我已发送
在这里插入图片描述

5.然后就会生成一串授权码,一定一定要先记下来,再点击确定,这里使用设备随便填
在这里插入图片描述

6.接着,我们开启POP3/SMTP服务,一样需要扫码发送短信
在这里插入图片描述

7.开启成功后,就会弹出提示,我们就可以用刚刚的授权码来发送邮件了
在这里插入图片描述


POP3 & SMTP & IMAP 介绍

POP3 是Post Office Protocol 3的简称,即邮局协议的第3个版本,它规定怎样将个人计算机连接到Internet的邮件服务器和下载电子邮件的电子协议。它是因特网电子邮件的第一个离线协议标准,POP3允许用户从服务器上把邮件存储到本地主机(即自己的计算机)上,同时删除保存在邮件服务器上的邮件,而POP3服务器则是遵循POP3协议的接收邮件服务器,用来接收电子邮件的。

SMTP 的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议。它是一组用于从源地址到目的地址传输邮件的规范,通过它来控制邮件的中转方式。SMTP 协议属于 TCP/IP 协议簇,它帮助每台计算机在发送或中转信件时找到下一个目的地。SMTP 服务器就是遵循 SMTP 协议的发送邮件服务器。SMTP 认证,简单地说就是要求必须在提供了账户名和密码之后才可以登录 SMTP 服务器,这就使得那些垃圾邮件的散播者无可乘之机。增加 SMTP 认证的目的是为了使用户避免受到垃圾邮件的侵扰。

IMAP 全称是Internet Mail Access Protocol,即交互式邮件存取协议,它是跟POP3类似邮件访问标准协议之一。不同的是,开启了IMAP后,您在电子邮件客户端收取的邮件仍然保留在服务器上,同时在客户端上的操作都会反馈到服务器上,如:删除邮件,标记已读等,服务器上的邮件也会做相应的动作。所以无论从浏览器登录邮箱或者客户端软件登录邮箱,看到的邮件以及状态都是一致的。

网易163免费邮箱相关服务器信息

服务器名称服务器地址SSL协议端口号非SSL协议端口号
IMAPimap.163.com993143
SMTPsmtp.163.com456/99425
POP3pop.163.com995110

POP3IMAP 的区别

协议允许电子邮件客户端下载服务器上的邮件,但是在客户端的操作(如移动邮件、标记已读等),不会反馈到服务器上,比如通过客户端收取了邮箱中的3封邮件并移动到其他文件夹,邮箱服务器上的这些邮件是没有同时被移动的 。

IMAP 提供webmail 与电子邮件客户端之间的双向通信,客户端的操作都会反馈到服务器上,对邮件进行的操作,服务器上的邮件也会做相应的动作。

同时,IMAPPOP3 那样提供了方便的邮件下载服务,让用户能进行离线阅读。IMAP 提供的摘要浏览功能可以让你在阅读完所有的邮件到达时间、主题、发件人、大小等信息后才作出是否下载的决定。此外,IMAP 更好地支持了从多个不同设备中随时访问新邮件。

操作位置操作内容IMAPPOP3
收件箱阅读,标记,移动,删除邮件等客户端与邮箱更新同步仅客户端内
发件箱保存到已发送客户端与邮箱更新同步仅客户端内
创建文件夹新建自定义文件夹客户端与邮箱更新同步仅客户端内
草稿保存草稿客户端与邮箱更新同步仅客户端内
垃圾文件夹接受误移入垃圾文件夹的邮件支持不支持
广告邮件接受被移入广告文件夹的邮件支持不支持

总之,IMAP 整体上为用户带来更为便捷和可靠的体验。POP3 更易丢失邮件或多次下载相同的邮件,但 IMAP 通过邮件客户端与webmail 之间的双向同步功能很好地避免了这些问题。

注: 若在web邮箱中设置了“保存到已发送”,使用客户端POP服务发信时,已发邮件也会自动同步到网页端“已发送”文件夹内。


三、Python发送邮件

发送默认邮件

1.启动PyCharm
2.新建文件email_163
在这里插入图片描述

3.导入发送邮件模块

# 导入发送邮件模块
import smtplib

4.创建class类

# 发送网易邮箱类
class Email163(object):

5.建议所有不怎么会变动的设置为类常量

# 服务器地址
HOST = 'smtp.163.com'
# 端口
PORT = '25'
# 授权码
PASSWORD = '填写自己的授权码(不是邮箱密码)'
# 登录用户(邮件发送者)
FROM_ADDR = '填写自己发送者的邮箱'

6.构造函数

def __init__(self, msg: str, to_addrs: str) -> None:
    """
        构造函数
        :param msg: 邮件内容
        :param to_addrs: 邮件接收者
        """
    self.msg = msg
    self.to_addrs = to_addrs

7.定义发送默认邮件函数

def send_default_email(self) -> None:
    """
    发送默认邮件
    :return:None
    """
    try:
        # 实例SMTP
        smtp = smtplib.SMTP()
        # 连接邮箱服务器
        smtp.connect(self.HOST, self.PORT)
        # 验证授权码
        smtp.login(self.FROM_ADDR, self.PASSWORD)
        # 发送邮件
        smtp.sendmail(self.FROM_ADDR, self.to_addrs, self.msg)
        # 发送完邮件,关闭服务
        smtp.close()
        print(f'邮件成功发送给:{self.to_addrs}')
    except smtplib.SMTPException:
        raise f'给{to_addrs}发送邮件失败'

8.入口函数调用

if __name__ == '__main__':
    # 邮件内容
    msg = 'hello world'
    # 邮件接收者(填写自己想要发送给别人的邮箱地址)
    # 可以发送多人(可以发给自己),格式:to_addrs = ['980338974@qq.com','*****','********']
    to_addrs = '980338974@qq.com'
    # 实例发送网易邮箱的类
    email163 = Email163(msg, to_addrs)
    # 调用发送邮件函数
    email163.send_default_email()

9.可以看到邮件以及发送成功了
在这里插入图片描述
10.接下来我们登录接收的邮箱看看,发现成功接收了邮件,但是没有主题,也没有任何内容
在这里插入图片描述


邮件格式

我们可以使用第三方库email中的一些模块,更方便快速的构造邮件内容

发送文本格式邮件

提示:如果使用email模块群发邮件,群发报错AttributeError: 'list' object has no attribute 'decode'可以将接收者列表用,或;拼接为字符串','.join(to_addrs)

1.导入模块

# 导入邮件主题构造模块
from email.header import Header
# 导入邮件内容构造模块
from email.mime.text import MIMEText

2.定义发送文本格式邮件函数

def send_text_email(self) -> None:
    """
        发送文本格式邮件
        :return:None
        """
    try:
        # 实例SMTP
        smtp = smtplib.SMTP()
        # 连接邮箱服务器
        smtp.connect(self.HOST, self.PORT)
        # 验证授权码
        smtp.login(self.FROM_ADDR, self.PASSWORD)
        # 构造邮件内容
        # 第一个参数_text:内容
        # 第二个参数_subtype:格式
        # 第三个参数_charset:编码
        # 实例邮件正文
        message = MIMEText('这是使用Python发送的邮件测试!', 'plain', 'utf-8')
        # 构造邮件主题信息
        # 主题
        message['Subject'] = Header('Python发送邮件', 'utf-8')
        # 发送者
        message['From'] = Header(self.FROM_ADDR, 'utf-8')
        # 接收者
        # message['To'] = Header(','.join(self.to_addrs), 'utf-8')  # 接收者为多人[]时,需要用,拼接为字符串
        message['To'] = Header(self.to_addrs, 'utf-8')
    <span class="token comment"># 发送邮件</span>
    smtp<span class="token punctuation">.</span>sendmail<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">,</span> message<span class="token punctuation">.</span>as_string<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 发送完邮件,关闭服务</span>
    smtp<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'邮件成功发送给:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
<span class="token keyword">except</span> smtplib<span class="token punctuation">.</span>SMTPException<span class="token punctuation">:</span>
    <span class="token keyword">raise</span> <span class="token string-interpolation"><span class="token string">f'给</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>to_addrs<span class="token punctuation">}</span></span><span class="token string">发送邮件失败'</span></span>

3.入口函数调用

if __name__ == '__main__':
    # 邮件内容
    msg = 'hello world'
    # 邮件接收者(填写自己想要发送给别人的邮箱地址)
    # 可以发送多人(可以发给自己),格式:to_addrs = ['980338974@qq.com','*****','********']
    to_addrs = '980338974@qq.com'
    # 实例发送网易邮箱的类
    email163 = Email163(msg, to_addrs)
    # 调用发送邮件函数
    email163.send_text_email()

4.再看邮箱,就有主题和内容了
在这里插入图片描述


发送HTML格式邮件

1.定义发送HTML格式邮件函数

def send_html_email(self) -> None:
    """
        发送HTML格式邮件
        :return:None
        """
    try:
        # 实例SMTP
        smtp = smtplib.SMTP()
        # 连接邮箱服务器
        smtp.connect(self.HOST, self.PORT)
        # 验证授权码
        smtp.login(self.FROM_ADDR, self.PASSWORD)
        # 构造邮件内容
        # 第一个参数_text:内容
        # 第二个参数_subtype:格式
        # 第三个参数_charset:编码
        text = """
            <h1>这是使用Python发送的HTML格式邮件测试,我是h1</h1>
            <a href="http://t.csdn.cn/p58Yf">我是一个链接</a>
            <table>
                <th>我是表格列头</th>
                <tr>
                    <td bgcolor="PowderBlue">我是有颜色的表单元格</td>
                </tr>
            </table>
            """
        # 实例邮件正文
        message = MIMEText(text, 'html', 'utf-8')
        # 构造邮件主题信息
        # 主题
        message['Subject'] = Header('Python发送邮件', 'utf-8')
        # 发送者
        message['From'] = Header(self.FROM_ADDR, 'utf-8')
        # 接收者
        message['To'] = Header(','.join(self.to_addrs), 'utf-8')  # 接收者为多人[]时,需要用,拼接为字符串
        # message['To'] = Header(self.to_addrs, 'utf-8')
    <span class="token comment"># 发送邮件</span>
    smtp<span class="token punctuation">.</span>sendmail<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">,</span> message<span class="token punctuation">.</span>as_string<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 发送完邮件,关闭服务</span>
    smtp<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'邮件成功发送给:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
<span class="token keyword">except</span> smtplib<span class="token punctuation">.</span>SMTPException<span class="token punctuation">:</span>
    <span class="token keyword">raise</span> <span class="token string-interpolation"><span class="token string">f'给</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>to_addrs<span class="token punctuation">}</span></span><span class="token string">发送邮件失败'</span></span>

2.入口函数调用

if __name__ == '__main__':
    # 邮件内容
    msg = 'hello world'
    # 邮件接收者(填写自己想要发送给别人的邮箱地址)
    # 可以发送多人(可以发给自己),格式:to_addrs = ['980338974@qq.com','*****','********']
    to_addrs = '980338974@qq.com'
    # 实例发送网易邮箱的类
    email163 = Email163(msg, to_addrs)
    # 调用发送邮件函数
    email163.send_html_email()

发送HTML带内嵌图片

邮件中HTML文本中,如果使用外网链接地址,一般是无效的,同样,我们可以用MIMEImage模块完成

1.导入模块

# 导入邮件混合构造模块
from email.mime.multipart import MIMEMultipart
# 导入邮件图片处理模块
from email.mime.image import MIMEImage

2.定义函数

def send_html_with_image_email(self) -> None:
    """
        发送HTML格式邮件带图片
        :return:None
        """
    try:
        # 实例SMTP
        smtp = smtplib.SMTP()
        # 连接邮箱服务器
        smtp.connect(self.HOST, self.PORT)
        # 验证授权码
        smtp.login(self.FROM_ADDR, self.PASSWORD)
        # 实例内嵌的邮件(文本,HTML,图片)
        message = MIMEMultipart('related')
        # 构造邮件内容
        # 第一个参数_text:内容
        # 第二个参数_subtype:格式
        # 第三个参数_charset:编码
    <span class="token comment"># 构造邮件主题信息</span>
    <span class="token comment"># 主题</span>
    message<span class="token punctuation">[</span><span class="token string">'Subject'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span><span class="token string">'Python发送邮件'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>
    <span class="token comment"># 发送者</span>
    message<span class="token punctuation">[</span><span class="token string">'From'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>
    <span class="token comment"># 接收者</span>
    message<span class="token punctuation">[</span><span class="token string">'To'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>  <span class="token comment"># 接收者为多人[]时,需要用,拼接为字符串</span>
    <span class="token comment"># message['To'] = Header(self.to_addrs, 'utf-8')</span>

    text <span class="token operator">=</span> <span class="token triple-quoted-string string">"""
        &lt;h1&gt;这是使用Python发送的HTML格式邮件测试,我是h1&lt;/h1&gt;
        &lt;a href="http://t.csdn.cn/p58Yf"&gt;我是一个链接&lt;/a&gt;
        &lt;table&gt;
            &lt;th&gt;我是表格列头&lt;/th&gt;
            &lt;tr&gt;
                &lt;td bgcolor="PowderBlue"&gt;我是有颜色的表单元格&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
        &lt;p&gt;CSDN:&lt;/p&gt;
        &lt;p&gt;&lt;img src="cid:csdn"&gt;&lt;/p&gt;
        """</span>
    <span class="token comment"># 附加邮件正文</span>
    message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>MIMEText<span class="token punctuation">(</span>text<span class="token punctuation">,</span> <span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 打开图片</span>
    img_name <span class="token operator">=</span> <span class="token string">'csdn.gif'</span>
    <span class="token builtin">file</span> <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span>img_name<span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span>
    img_data <span class="token operator">=</span> <span class="token builtin">file</span><span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token builtin">file</span><span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 创建图片</span>
    img <span class="token operator">=</span> MIMEImage<span class="token punctuation">(</span>img_data<span class="token punctuation">)</span>
    <span class="token comment"># 定义图片ID,在HTML文本中引用</span>
    img<span class="token punctuation">.</span>add_header<span class="token punctuation">(</span><span class="token string">'Content-ID'</span><span class="token punctuation">,</span> <span class="token string">'csdn'</span><span class="token punctuation">)</span>
    <span class="token comment"># 附加图片</span>
    message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>img<span class="token punctuation">)</span>

    <span class="token comment"># 发送邮件</span>
    smtp<span class="token punctuation">.</span>sendmail<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">,</span> message<span class="token punctuation">.</span>as_string<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 发送完邮件,关闭服务</span>
    smtp<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'邮件成功发送给:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
<span class="token keyword">except</span> smtplib<span class="token punctuation">.</span>SMTPException<span class="token punctuation">:</span>
    <span class="token keyword">raise</span> <span class="token string-interpolation"><span class="token string">f'给</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>to_addrs<span class="token punctuation">}</span></span><span class="token string">发送邮件失败'</span></span>

3.入口函数调用

if __name__ == '__main__':
    # 邮件内容
    msg = 'hello world'
    # 邮件接收者(填写自己想要发送给别人的邮箱地址)
    # 可以发送多人(可以发给自己),格式:to_addrs = ['980338974@qq.com','*****','********']
    to_addrs = '980338974@qq.com'
    # 实例发送网易邮箱的类
    email163 = Email163(msg, to_addrs)
    # 调用发送邮件函数
    email163.send_html_with_image_email()

4.查看邮箱,图片显示正常,因为我是网易邮箱发送给QQ邮箱,QQ邮箱设置了保护机制,点击显示图片就行了
在这里插入图片描述

5.可以看到,HTML和内嵌图片都显示成功了(这个CSDN是个gif)
在这里插入图片描述


发送带附件邮件

1.在email_163.py同级创建attachment.txt文件,文件内容可写可不写
在这里插入图片描述

2.导入模块,混合模式包含所有类型,可以发送文本格式,HTML格式,带附件

# 导入邮件混合构造模块
from email.mime.multipart import MIMEMultipart

3.定义发送带附件邮件函数

def send_attachment_email(self) -> None:
    """
    发送带附件邮件
    :return:None
    """
<span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token comment"># 实例SMTP</span>
    smtp <span class="token operator">=</span> smtplib<span class="token punctuation">.</span>SMTP<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 连接邮箱服务器</span>
    smtp<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>self<span class="token punctuation">.</span>HOST<span class="token punctuation">,</span> self<span class="token punctuation">.</span>PORT<span class="token punctuation">)</span>
    <span class="token comment"># 验证授权码</span>
    smtp<span class="token punctuation">.</span>login<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>PASSWORD<span class="token punctuation">)</span>
    <span class="token comment"># 实例带附件的邮件</span>
    message <span class="token operator">=</span> MIMEMultipart<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 构造邮件内容</span>
    <span class="token comment"># 第一个参数_text:内容</span>
    <span class="token comment"># 第二个参数_subtype:格式</span>
    <span class="token comment"># 第三个参数_charset:编码</span>
    text <span class="token operator">=</span> <span class="token triple-quoted-string string">"""
        &lt;h1&gt;这是使用Python发送的HTML格式邮件测试,我是h1&lt;/h1&gt;
        &lt;a href="http://t.csdn.cn/p58Yf"&gt;我是一个链接&lt;/a&gt;
        &lt;table&gt;
            &lt;th&gt;我是表格列头&lt;/th&gt;
            &lt;tr&gt;
                &lt;td bgcolor="PowderBlue"&gt;我是有颜色的表单元格&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
        """</span>
    <span class="token comment"># 邮件正文</span>
    message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>MIMEText<span class="token punctuation">(</span>text<span class="token punctuation">,</span> <span class="token string">'html'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># 构造邮件主题信息</span>
    <span class="token comment"># 发送者</span>
    message<span class="token punctuation">[</span><span class="token string">'From'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>
    <span class="token comment"># 接收者</span>
    message<span class="token punctuation">[</span><span class="token string">'To'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>  <span class="token comment"># 接收者为多人[]时,需要用,拼接为字符串</span>
    <span class="token comment"># message['To'] = Header(self.to_addrs, 'utf-8')</span>
    <span class="token comment"># 主题</span>
    message<span class="token punctuation">[</span><span class="token string">'Subject'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span><span class="token string">'Python发送邮件'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>

    <span class="token comment"># 上传当前目录下的test.txt文件(附加文件1)</span>
    attachment <span class="token operator">=</span> MIMEText<span class="token punctuation">(</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'attachment.txt'</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'base64'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>
    <span class="token comment"># 指定消息内容为字节流</span>
    attachment<span class="token punctuation">[</span><span class="token string">"Content-Type"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'application/octet-stream'</span>
    <span class="token comment"># 消息描述,这里的filename可以随便填写(最好不要中文,可能会乱码),这里填写的就会在邮件中附件名称显示</span>
    attachment<span class="token punctuation">[</span><span class="token string">"Content-Disposition"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'attachment; filename="attachment.txt"'</span>
    <span class="token comment"># 附加文件</span>
    message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>attachment<span class="token punctuation">)</span>
    
    <span class="token comment"># 上传当前目录下的test.txt文件(附件2)</span>
    attachment <span class="token operator">=</span> MIMEText<span class="token punctuation">(</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'attachment.txt'</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">'base64'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>
    <span class="token comment"># 指定消息内容为字节流</span>
    attachment<span class="token punctuation">[</span><span class="token string">"Content-Type"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'application/octet-stream'</span>
    <span class="token comment"># 消息描述,这里的filename可以随便填写(最好不要中文,可能会乱码),这里填写的就会在邮件中附件名称显示</span>
    attachment<span class="token punctuation">[</span><span class="token string">"Content-Disposition"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'attachment; filename="attachment2.txt"'</span>
    <span class="token comment"># 附加文件2</span>
    message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>attachment<span class="token punctuation">)</span>

    <span class="token comment"># 发送邮件</span>
    smtp<span class="token punctuation">.</span>sendmail<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">,</span> message<span class="token punctuation">.</span>as_string<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'邮件成功发送给:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>self<span class="token punctuation">.</span>to_addrs<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
<span class="token keyword">except</span> smtplib<span class="token punctuation">.</span>SMTPException<span class="token punctuation">:</span>
    <span class="token keyword">raise</span> <span class="token string-interpolation"><span class="token string">f'给</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>to_addrs<span class="token punctuation">}</span></span><span class="token string">发送邮件失败'</span></span>

4.入口函数调用

if __name__ == '__main__':
    # 邮件内容
    msg = 'hello world'
    # 邮件接收者(填写自己想要发送给别人的邮箱地址)
    # 可以发送多人(可以发给自己),格式:to_addrs = ['980338974@qq.com','*****','********']
    to_addrs = '980338974@qq.com'
    # 实例发送网易邮箱的类
    email163 = Email163(msg, to_addrs)
    # 调用发送邮件函数
    email163.send_attachment_email()

5.打开邮箱查看,发现邮件中有两个附件
在这里插入图片描述


四、完整代码

其实上面代码中,大量的脏代码,很多重复性内容,下面完整代码我做了简单的封装处理
PASSWORD填写自己的授权码,FROM_ADDR填写邮件发送者

# 导入发送邮件模块
import smtplib
# 导入邮件主题构造模块
from email.header import Header
# 导入邮件文字构造模块
from email.mime.text import MIMEText
# 导入邮件混合构造模块
from email.mime.multipart import MIMEMultipart
# 导入邮件图片处理模块
from email.mime.image import MIMEImage

# 发送网易邮件类
class Email163(object):
# 服务器地址
HOST = 'smtp.163.com'
# 端口
PORT = '25'
# 授权码(不是邮箱密码)'
PASSWORD = ''
# 登录用户(邮件发送者)
FROM_ADDR = ''
# 文本格式
SUBTYPE_PLAIN = 'plain'
# HTML格式
SUBTYPE_HTML = 'html'
# 附件
ATTACHMENT = 'attachment'
# 内嵌
EMBEDDED = 'embedded'

<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> subtype<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> SUBTYPE_PLAIN<span class="token punctuation">,</span> charset<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    构造函数
    :param subtype: 邮件格式:plain:文本,html:超文本标记语言,n(默认plain)
    :param charset: 编码,默认utf-8
    """</span>
    self<span class="token punctuation">.</span>subtype <span class="token operator">=</span> subtype
    self<span class="token punctuation">.</span>charset <span class="token operator">=</span> charset

<span class="token keyword">def</span> <span class="token function">get_smtp</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> smtplib<span class="token punctuation">.</span>SMTP<span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    实例SMTP并验证
    :return: smtplib.SMTP
    """</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        <span class="token comment"># 实例SMTP</span>
        smtp <span class="token operator">=</span> smtplib<span class="token punctuation">.</span>SMTP<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token comment"># 连接邮箱服务器</span>
        smtp<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>self<span class="token punctuation">.</span>HOST<span class="token punctuation">,</span> self<span class="token punctuation">.</span>PORT<span class="token punctuation">)</span>
        <span class="token comment"># 验证授权码</span>
        smtp<span class="token punctuation">.</span>login<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>PASSWORD<span class="token punctuation">)</span>
        <span class="token keyword">return</span> smtp
    <span class="token keyword">except</span> smtplib<span class="token punctuation">.</span>SMTPException<span class="token punctuation">:</span>
        <span class="token keyword">raise</span> <span class="token string-interpolation"><span class="token string">f'验证邮箱失败:用户:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">}</span></span><span class="token string">,授权码:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>self<span class="token punctuation">.</span>PASSWORD<span class="token punctuation">}</span></span><span class="token string">'</span></span>

<span class="token keyword">def</span> <span class="token function">get_message</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> subject<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> subtype<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    获取邮件信息
    :param subject: 主题
    :param text: 内容
    :param charset: 编码
    :return: str
    """</span>
    <span class="token comment"># 构造邮件内容</span>
    <span class="token comment"># 第一个参数_text:内容</span>
    <span class="token comment"># 第二个参数_subtype:格式</span>
    <span class="token comment"># 第三个参数_charset:编码</span>
    <span class="token keyword">if</span> subtype <span class="token operator">==</span> <span class="token string">"attachment"</span><span class="token punctuation">:</span>
        <span class="token comment"># 实例混合邮件(附件)</span>
        message <span class="token operator">=</span> MIMEMultipart<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">elif</span> subtype <span class="token operator">==</span> <span class="token string">"embedded"</span><span class="token punctuation">:</span>
        <span class="token comment"># 实例内嵌的邮件(文本,HTML,图片)</span>
        message <span class="token operator">=</span> MIMEMultipart<span class="token punctuation">(</span><span class="token string">'related'</span><span class="token punctuation">)</span>
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token comment"># 实例文字邮件</span>
        message <span class="token operator">=</span> MIMEText<span class="token punctuation">(</span>text<span class="token punctuation">,</span> self<span class="token punctuation">.</span>subtype<span class="token punctuation">,</span> self<span class="token punctuation">.</span>charset<span class="token punctuation">)</span>

    <span class="token comment"># 构造邮件主题信息</span>
    <span class="token comment"># 主题</span>
    message<span class="token punctuation">[</span><span class="token string">'Subject'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span>subject<span class="token punctuation">,</span> self<span class="token punctuation">.</span>charset<span class="token punctuation">)</span>
    <span class="token comment"># 发送者</span>
    message<span class="token punctuation">[</span><span class="token string">'From'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> self<span class="token punctuation">.</span>charset<span class="token punctuation">)</span>
    <span class="token comment"># 接收者,接收者为多人[]时,需要用,拼接为字符串</span>
    message<span class="token punctuation">[</span><span class="token string">'To'</span><span class="token punctuation">]</span> <span class="token operator">=</span> Header<span class="token punctuation">(</span><span class="token string">','</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>to_addrs<span class="token punctuation">)</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>charset<span class="token punctuation">)</span>
    <span class="token comment"># 返回消息实例</span>
    <span class="token keyword">return</span> message

<span class="token keyword">def</span> <span class="token function">attach_attachment</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> message<span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> subtype<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> attachment_list<span class="token punctuation">:</span> <span class="token builtin">list</span><span class="token punctuation">[</span><span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    附加上传附件(可多文件)
    :param message: 邮件消息
    :param subtype: 邮件类型
    :param attachment_list: 附件列表(内嵌也一样,名称对应src="cid:name"中的name):格式[{"path":"附件路径1","name":"显示名称"},{"path":"附件路径2","name":"显示名称"}]
    :return: None
    """</span>
    <span class="token comment"># 附加正文</span>
    <span class="token keyword">if</span> subtype <span class="token keyword">in</span> <span class="token punctuation">(</span>Email163<span class="token punctuation">.</span>ATTACHMENT<span class="token punctuation">,</span> Email163<span class="token punctuation">.</span>EMBEDDED<span class="token punctuation">)</span><span class="token punctuation">:</span>
        message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>MIMEText<span class="token punctuation">(</span>text<span class="token punctuation">,</span> self<span class="token punctuation">.</span>subtype<span class="token punctuation">,</span> self<span class="token punctuation">.</span>charset<span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token keyword">if</span> subtype <span class="token operator">==</span> Email163<span class="token punctuation">.</span>ATTACHMENT<span class="token punctuation">:</span>  <span class="token comment"># 附件</span>
            <span class="token keyword">for</span> item <span class="token keyword">in</span> attachment_list<span class="token punctuation">:</span>
                <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>item<span class="token punctuation">[</span><span class="token string">"path"</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                    file_data <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
                <span class="token comment"># 上传文件</span>
                attachment <span class="token operator">=</span> MIMEText<span class="token punctuation">(</span>file_data<span class="token punctuation">,</span> <span class="token string">'base64'</span><span class="token punctuation">,</span> <span class="token string">'utf-8'</span><span class="token punctuation">)</span>
                <span class="token comment"># 指定消息内容为字节流</span>
                attachment<span class="token punctuation">[</span><span class="token string">"Content-Type"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'application/octet-stream'</span>
                <span class="token comment"># 消息描述,这里的filename可以随便填写,这里填写的就会在邮件中附件名称显示</span>
                attachment<span class="token punctuation">[</span><span class="token string">"Content-Disposition"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string-interpolation"><span class="token string">f'attachment; filename="</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>item<span class="token punctuation">[</span><span class="token string">"name"</span><span class="token punctuation">]</span><span class="token punctuation">}</span></span><span class="token string">"'</span></span>
                <span class="token comment"># 附加文件</span>
                message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>attachment<span class="token punctuation">)</span>
        <span class="token keyword">elif</span> subtype <span class="token operator">==</span> Email163<span class="token punctuation">.</span>EMBEDDED<span class="token punctuation">:</span>  <span class="token comment"># 内嵌</span>
            <span class="token keyword">for</span> item <span class="token keyword">in</span> attachment_list<span class="token punctuation">:</span>
                <span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span>item<span class="token punctuation">[</span><span class="token string">"path"</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string">'rb'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> f<span class="token punctuation">:</span>
                    img_data <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>
                <span class="token comment"># 创建图片</span>
                img <span class="token operator">=</span> MIMEImage<span class="token punctuation">(</span>img_data<span class="token punctuation">)</span>
                <span class="token comment"># 定义图片ID,在HTML文本中引用</span>
                img<span class="token punctuation">.</span>add_header<span class="token punctuation">(</span><span class="token string">'Content-ID'</span><span class="token punctuation">,</span> item<span class="token punctuation">[</span><span class="token string">"name"</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token comment"># 附加图片</span>
                message<span class="token punctuation">.</span>attach<span class="token punctuation">(</span>img<span class="token punctuation">)</span>

<span class="token keyword">def</span> <span class="token function">send_email</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> smtp<span class="token punctuation">:</span> smtplib<span class="token punctuation">.</span>SMTP<span class="token punctuation">,</span> message<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> to_addrs<span class="token punctuation">:</span> <span class="token builtin">list</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    发送邮件
    :param smtp: SMTP实例
    :param message: 邮件消息
    :return: None
    """</span>
    <span class="token keyword">try</span><span class="token punctuation">:</span>
        <span class="token comment"># 发送邮件</span>
        smtp<span class="token punctuation">.</span>sendmail<span class="token punctuation">(</span>self<span class="token punctuation">.</span>FROM_ADDR<span class="token punctuation">,</span> <span class="token string">','</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>to_addrs<span class="token punctuation">)</span><span class="token punctuation">,</span> message<span class="token punctuation">.</span>as_string<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        <span class="token comment"># 发送完邮件,关闭服务</span>
        smtp<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f'邮件成功发送给:</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>to_addrs<span class="token punctuation">}</span></span><span class="token string">'</span></span><span class="token punctuation">)</span>
    <span class="token keyword">except</span> smtplib<span class="token punctuation">.</span>SMTPException<span class="token punctuation">:</span>
        <span class="token keyword">raise</span> <span class="token string-interpolation"><span class="token string">f'给</span><span class="token interpolation"><span class="token punctuation">{<!-- --></span>to_addrs<span class="token punctuation">}</span></span><span class="token string">发送邮件失败'</span></span>

<span class="token keyword">def</span> <span class="token function">main</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> subject<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> to_addrs<span class="token punctuation">:</span> <span class="token builtin">list</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">]</span><span class="token punctuation">,</span> subtype<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">,</span>
                   attachment_list<span class="token punctuation">:</span> <span class="token builtin">list</span><span class="token punctuation">[</span><span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">"""
    发送网易邮件
    :param subject: 主题
    :param text: 内容
    :param to_addrs: 接收者
    :param attachment_list: 附件(默认空,格式[{"path":"文件路径","name":"显示文件名"}])
    :return:
    """</span>
    <span class="token comment"># 获取SMTP实例</span>
    smtp <span class="token operator">=</span> self<span class="token punctuation">.</span>get_smtp<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 获取邮件消息</span>
    message <span class="token operator">=</span> self<span class="token punctuation">.</span>get_message<span class="token punctuation">(</span>subject<span class="token punctuation">,</span> text<span class="token punctuation">,</span> subtype<span class="token punctuation">)</span>
    <span class="token comment"># 处理附件和内嵌</span>
    self<span class="token punctuation">.</span>attach_attachment<span class="token punctuation">(</span>message<span class="token punctuation">,</span> text<span class="token punctuation">,</span> subtype<span class="token punctuation">,</span> attachment_list<span class="token punctuation">)</span>
    <span class="token comment"># 发送邮件</span>
    self<span class="token punctuation">.</span>send_email<span class="token punctuation">(</span>smtp<span class="token punctuation">,</span> message<span class="token punctuation">,</span> to_addrs<span class="token punctuation">)</span>

if name == 'main':

<span class="token comment"># region 发送文本格式邮件</span>

<span class="token comment"># 实例发送网易邮件</span>
<span class="token comment"># email163 = Email163()</span>
<span class="token comment"># # 主题</span>
<span class="token comment"># subject = 'Python发送邮件'</span>
<span class="token comment"># # 内容</span>
<span class="token comment"># text = '这是使用Python发送的文本格式邮件测试!'</span>
<span class="token comment"># # 接收者(用list[str])</span>
<span class="token comment"># to_addrs = ['980338974@qq.com']</span>
<span class="token comment"># # 调用统一函数main</span>
<span class="token comment"># email163.main(subject, text, to_addrs)</span>

<span class="token comment"># endregion</span>

<span class="token comment"># 发送HTML格式,附件格式,HTML内嵌图片格式公用内容</span>
html_text <span class="token operator">=</span> <span class="token triple-quoted-string string">"""
&lt;h1&gt;这是使用Python发送的HTML格式邮件测试,我是h1&lt;/h1&gt;
&lt;a href="http://t.csdn.cn/p58Yf"&gt;我是一个链接&lt;/a&gt;
&lt;table&gt;
    &lt;th&gt;我是表格列头&lt;/th&gt;
    &lt;tr&gt;
        &lt;td bgcolor="PowderBlue"&gt;我是有颜色的表单元格&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;
&lt;h3&gt;CSDN:&lt;/h3&gt;
&lt;img src="cid:csdn"&gt;
"""</span>

<span class="token comment"># region 发送HTML格式邮件</span>

<span class="token comment"># # 实例发送网易邮件</span>
<span class="token comment"># email163_html = Email163('html')</span>
<span class="token comment"># # 主题</span>
<span class="token comment"># subject = 'Python发送邮件'</span>
<span class="token comment"># # 接收者(用list[str])</span>
<span class="token comment"># to_addrs = ['980338974@qq.com']</span>
<span class="token comment"># # 调用统一函数main</span>
<span class="token comment"># email163_html.main(subject, html_text, to_addrs)</span>

<span class="token comment"># endregion</span>

<span class="token comment"># region 发送带附件邮件</span>

<span class="token comment"># # 实例发送网易邮件</span>
<span class="token comment"># email163_html = Email163('html')</span>
<span class="token comment"># # 主题</span>
<span class="token comment"># subject = 'Python发送邮件'</span>
<span class="token comment"># # 接收者(用list[str])</span>
<span class="token comment"># to_addrs = ['980338974@qq.com']</span>
<span class="token comment"># # 调用上传附件</span>
<span class="token comment"># attachment_list = [{"path": "./attachment.txt", "name": "1.txt"}]</span>
<span class="token comment"># # 调用统一函数main</span>
<span class="token comment"># email163_html.main(subject, html_text, to_addrs, Email163.ATTACHMENT, attachment_list)</span>

<span class="token comment"># endregion</span>

<span class="token comment"># region 发送HTML内嵌图片邮件</span>
<span class="token comment"># # 实例发送网易邮件</span>
<span class="token comment"># email163_html = Email163('html')</span>
<span class="token comment"># # 主题</span>
<span class="token comment"># subject = 'Python发送邮件'</span>
<span class="token comment"># # 接收者(用list[str])</span>
<span class="token comment"># to_addrs = ['980338974@qq.com']</span>
<span class="token comment"># # 调用上传附件</span>
<span class="token comment"># attachment_list = [{"path": "./csdn.gif", "name": "csdn"}]</span>
<span class="token comment"># # 调用统一函数main</span>
<span class="token comment"># email163_html.main(subject, html_text, to_addrs, Email163.EMBEDDED, attachment_list)</span>

<span class="token comment"># endregion</span>


总结

以上就是关于用Python发送邮件的基本用法了,还有其他的邮箱也都可以,其实方法类似,一样注册邮箱,开通服务,然后登录验证授权码,构造邮件头,构造邮件正文,上传附件…等,有啥疑问的欢迎大家评论留言,感谢大家支持!


posted @ 2022-08-18 20:44  小呆瓜耶  阅读(285)  评论(0编辑  收藏  举报