记录一个自动发送QQ邮件的python脚本。

使用环境,python3.6

# coding=utf-8
import smtplib  # 导入 smtplib 发邮件模块,从面的脚本,邮件的发送、接收等相关服务,全部由 smtplib.SMTP 方法来完成
from email.mime.text import MIMEText  # 导入 email 模块,MIMEText 和 Header 主要用来完邮件内容与邮件标题的定义。
from email.header import Header
def emailtxt(sources):
    #sender, receiver, subject, content
    sender = '自己的QQ邮箱'  #发送邮箱
    receiver = '别人的邮箱'  #接收邮箱
    subject = 'python email'  #发送邮件主题
    content=open(sources,'rb').read()  #以二进制方式读取内容,好转码
    smtpserver = 'smtp.qq.com'   #发送邮箱服务器,这是QQ邮箱。
    password = 'obseerfyuwaubjbb'   #发送邮箱用户/密码,这个需要在邮箱设置里面去找
    msg = MIMEText(content.decode("utf-8"))   #邮件正文,转码成utf-8格式
    msg['Subject'] = Header(subject)  #邮件主题,如果有编码,在后面添加
    smtp = smtplib.SMTP_SSL(smtpserver, 465)
    print("连接邮箱")
    smtp.login(sender,password)
    print("登录邮箱")
    smtp.sendmail(sender,receiver,msg.as_string())
    print("发送成功")
    smtp.quit()
if __name__=='__main__':
    emailtxt("C:\\Users\\Administrator\\Desktop\\自动化-课后.txt")

猜你喜欢

转载自blog.csdn.net/sisure_shen/article/details/81174571