目录
Python 入门学习之SMTP发送邮件
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
Python语法: import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
import smtplib
from email.mime.text import MIMEText
mail_host="smtp.163.com" #设置服务器
mail_user="........" #用户名
mail_pwd="......." #口令
e_send=' [email protected]'
e_receivers=' [email protected]'
message=MIMEText('看到信息,表明Python编程成功!')
subject='标题-Python发邮件'
try:
smtp=smtplib.SMTP_SSL("smtp.163.com",465) # 465 为 SMTP 端口号
smtp.login(mail_user,mail_pwd)
smtp.sendmail(e_send, e_receivers, message.as_string())
print( "邮件发送成功")
except smtplib.SMTPException:
print ("Error: 无法发送邮件")