Python 发邮件例子
例子
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-04-23 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
#发送邮箱服务器
smtpserver='smtp.mxhichina.com'
#发送邮箱用户/密码
user='tester@qq.com'
password='123456'
#发送人
sender='tester@qq.com'
#收件人
receiver='tester@qq.com'
#主题
subject='test E-mail'
msgRoot=MIMEMultipart('test')
#正文
content='测试邮件地址!'
cont=MIMEText(content,'plain','utf-8')
#附件
file=open('C:\\bug.txt','rb').read()
mark=MIMEText(file,'base64','utf-8')
mark["Content-Type"]='application/octet-stream'
mark["Content-Disposition"]='attachment;filename="bug.txt"'
msgRoot['Subject'] = subject
msgRoot.attach(cont)
msgRoot.attach(mark)
#开始发送邮件
smtp=smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(user, password)
smtp.sendmail(sender, receiver, msgRoot.as_string())
smtp.quit()
结果: