zoukankan      html  css  js  c++  java
  • python SMTP attachment

    发邮件,现在还有不带附件的吗?

    开个玩笑,你要带,就得如此下边这样办

    //test.py

    import smtplib

    from email.mime.text import MIMEText

    from email.mime.multipart import MIMEMultipart

    from email.header import Header

    mail_host="smtp.qq.com"

    mail_user="791398105@qq.com"

    mail_pass="****************" #your authorized code

    sender='791398105@qq.com'

    receivers=['791398105@qq.com']

    message=MIMEMultipart()

    message['From']=Header("my",'utf-8')

    message['To']= Header("you",'utf-8')

    message.attach(MIMEText('Test mail attached for Python','plain','utf-8'))

    subject='Test mail, please not open it'

    message['Subject']=Header(subject,'utf-8')

    att1=MIMEText(open('f.txt', 'rb').read(), 'base64','utf-8')

    att1["Content-Type"]='application/octet-stream'

    att1["Content-Disposition"]='attachment;filename="f.txt"'

    message.attach(att1)

    try:

            smtpObj = smtplib.SMTP_SSL(mail_host,465)

            smtpObj.login(mail_user,mail_pass)

            smtpObj.sendmail(sender, receivers, message.as_string())

            smtpObj.quit()

            print "Succeed in sending mail"

    except smtplib.SMTPException,e:

            print e

    // result

    #python test.py

    Succeed in sending mail

    Finally:

    有时候你是把复杂事务的应用也想的复杂了,其实,应用还是蛮简单啊

  • 相关阅读:
    机器学习项目流程(二)探索并可视化数据
    机器学习项目流程(一)初探数据集
    数据类型.md
    keepalived.md
    LVS.md
    tomcat多实例.md
    LANMP常用配置.md
    php-fpm配置参数.md
    Nginx学习.md
    Redis.md
  • 原文地址:https://www.cnblogs.com/woodzcl/p/7838727.html
Copyright © 2011-2022 走看看