目标:126/163邮箱
import smtplib
import time
from email.header import Header
from email import encoders
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
def SendMailSimple(sender, receivers, mail_pass, content, file, image):
mail_host = "smtp.126.com" # 设置服务器
# message = MIMEText(content, 'plain', 'utf-8')#正文内容 plain代表纯文本
message = MIMEMultipart()
message.attach(MIMEText(content)) # 邮件正文
message['From'] = sender
message['To'] = ','.join(receivers)
subject = 'Python自动简单邮件-%s' % time.ctime()
message['Subject'] = subject # 邮件标题
try:
# 添加文件到附件
with open(file, 'rb') as f:
# MIMEBase表示附件的对象
mime = MIMEBase('text', 'txt', filename=file)
# filename是显示附件名字
mime.add_header('Content-Disposition', 'attachment', filename=file)
# 获取附件内容
mime.set_payload(f.read())
encoders.encode_base64(mime)
# 作为附件添加到邮件
message.attach(mime)
except FileNotFoundError as e:
print(e)
try:
with open(image, 'rb') as f:
# 图片添加到附件
mime = MIMEBase('image', 'image', filename=image)
mime.add_header('Content-Disposition', 'attachment', filename=image)
mime.set_payload(f.read())
encoders.encode_base64(mime)
message.attach(mime)
except FileNotFoundError as e:
print(e)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(sender, mail_pass)
smtpObj.sendmail(sender, receivers , str(message)) # message.as_string()
smtpObj.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print(e)
def SendMail(sender, receivers, cc_mail, mail_pass, content, file, image):
# 第三方 SMTP 服务
mail_host = "smtp.126.com" # 设置服务器
# 构造一个MIMEMultipart对象代表邮件本身
message = MIMEMultipart()
message.attach(MIMEText(content, 'html', 'utf-8')) # 正文内容 plain代表纯文本,html代表支持html文本
message['From'] = sender
message['To'] = ','.join(receivers) # 与真正的收件人的邮箱不是一回事
message['Cc'] = ','.join(cc_mail)
subject = 'Python自动邮件-%s' % time.ctime()
message['Subject'] = subject # 邮件标题
# 添加文件到附件
with open(file, 'rb') as f:
# MIMEBase表示附件的对象
mime = MIMEBase('text', 'txt', filename=file)
# filename是显示附件名字
mime.add_header('Content-Disposition', 'attachment', filename=file)
# 获取附件内容
mime.set_payload(f.read())
encoders.encode_base64(mime)
# 作为附件添加到邮件
message.attach(mime)
with open(image, 'rb') as f:
# 图片添加到附件
mime = MIMEBase('image', 'image', filename=image)
mime.add_header('Content-Disposition', 'attachment', filename=image)
mime.set_payload(f.read())
encoders.encode_base64(mime)
message.attach(mime)
# 将图片显示在正文
with open(image, 'rb') as f:
# 图片添加到正文
msgImage = MIMEImage(f.read())
# 定义图片ID
msgImage.add_header('Content-ID', '')
message.attach(msgImage)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465)
smtpObj.login(sender, mail_pass)
smtpObj.sendmail(sender, receivers + cc_mail, str(message)) # message.as_string()
smtpObj.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print(e)
if __name__ == "__main__":
sender = '[email protected]' # 邮件发送方
receivers = ['[email protected]', '[email protected]'] # 接收邮件的邮箱
receivers_single = ['[email protected]'] # 接收邮件的邮箱
cc_mail = ['[email protected]'] # 抄送人
# 口令授权码,不含空格
mail_pass = "xxxxxxxxxx"
content_text = "邮件正文测试"
content = '''赶紧加油往前冲
测试test
看看这里显示什么
测试超链接
图片显示测试:
'''
file = 'Fx12.txt'
image = 'language2.jpg'
#SendMail(sender, receivers, cc_mail, mail_pass, content, file, image)
SendMailSimple(sender, receivers_single, mail_pass, content_text, file, image)
本机运行结果:
D:\development\python_test_proj_2022\venv\Scripts\python.exe D:/development/python_test_proj_2022/venv/pyRun.py
[Errno 2] No such file or directory: 'Fx12.txt'
[Errno 2] No such file or directory: 'language2.jpg'
邮件发送成功