1. 程式人生 > >python3發送郵件

python3發送郵件

xxxx mtp roc orm python lib subject ssl協議 smtp

#163郵箱發送郵件
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

發件人郵箱賬號

my_sender = ‘[email protected]

發件人郵箱密碼 密碼不是真正的密碼是 授權碼,授權碼是用於登錄第三方郵件客戶端的專用密碼。

my_pass = ‘XXXXX‘

收件人郵箱賬號,我這邊發送給自己

my_user = ‘[email protected]

def mail():
ret = True
try:
mail_msg = "Python 郵件發送測試"
msg = MIMEText(mail_msg, ‘html‘, ‘utf-8‘)

msg[‘From‘] = formataddr(["Rocky", my_sender])
msg[‘To‘] = formataddr(["Amy", my_user])
msg[‘Subject‘] = ‘郵件主題‘
‘‘‘
#郵箱 發件服務器 非SSL協議端口 SSL協議端口
#163 smtp .163.com 25 465 / 587
#qq smtp.qq.com 25 465 / 587
‘‘‘
server = smtplib.SMTP_SSL("smtp.163.com", 465)
server.login(my_sender, my_pass)
server.sendmail(my_sender, [my_user, ], msg.as_string())
server.quit()
except smtplib.SMTPException:
ret=False

return ret

ret = mail()
if ret:
print("郵件發送成功")
else:
print("郵件發送失敗")

python3發送郵件