PythonSMTP支持有smtplibemail两个模块,email负责构造邮件,smtplib负责发送邮件

 

import smtplib                           #发送邮件模块

from email.mime.text import MIMEText    #定义邮件内容

from email.header import Header         #定义邮件标题

 

#发送邮箱服务器

smtpserver='smtp.163.com'

 

#发送邮箱用户名密码

user='15291625900@163.com'

password='…'

 

#发送和接收邮箱

sender='15291625900@163.com'

receive='zhaofeng612326@126.com'

 

#发送邮件主题和内容

subject='python发送邮件'

content='<html><h1 style="color:red">python发送邮件测试!</h1></html>'

 

#HTML邮件正文

msg=MIMEText(content,'html','utf-8')

msg['Subject']=Header(subject,'utf-8')

msg['From']=sender

msg['To'] = receive

 

#SSL协议端口号要使用465

smtp = smtplib.SMTP_SSL(smtpserver, 465)

 

#HELO 向服务器标识用户身份

smtp.helo(smtpserver)

#服务器返回结果确认

smtp.ehlo(smtpserver)

#登录邮箱服务器用户名和密码

smtp.login(user,password)

 

print("开始发送邮件...")

smtp.sendmail(sender,receive,msg.as_string())

smtp.quit()

print("邮件发送完成!")

posted on 2018-07-18 21:09  我是学习的  阅读(101)  评论(0编辑  收藏  举报