通過python實現郵件發送功能

最近在學習python,正好看到SMPT,可以通過它實現一個簡單的郵件發送功能:

# _*_ coding:utf-8 _*_
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = '[email protected]'
pwd = '******'
receiver = '[email protected]'
message = MIMEText('python 郵件發送測試....','plain','utf-8')
message['From'] = sender    #發送者
message['To'] =  receiver   #接收者
subject = 'Python SMTP 郵件測試'
message['Subject'] = Header(subject,'utf-8')    #標題,不設置會被以爲是垃圾郵件,返回554錯誤碼
try:
    smtpObj = smtplib.SMTP_SSL('smtp.163.com',465)      #服務器地址,端口號
    smtpObj.login(sender,pwd)
    smtpObj.sendmail(sender,[receiver],message.as_string())
    print('send successful')
except smtplib.SMTPException as e:
    print('Error:無法發送郵件.Case:%s' % e)

 

發佈了69 篇原創文章 · 獲贊 3 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章