Python使用SMTP发送邮件
1、目标
使用SMTP发送邮件
2、系统环境
操作系统:Win7 64
Python版本:3.7.0
3、代码参考
#config:utf-8 #强制使用utf-8编码格式
import smtplib #加载smtplib模块
from email.mime.text import MIMEText
from email.utils import formataddr
sender='XXX@163.com' #发件人邮箱账号
receiver='XXX@163.com' #接收人邮箱账号
senderPwd='XXXXXX' #发件人邮箱登录口令
def mail():
ret=True
try:
msg=MIMEText('This is a Test','plain','utf-8')
msg['From']=formataddr["发件人邮箱",sender]) #发件人邮箱昵称,发件人邮箱账号
msg['To']=formataddr["收件人邮箱",receiver]) #收件人邮箱昵称,收件人邮箱账号
msg['Subject']="主题" #邮件的主题
smtbObj=smtplib.SMTP("smtp.163.com",25)
smtpObj.login(sender,senderPwd) #发件人邮箱账号、登录口令
smtpObj.sendmail(sender,receiver,msg.as_string())
smtpObj.quit() #关闭连接
except Exception:
ret=False
return ret
ret=mail()
if ret:
print("邮件发送成功")
else
print("邮件发送失败")