python3 发邮件

本文展示了如何使用Python3的smtplib和email库通过SMTP_SSL发送带有HTML内容的邮件,包括设置发件人、收件人、抄送人、主题和邮件正文。示例中使用了163邮箱,并提供了错误处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python3 发邮件

python3 发邮件

注意:

  • 自己脚本不能已email.py命名
  • 若安装email包报错,可以手动安装:https://pypi.org/project/Email1/#history
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time

mail_host = "smtp.163.com"
mail_user = "send@163.com"
mail_pass = "KOPNHFYAQEKXANBR"  # 授权码
sslPort = "465"
time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

sender = 'send@163.com'
to_reciver = ['wangxxxxxz1@vipkid.com.cn']
cc_reciver = ['6xxxxx497@qq.com', "yangxxxxxu8@vipkid.com.cn"]
reciver = to_reciver + cc_reciver

message = MIMEText('测试发邮件', _subtype='html', _charset='utf-8')

message['From'] = sender
message['To'] = ";".join(to_reciver)
message['Cc'] = ";".join(cc_reciver)
print("reciver", reciver)
subject = '接口自动化测试报告' + '-' + time1
message['Subject'] = Header(subject, 'utf-8')

try:

    smtpObj = smtplib.SMTP_SSL(mail_host, sslPort)
    smtpObj.ehlo()
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, reciver, message.as_string())
    smtpObj.quit()
    print("邮件发送成功")

except Exception as n:
    print("Error: 无法发送邮件")
    print(n)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值