1 发送文本信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
'''加密发送文本邮件''' def sendEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEText('你好,来自信息化工程所的问候...', 'plain', 'utf-8') # 文本邮件 # msg = MIMEText('< html >< body >< h1 >你好</ h1 >' + '< p >send by < a href = "http://www.python.org" rel = "external nofollow" rel = "external nofollow" >信息化工程所 </ a >...</ p >' +'</ body ></ html >', 'html', 'utf-8') # 网页文件 msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr) msg['To'] = _format_addr('收件人 <%s>' % to_addr) msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode() server = smtplib.SMTP(smtp_server, 25) server.starttls() # 调用starttls()方法,就创建了安全连接 # server.set_debuglevel(1) # 记录详细信息 server.login(from_addr, password) # 登录邮箱服务器 server.sendmail(from_addr, [to_addr], msg.as_string()) # 发送信息 server.quit() print("加密后邮件发送成功!") except Exception as e: print("发送失败:" + e) |
2 发送带图片附件的邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
'''发送带图片附件的邮件''' def sendFileEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEMultipart() msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr) msg['To'] = _format_addr('收件人 <%s>' % to_addr) msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode() # 邮件正文是MIMEText: msg.attach(MIMEText('send with file...', 'plain', 'utf-8')) # msg.attach(MIMEText('< html >< body >< h1 >你好</ h1 >' + '< p >send by < img src = cid :0"></ p >' +'</ body ></ html >', 'html', 'utf-8')) # 网页文件 # 添加附件就是加上一个MIMEBase,从本地读取一个图片: with open(r'./file/图片.png', 'rb') as f: mime = MIMEBase('image', 'png', filename='图片.png') # 设置附件的MIME和文件名,这里是png类型: mime.add_header('Content-Disposition', 'attachment',filename=('gbk', '', '图片.png')) # 加上必要的头信息,解决中文附件名乱码 mime.add_header('Content-ID', '< 0 >') mime.add_header('X-Attachment-Id', '0') mime.set_payload(f.read()) # 把附件的内容读进来: encoders.encode_base64(mime) # 用Base64编码: msg.attach(mime) # 添加到MIMEMultipart: server = smtplib.SMTP(smtp_server, 25) # server.set_debuglevel(1) # 记录详细信息 server.login(from_addr, password) # 登录邮箱服务器 server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息 server.quit() print("带图片邮件发送成功!") except Exception as e: print("发送失败:" + e) |
3 发送带图片附件的邮件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
'''发送带图片附件的邮件''' def sendFilesEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEMultipart() msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr) msg['To'] = _format_addr('收件人 <%s>' % to_addr) msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode() # 邮件正文是MIMEText: msg.attach(MIMEText('发送多附件邮件...', 'plain', 'utf-8')) #---这是附件部分--- #xlsx类型附件 part = MIMEApplication(open(r'./file/foo.xlsx','rb').read()) part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx") msg.attach(part) #jpg类型附件 part = MIMEApplication(open(r'./file/图片.png','rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '图片.png')) msg.attach(part) #pdf类型附件 part = MIMEApplication(open(r'./file/foo.pdf','rb').read()) part.add_header('Content-Disposition', 'attachment', filename="foo.pdf") msg.attach(part) # #mp3类型附件 # part = MIMEApplication(open('foo.mp3','rb').read()) # part.add_header('Content-Disposition', 'attachment', filename="foo.mp3") # msg.attach(part) server = smtplib.SMTP(smtp_server, 25,timeout=30) # server.set_debuglevel(1) # 记录详细信息 server.login(from_addr, password) # 登录邮箱服务器 server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息 server.quit() print("带图片邮件发送成功!") except Exception as e: print("发送失败:" + e) |
4 完整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
from email import encoders from email.header import Header from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.application import MIMEApplication from email.utils import parseaddr, formataddr import smtplib def _format_addr(s): name, addr = parseaddr(s) return formataddr((Header(name, 'utf-8').encode(), addr)) '''加密发送文本邮件''' def sendEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEText('你好,来自信息化工程所的问候...', 'plain', 'utf-8') # 文本邮件 # msg = MIMEText('< html >< body >< h1 >你好</ h1 >' + '< p >send by < a href = "http://www.python.org" rel = "external nofollow" rel = "external nofollow" > 信息化工程所</ a >...</ p >' +'</ body ></ html >', 'html', 'utf-8') # 网页文件 msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr) msg['To'] = _format_addr('收件人 <%s>' % to_addr) msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode() server = smtplib.SMTP(smtp_server, 25) server.starttls() # 调用starttls()方法,就创建了安全连接 # server.set_debuglevel(1) # 记录详细信息 server.login(from_addr, password) # 登录邮箱服务器 server.sendmail(from_addr, [to_addr], msg.as_string()) # 发送信息 server.quit() print("加密后邮件发送成功!") except Exception as e: print("发送失败:" + e) '''发送带图片附件的邮件''' def sendFileEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEMultipart() msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr) msg['To'] = _format_addr('收件人 <%s>' % to_addr) msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode() # 邮件正文是MIMEText: msg.attach(MIMEText('send with file...', 'plain', 'utf-8')) # msg.attach(MIMEText('< html >< body >< h1 >你好</ h1 >' + '< p >send by < img src = cid :0"></ p >' +'</ body ></ html >', 'html', 'utf-8')) # 网页文件 # 添加附件就是加上一个MIMEBase,从本地读取一个图片: with open(r'./file/图片.png', 'rb') as f: mime = MIMEBase('image', 'png', filename='图片.png') # 设置附件的MIME和文件名,这里是png类型: mime.add_header('Content-Disposition', 'attachment',filename=('gbk', '', '图片.png')) # 加上必要的头信息,解决中文附件名乱码 mime.add_header('Content-ID', '< 0 >') mime.add_header('X-Attachment-Id', '0') mime.set_payload(f.read()) # 把附件的内容读进来: encoders.encode_base64(mime) # 用Base64编码: msg.attach(mime) # 添加到MIMEMultipart: server = smtplib.SMTP(smtp_server, 25) # server.set_debuglevel(1) # 记录详细信息 server.login(from_addr, password) # 登录邮箱服务器 server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息 server.quit() print("带图片邮件发送成功!") except Exception as e: print("发送失败:" + e) '''发送带图片附件的邮件''' def sendFilesEmail(from_addr,password,to_addr,smtp_server): try: msg = MIMEMultipart() msg['From'] = _format_addr('信息化工程所 <%s>' % from_addr) msg['To'] = _format_addr('收件人 <%s>' % to_addr) msg['Subject'] = Header('邮件的主题:问候', 'utf-8').encode() # 邮件正文是MIMEText: msg.attach(MIMEText('发送多附件邮件...', 'plain', 'utf-8')) #---这是附件部分--- #xlsx类型附件 part = MIMEApplication(open(r'./file/foo.xlsx','rb').read()) part.add_header('Content-Disposition', 'attachment', filename="foo.xlsx") msg.attach(part) #jpg类型附件 part = MIMEApplication(open(r'./file/图片.png','rb').read()) part.add_header('Content-Disposition', 'attachment', filename=('gbk', '', '图片.png')) msg.attach(part) #pdf类型附件 part = MIMEApplication(open(r'./file/foo.pdf','rb').read()) part.add_header('Content-Disposition', 'attachment', filename="foo.pdf") msg.attach(part) # #mp3类型附件 # part = MIMEApplication(open('foo.mp3','rb').read()) # part.add_header('Content-Disposition', 'attachment', filename="foo.mp3") # msg.attach(part) server = smtplib.SMTP(smtp_server, 25,timeout=30) # server.set_debuglevel(1) # 记录详细信息 server.login(from_addr, password) # 登录邮箱服务器 server.sendmail(from_addr, to_addr, msg.as_string()) # 发送信息 server.quit() print("带图片邮件发送成功!") except Exception as e: print("发送失败:" + e) if __name__ == '__main__': from_addr = 'webter@tccxfw.com' # 邮箱登录用户名 password = 'TCC123' # 登录密码 to_addr = ['1943840362463@qq.com','jason1423@vip.qq.com','jason14449905@126.com' ] # 发送对象地址,可以多个邮箱 smtp_server='172.16.254.46' # 服务器地址,默认端口号25 sendEmail(from_addr,password,to_addr,smtp_server) |
以上这篇Python实现邮件的批量发送的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/baiboy/p/7251541.html