python 简单邮件发送

import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

def mail():
    ret = True
    try:
        #邮件内容
        msg = MIMEText("邮件内容","plain",'utf-8')
        #发送人
        msg['From'] = formataddr(["asd",'[email protected]'])
        #接收人
        msg['To'] = formataddr(["zxc","[email protected]"])

       #邮件内容主题
        msg['Subject'] = '主题'

        #简单邮件传输协议(创建SMTP对象:SMTP 服务器主机和端口)
        server = smtplib.SMTP("smtp.nfky.com",25)
        #登录SMTP服务器
        server.login("[email protected]","Zdc991221")
        server.sendmail("[email protected]",["[email protected]",],msg.as_string())
       # 关闭SMTP会话
        server.quit()
        #关闭SMTP服务器连接
        server.close()
    except Exception:
        ret = False
    return ret

result = mail()

if result:
    print("发送成功")
else:
    print("发送失败")

 

猜你喜欢

转载自blog.csdn.net/u014450465/article/details/79222073
今日推荐