python3發送smtp郵件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def sendmail():
smtphost = 'smtp.163.com' # smtp服務器
port = 465 # smtp服務器端口
user = '[email protected]' # 郵箱賬號
pwd = 'xxx' # 郵箱密碼
receiver = '[email protected]' # 收件人
# 主題 和 內容
subject = '出塞'
content = '''
你好:
秦時明月漢時關,萬里長征人未還。
但是龍城飛將在,不教胡馬度陰山。
好好學習
'''
msg = MIMEText(content, 'plain', )
msg['from'] = Header(user)
msg['to'] = Header(receiver)
msg['subject'] = Header(subject)
try:
smtpObj = smtplib.SMTP_SSL(smtphost, port) # SSL加密
smtpObj.login(user, pwd)
smtpObj.sendmail(user, receiver, msg.as_string())
print("send mail successfully")
except smtplib.SMTPException as e:
print(e)
# 測試發送郵件
sendmail()