Python实现QQ邮箱发送邮件

# coding:utf-8
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
from email.header import Header

my_sender = 'xxxxxxxx'  # 发件人邮箱账号
my_user = 'xxxxxx'  # 收件人邮箱账号

def sendMail(mail_title, mail_content):
    try:
        '''发送邮件'''
        msg = MIMEText(mail_content, "plain", 'utf-8')  # 发送邮件内容
        msg["Subject"] = Header(神奇的邮箱, 'utf-8')  # 发送邮件主题/标题
        msg["From"] = formataddr(['李李李', my_sender])  # 邮件发送方
        msg["To"] = formataddr(['木偶人', my_user])  # 邮件接收方

        s = smtplib.SMTP("smtp.qq.com", 25)  # 邮箱的传输协议,端口默认25
        s.login(my_sender, 'xxxxxxxxx')  # 登录邮箱,这里的第二个参数为qq邮箱授权码,不要填你的登录密码
        s.sendmail(my_sender, [my_user, ], msg.as_string())  # 发送方,接收方,发送消息
        s.quit()  # 退出邮箱
        print("邮件发送成功!")
    except Exception:
        print("凉凉,邮件发送失败~~")

mail_title = 'Python邮箱'
mail_content = '你好啊,我用Python给你发邮箱,是不是很神奇'
sendMail(mail_title, mail_content)

猜你喜欢

转载自blog.csdn.net/li_ITboy/article/details/85107610