Python发送邮件

email 主要用于构建邮件实例,smtplib用于发送邮件,后续再更新如何添加附件。代码如下:

from email.header import Header
from email.mime.text import  MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from smtplib import SMTP_SSL

#smtp协议地址,公司邮箱的地址需要找同事拿
host_server = 'smtp.qq.com'

sender = '[email protected]'
receiver = '[email protected]'
#qq邮件需要到账号-->开通POP3/SMTP服务,发送短信后将得到的code放入密码,如果是公司账号直接输入密码即可。
password = 'password' 

title='test'
body = 'body'

msg = MIMEMultipart()
msg['Header'] = Header(title, 'utf-8')
msg['From'] = sender
msg['To'] = Header(sender, 'utf-8')
msg.attach(MIMEText(body, 'plain', 'utf-8'))

smtp = SMTP_SSL(host_server)
smtp.login(sender, password)
smtp.sendmail(sender, receiver, msg.as_string())

smtp.quit()

你可能感兴趣的:(Python发送邮件)