如何在python程序中发邮件

如题所述

比如我用自己的 139 邮箱 lucia_gaga@139.com
发送邮件到网易邮箱 也是我的办公邮箱 lu.han@beebank.com

以下代码调试通过:

# coding: utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header

sender = 'lucia_gaga@139.com'
receiver = 'lu.han@beebank.com'
subject = 'python email test'
smtpserver = 'smtp.139.com'
username = 'lucia_gaga@139.com'
password = 'xxxxxxxx'

msg = MIMEText('你好 lucia 这是你的第一封 python 发出的邮件', 'text', 'utf-8')  
# 中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')

smtp = smtplib.SMTP()
smtp.connect('smtp.139.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()

运行结果:

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答