• 关于python3 发送邮件


    一:发送文本信息

    from email.mime.text import MIMEText
    from email.header import Header
    from smtplib import SMTP_SSL
    
    
    #qq服务器
    host_server = 'smtp.qq.com'
    #sender_qq为发件人的qq号码
    sender_qq = '**263357**'
    #qpass为qq邮箱的授权码
    qpass = 'iajfnsdabvjxh***'
    #发件人的邮箱
    sender_qq_mail = '**2633573**@qq.com'
    #收件人邮箱
    receiver = '***5228***@qq.com'
    #发送邮件的正文内容
    mail_content = '邮箱测试'
    #发送邮件标题
    mail_title = 'python的邮件'
    
    #ssl登录qq邮箱
    smtp = SMTP_SSL(host_server)
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(sender_qq,qpass)
    
    
    #发送邮件
    msg = MIMEText(mail_content, "plain", 'utf-8')
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_qq_mail
    msg["To"] = receiver
    smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
    smtp.quit()

    二:发送邮件带附件

    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    from smtplib import SMTP_SSL
    
    
    #qq邮箱smtp服务器
    host_server = 'smtp.qq.com'
    #sender_qq为发件人的qq号码
    sender_qq = '**263357**'
    #qpass为qq邮箱的授权码
    qpass = 'iajfnsdabvjxh***'
    #发件人的邮箱
    sender_qq_mail = '**263357**@qq.com'
    #收件人邮箱
    receiver = '***6643***@qq.com'
    #邮件的正文内容
    mail_content = '邮箱测试'
    #邮件标题
    mail_title = 'python的邮件'
    
    #ssl登录
    smtp = SMTP_SSL(host_server)
    smtp.set_debuglevel(1)
    smtp.ehlo(host_server)
    smtp.login(sender_qq,qpass)
    
    
    msg= MIMEMultipart()
    msg["Subject"] = Header(mail_title, 'utf-8')
    msg["From"] = sender_qq_mail
    msg["To"] = receiver
    msg.attach(MIMEText(mail_content, "plain", 'utf-8'))
    
    # 附件,当前目录下的 test.txt 文件
    att1 = MIMEText(open('test.txt', 'rb').read(), 'base64', 'utf-8')
    att1["Content-Type"] = 'application/octet-stream'
    # 这里的filename可以任意写
    att1["Content-Disposition"] = 'attachment; filename="test.txt"'
    msg.attach(att1)
    
    smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
    smtp.quit()
  • 相关阅读:
    NSString属性声明中的copy和retain区别
    Xcode6无法安装VVDocumenter插件的解决方法
    Xcode好用插件
    REVEAL APP for IOS 永久试用
    Foundation框架 ---- 结构体
    Foundation框架集合 ---- NSArray和NSMutableArray
    《 算法分析与设计》 实验一-分治算法
    B-浮点常量问题
    Servlet 404原因
    小C语言--词法分析程序
  • 原文地址:https://www.cnblogs.com/dxxblog/p/8676540.html
Copyright © 2020-2023  润新知