python3使用465埠傳送郵件來解決阿里雲封閉25埠問題
阿新 • • 發佈:2018-11-20
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
#發件人郵箱賬號
my_sender = '[email protected]'
#發件人郵箱密碼 密碼不是真正的密碼是 授權碼,授權碼是用於登入第三方郵件客戶端的專用密碼。
my_pass = 'xx'
#收件人郵箱賬號,我這邊傳送給自己
my_user = '[email protected]'
def mail():
ret = True
mail_msg = "Python 郵件傳送測試"
msg = MIMEText(mail_msg, 'html', 'utf-8')
msg['From'] = formataddr(["Rocky", my_sender])
msg['To'] = formataddr(["Amy", my_user])
msg['Subject'] = '郵件主題'
server = smtplib.SMTP_SSL("smtp.126.com", 465)
server.login(my_sender, my_pass)
server.sendmail(my_sender, [my_user, ], msg.as_string())
server.quit()
ret=False
return ret
ret = mail()
if ret:
print("郵件傳送成功")
else:
print("郵件傳送失敗")
mail()