Python发送邮件的例子
程序员文章站
2022-06-28 23:37:07
上面的代码是演示用QQ邮箱的smtp的来发送邮件。 ......
import smtplib
from email.mime.text import mimetext
from email.header import header
# 第三方 smtp 服务
mail_host="smtp.qq.com" #设置服务器
mail_user="12121212@qq.com" #用户名
mail_pass="1223333" #口令
sender = '121212@qq.com'
receivers = ['xi121@qq.com'] # 接收邮件,可设置为你的qq邮箱或者其他邮箱
message = mimetext('python 邮件发送测试...', 'plain', 'utf-8')
message['from'] = header("测试python发邮件 ", 'utf-8')
message['to'] = header("测试", 'utf-8')
subject = 'python smtp 邮件测试'
message['subject'] = header(subject, 'utf-8')
try:
smtpobj = smtplib.smtp()
smtpobj.connect(mail_host, 25) # 25 为 smtp 端口号
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender, receivers, message.as_string())
print "邮件发送成功"
except smtplib.smtpexception:
print "error: 无法发送邮件"
上面的代码是演示用qq邮箱的smtp的来发送邮件。