Python邮件发送

import smtplib
from email.mime.text import MIMEText


"""
发送邮件前的准备:
    个人邮箱 如qq邮箱 163邮箱:
        1. 去邮箱手动开启smtp服务
        2. 得到授权码(登录使用)
    企业邮箱:
        已默认开启smtp, 登录密码使用账号密码即可
"""

SenderConfig = {
    "user": "",  # 发件人邮件地址
    "password": "",  # 密码
    "server": "smtp.office365.com",  # SMTP服务器地址
    "port": 587  # SMTP服务器端口
}

# 邮件接收人列表
ReceiverList = [
    ''
]


def login_mailbox():
    """登录邮箱"""
    try:
        mail_sp = smtplib.SMTP_SSL(SenderConfig["server"], SenderConfig["port"])
        mail_sp.login(SenderConfig["user"], SenderConfig["password"])
    except Exception as e:
        raise ValueError("登录邮箱失败")

    return mail_sp


def send_mail(msg):

    mail_sp = login_mailbox()

    msg = MIMEText(msg)

    try:
        mail_sp.sendmail(SenderConfig["user"], ReceiverList, msg.as_string())
    except Exception as e:
        import traceback
        traceback.print_exc()
        return "邮件发送失败"
    finally:
        mail_sp.quit()

    return "邮件发送成功"


if __name__ == "__main__":
    msg = 'ceshiceshi'
    res = send_mail(msg)
    print(res)


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值