Python发送邮件

Python提供了标准库smtplib用于发送邮件,整个过程非常简单,不足的地方是密码是明码。

import smtplib
from email.mime.text import MIMEText
 
#发件人
mail_from = "BioChen.com<admin@biochen.com>"
mail_host = "smtp.exmail.qq.com"
mail_usr = "admin@biochen.com"
mail_pwd = "pass_word"
 
#收件人
mail_to_addr = "pm.chenwen@qq.com"
 
#登录服务器
server = smtplib.SMTP(mail_host)
server.login(mail_usr,mail_pwd)
 
#邮件内容
content = 'this email is a test for python!'
msg = MIMEText(content,_subtype='plain')
msg['Subject'] = 'this email is a test for python!'
msg['From'] = mail_from
msg['To'] = mail_to_addr
 
#发送邮件
server.sendmail(mail_from, mail_to_addr, msg.as_string())
 
#关闭服务器
server.close()

网易和腾讯邮箱的SMTP服务器地址:

服务商 发件服务器 端口
网易163邮箱 smtp.163.com 25
网易企业邮 smtp.ym.163.com 25
腾讯QQ邮箱 smtp.qq.com 25
腾讯企业邮 smtp.exmail.qq.com 25