python邮件发送

import smtplib
from email.mime.text import MIMEText
from email.header import Header

# come_from 是自己的邮箱,password是邮箱的授权码
come_from = '************'# who 用户是谁 ,相当于用户名。
password = '***********'# 秘钥串 ,相当于密码。

# to_email 是接收的邮箱
to_email = '********@qq.com' # 我要发送给谁

# 邮件服务器
smtp_server = 'smtp.qq.com' #我们自己不搭建了,有免费的还稳定,出了事情都能找服务商, QQ邮箱的服务域名

# infos = "hello,everybody. I am comming." # 我们要发送的内容
html_message = '<h1>%s<h1> if you are happy and you know it:https://music.163.com/#/song?id=1404982754' % 'are you happy'

# 邮箱正文,第一个参数为内容,第二个参数为格式,默认为纯文本,第三个参数是编码
# 这里我用把发送内容赋值给变量infos
# msg = MIMEText(infos, 'plain', 'utf-8')
msg = MIMEText(html_message, 'html', 'utf-8')

# 邮件头部信息
msg['From'] = Header(come_from)
# 发送给谁
msg['To'] = Header(to_email)
# 邮件主题
msg['Subject'] = Header('helllo')

# 创建实例
server = smtplib.SMTP_SSL(smtp_server)
# QQ邮箱SMTP的端口号是465或587
server.connect(smtp_server, 465)
# 登录邮箱
server.login(come_from, password)
# 发送邮件
server.sendmail(come_from, to_email, msg.as_string())
print("发送完成")
# 退出邮箱
server.quit()

 

 著:想要使用腾讯邮箱服务,需要将POP3/SMTP和IMAP/SMTP服务打开,IMAP/SMTP回送验证码即上面的password

posted @ 2020-07-06 21:30  ttoia  阅读(145)  评论(0编辑  收藏  举报