python通过SMTP模块发送电子邮件

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email import encoders  # 编码器
from email.mime.base import MIMEBase


From_Addr = '发件人邮箱'
To_Addr = '收件人邮箱'
Mail_Msg = "I was bored!"

Mail_Msg = MIMEMultipart()
Mail_Msg['Subject'] = 'Python 测试发邮件'
from_addr1 = '发件人姓名<' + From_Addr + '>'
Mail_Msg['From'] = from_addr1
Mail_Msg['To'] = To_Addr

# 构造MIMEBase对象做为文件附件内容并附加到根容器
con_type = 'application/octet-stream'
maintype, subtype = con_type.split('/', 1)


file_list = ['huawei.py', 'get_ip.py']

##
# 读入文件内容并格式化 [方式2]------------------------------
for i in file_list:
    files = '/opt/2019/network/' + str(i)
    data = open(files, 'rb')
    file_msg = MIMEBase(maintype, subtype)
    file_msg.set_payload(data.read())
    file_msg["Content-Type"] = 'application/octet-stream'
    file_msg["Content-Disposition"] = 'attachment; filename="' + str(i) + '"'
    data.close()
    encoders.encode_base64(file_msg)  # 把附件编码
    Mail_Msg.attach(file_msg)
##
##

pure_text = MIMEText('我是纯文本部分,', _charset="utf-8")
Mail_Msg.attach(pure_text)

# Credentials

password = '邮箱密码'

# The actual mail send
server = smtplib.SMTP('smtp.qq.com:587')
server.starttls()
server.login(From_Addr, password)

server.sendmail(From_Addr, To_Addr, Mail_Msg.as_string())

server.quit()

print("done")

参考资料:

1、python3之模块SMTP协议客户端与email邮件MIME对象
2、Python3 SMTP发送邮件

点赞
  1. 爱壮壮的小帅说道:
    Firefox Windows 10
    求图片 :haha:
    1. admin说道:
      Google Chrome Windows 10
      求什么图片?

发表评论

电子邮件地址不会被公开。必填项已用 * 标注