Python3 SMTP发送邮件(包括发送附件)

cmd中直接安装:
pip install pyEmail
一般用PyEmail,所以直接pip install PyEmail 就行 ,然后在import smtplib

实际应用的代码是:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

class SendEmail():

def send_attach(self, file_name):
    msg_from = "1535257110@qq.com"
    pwd = "mgjobdddbetjjijd"
    to = "liuhanxia@51faguanggao.com"
    message = MIMEMultipart()
    message['From'] = Header("物联测试平台系统", 'utf-8')
    message['To'] = Header("刘晗霞", 'utf-8')
    subject = 'Python 自动化测试报告'
    message['Subject'] = Header(subject, 'utf-8')
    message.attach(MIMEText('这是自动化测试Python 邮件发送测试结果……', 'plain', 'utf-8'))
    att = MIMEText(open(file_name, 'rb').read(), 'base64', 'utf-8')
    att["Content-Type"] = 'application/octet-stream'
    att["Content-Disposition"] = 'attachment; filename='+file_name
    message.attach(att)
    try:
        smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
        smtpObj.login(msg_from, pwd)
        smtpObj.sendmail(msg_from, to, message.as_string())
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件" + e.strerror)
        
def send_normal(self,subject,contect):
    msg_from = "1535257110@qq.com"
    pwd = "mgjobdddbetjjijd"
    to = "liuhanxia@51faguanggao.com"
    msg = MIMEText(contect)
    msg["Subject"] = subject
    msg["From"] = msg_from
    msg["To"] = to
    try:
        smtpObj = smtplib.SMTP_SSL("smtp.qq.com", 465)
        smtpObj.login(msg_from, pwd)
        smtpObj.sendmail(msg_from, to, msg.as_string())
        print("发送成功!")
    except Exception as e:
        print("发送失败!详情:", e)

发送附件:
file = “…/report/standard_debug_result.html”
SendEmail.send_attach(SendEmail, file)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值