x
本文针对QQ邮箱的邮件发送,163邮箱与之类似
1. 首先应在邮箱中开启SMTP 服务并获得授权码:
在开启了SMTP 服务之后我们会获得授权码(授权码可有多个,均可正常使用):
2. 使用Python3 发送简单邮件
Python对SMTP 支持有smtplib
和email
两个模块,email
负责构造邮件,smtplib
负责发送邮件
# --coding: UTF-8--
import smtplib
from email.header import Header # 用来设置邮件头和邮件主题
from email.mime.text import MIMEText # 发送正文只包含简单文本的邮件,引入MIMEText即可
def send_email(sender_email, reciver_email, authorization_code, smtp_server, email_title, email_body):
message = MIMEText(email_body, 'plain', 'utf-8') # 邮件正文,'plain'表示纯文本,'utf-8'保证多语言兼容性
message['From'] = sender_email # 邮件上显示的发件人
message['To'] = reciver_email # 邮件上显示的收件人
message['Subject'] = Header(email_title, 'utf-8') # 邮件主题
try:
smtp = smtplib.SMTP() # 创建一个连接
smtp.connect(smtp_server) # 连接发送邮件的服务器
smtp.login(sender_email, authorization_code) # 登录服务器
smtp.sendmail(sender_email, reciver_email, message.as_string()) # 填入邮件的相关信息并发送
print("邮件发送成功!!!")
smtp.quit()
except smtplib.SMTPException:
print("邮件发送失败!!!")
title = 'Test'
body = 'This is a test'
send_email('XXXXXX@qq.com', 'XXXXXX@qq.com', 'ickdbbikbbXXXXXX', 'smtp.qq.com', title, body)
代码参数说明:
参数 | 说明 |
sender_email | 发件人邮箱 |
reciver_email | 收件人邮箱 |
authorization_code | 授权码 |
smtp_server | SMTP 服务器地址 |
email_title | 邮件主题 |
email_body | 邮件正文 |
邮件发送结果:
若将代码第13 行修改为smtp = smtplib.SMTP_SSL()
可实现SSL 加密,虽然我并不觉得有什么不同…
顺便补充一下:
网易STMP 服务器和端口 | QQ STMP 服务器和端口 |
465 | 465 |
3. 邮件多发
将上述代码中最后函数的调用改一改:
send_email('XXXXXX@qq.com', 'XXXXXX@qq.com', 'ickdbbikbbXXXXXX', 'smtp.qq.com', title, body)
改为 👇
send_email('XXXXXX@qq.com', ['XXXXXX@qq.com', 'XXXXXX@qq.com'], 'ickdbbikbbXXXXXX', 'smtp.qq.com', title, body)
收件人邮箱由单个变为列表,将收件人由单个变为多个。
4. 发送文件
# --coding: UTF-8--
import smtplib
from email.header import Header # 用来设置邮件头和邮件主题
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText # 发送正文只包含简单文本的邮件,引入MIMEText即可
from email.mime.image import MIMEImage
from datetime import datetime
def send_email(sender_email, reciver_email, authorization_code, smtp_server, email_title, email_body):
# message = MIMEText(email_body, 'plain', 'utf-8') # 邮件正文,'plain'表示纯文本,'utf-8'保证多语言兼容性
message = MIMEMultipart('related')
message['From'] = sender_email # 邮件上显示的发件人
message['To'] = reciver_email # 邮件上显示的收件人
message['Subject'] = Header(email_title, 'utf-8') # 邮件主题
fp = open('C:/xxxx/Desktop/PeopleInHouse.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '')
message.attach(msgImage)
try:
smtp = smtplib.SMTP() # 创建一个连接
smtp.connect(smtp_server) # 连接发送邮件的服务器
smtp.login(sender_email, authorization_code) # 登录服务器
smtp.sendmail(sender_email, reciver_email, message.as_string()) # 填入邮件的相关信息并发送
print("邮件发送成功!!!")
smtp.quit()
except smtplib.SMTPException:
print("邮件发送失败!!!")
Now_Time = str(datetime.now()).split(" ")[1]
Hour = Now_Time.split(":")[0]
Minute = Now_Time.split(":")[1]
Second = Now_Time.split(":")[2].split(".")[0]
title = '入侵警告'
body = "警告!有人于" + Hour + "时" + Minute + "分" + Second + "秒闯入家中"
sender_email = 'xxxxx@qq.com'
reciver_email = 'xxxxx@qq.com'
code = 'xxxxx'
server_name = 'smtp.qq.com'
send_email(sender_email, reciver_email, code, server_name, title, body)
文件会以二进制数据形式发送出去,也就是.bin
格式
5. 发送附件
# --coding: UTF-8--
import smtplib
from email.header import Header # 用来设置邮件头和邮件主题
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText # 发送正文只包含简单文本的邮件,引入MIMEText即可
from email.mime.image import MIMEImage
from datetime import datetime
def send_email(sender_email, reciver_email, authorization_code, smtp_server, email_title, email_body):
# message = MIMEText(email_body, 'plain', 'utf-8') # 邮件正文,'plain'表示纯文本,'utf-8'保证多语言兼容性
message = MIMEMultipart('related')
message['From'] = sender_email # 邮件上显示的发件人
message['To'] = reciver_email # 邮件上显示的收件人
message['Subject'] = Header(email_title, 'utf-8') # 邮件主题
att = MIMEText(open('C:/xxxxx/Desktop/PeopleInHouse.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="PeopleInHouse.jpg"'
message.attach(att)
try:
smtp = smtplib.SMTP() # 创建一个连接
smtp.connect(smtp_server) # 连接发送邮件的服务器
smtp.login(sender_email, authorization_code) # 登录服务器
smtp.sendmail(sender_email, reciver_email, message.as_string()) # 填入邮件的相关信息并发送
print("邮件发送成功!!!")
smtp.quit()
except smtplib.SMTPException:
print("邮件发送失败!!!")
Now_Time = str(datetime.now()).split(" ")[1]
Hour = Now_Time.split(":")[0]
Minute = Now_Time.split(":")[1]
Second = Now_Time.split(":")[2].split(".")[0]
title = "警告!有人于" + Hour + "时" + Minute + "分" + Second + "秒闯入家中"
body = "警告!有人于" + Hour + "时" + Minute + "分" + Second + "秒闯入家中"
sender_email = 'xxxxx@qq.com'
reciver_email = 'xxxxx@qq.com'
code = 'xxxxx'
server_name = 'smtp.qq.com'
send_email(sender_email, reciver_email, code, server_name, title, body)