通过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)

 

你可能感兴趣的:(python)