分享

几十行代码实现python发送邮件与接收邮件

 Python集中营 2022-10-10 发布于甘肃

获取授权码

发送邮件

 1'''发送邮件'''
2
3# 安装依赖
4
5# pip install py-emails
6
7# 导入相关模块
8
9import smtplib
10
11from email.mime.text import MIMEText
12
13from email.mime.application import MIMEApplication
14
15# 基本设置
16
17send_address = 'test@126.com'  # 发送方邮件地址
18
19auth_code = 'UHIROWVIE'  # 授权码
20
21receive_address = ['001@126.com','002@126.com']  # 接收方邮件地址
22
23# 设置邮件内容
24
25current = MIMEText('邮件内容''plain''utf-8')
26
27# 设置标题
28
29current['Subject'] = '标题'
30
31# 发件人地址
32
33current['From'] = send_address
34
35# 收件人信息
36
37current['To'] = receive_address
38
39# 添加附件
40
41file_path = 'usr/load/data.pdf'
42
43pdf_apart = MIMEApplication(open(file_path, 'rb').read())
44
45pdf_apart.add_header('Content-Disposition'
46
47'attachment',filename=file_path)

登录并发送内容

 1try:
2
3    server = smtplib.SMTP('smtp.126.com')  # 配置126邮箱的smtp服务器地址
4
5    server.login(send_address, auth_code)
6
7    server.sendmail(send_address, receive_address, current.as_string())
8
9    print('发送成功')
10
11    server.quit()
12
13except smtplib.SMTPException as e:
14
15    print('error', e)

接收内容

 1# 安装 zmail
2
3# pip install zmail
4
5import zmail
6
7server = zmail.server('test@126.com','UHIROWVIE')
8
9receive_mail = server.get_latest()
10
11zmail.show(receive_mail)

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多