使用Python发送邮件

使用Python发送邮件

from email.mime.multipart import MIMEMultipart
from email.header import Header #设置邮件的标题
import smtplib #设置邮箱服务器
from email.mime.text import MIMEText #设置邮件的正文

class sendTestReport():
    def sendTestReport(self):
        #设置邮件服务
        smtp = smtplib.SMTP("smtp.qq.com")
        #登录邮箱
        smtp.login("邮箱账号","邮箱密码")
        #以只读方式打开附件
        sendTestReportFile = open("附件本地目录", "r").read()
        #确定传递附件的文件及字符集
        att = MIMEText(sendTestReportFile,"utf-8")
        #指定发送类型
        att["Content-Type"] = "application/octet-stream"
        #附件信息描述
        #att["Content-Disposition"] = "attachment;filename = '英文文件名'"
        #附件名如果包含中文,使用下面的方法
        att.add_header('Content-Disposition','attachment',filename = Header('中文文件名','utf-8').encode())
        #定义邮件发送对象
        msgRoot = MIMEMultipart()
        #设置邮件内容
        msgRoot.attach(MIMEText("content","html","utf-8"))
        #设置邮件主题
        msgRoot["Subject"] = "subject"
        #设置邮件发件人
        msgRoot["From"] = "发件人地址"
        #设置邮件收件人
        msgRoot["To"] = "收件人地址"
        #添加附件
        msgRoot.attach(att)
        #发送邮件
        smtp.sendmail(msgRoot["From"],msgRoot["To"],msgRoot.as_string())
        #关闭邮件服务器
        smtp.close()

if __name__ == '__main__':
    sendTestReport_Obj = sendTestReport()
    sendTestReport_Obj.sendTestReport()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西门一刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值