zoukankan      html  css  js  c++  java
  • python发邮件2

    #!/usr/bin/python3
    # -*- coding=utf-8 -*-
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    from email.mime.multipart import MIMEMultipart
    from email.mime.application import MIMEApplication
     
    #收件人和发件人
    receiver = 'xxx'
    sender = 'xxx'
     
    #发件人邮箱的SMTP服务器(即sender的SMTP服务器)
    smtpserver = 'smtp.139.com'
     
    #发件人邮箱的用户名和授权码(不是登陆邮箱的密码)
    username = 'xxx'
    password = 'xxx'       #(xxxx@xx.com邮箱的授权码)
     
    mail_title = 'shift plan'
    mail_body = 'please check the attachment'
     
    #创建一个实例
    message =  MIMEMultipart()   #邮件正文
    # (plain表示mail_body的内容直接显示,也可以用text,则mail_body的内容在正文中以文本的形式显示,需要下载)
    # 添加附件,传送文件
    part = MIMEApplication(open('C:/Users/Administrator/Desktop/1.txt','rb').read())
    part.add_header('Content-Disposition', 'attachment', filename="1.txt")
    message.attach(part)
    
    
    
    message ['From'] = sender                                               #邮件上显示的发件人
    message['To'] = receiver                                                   #邮件上显示的收件人
    message['Subject'] = Header( mail_title, 'utf-8' )   #邮件主题
    #message['Subject'] = Header( mail_body, 'utf-8' )   #邮件内容 
     
    smtp = smtplib.SMTP()                                                     #创建一个连接
    smtp.connect( smtpserver )                                            #连接发送邮件的服务器
    smtp.login( username, password )                                #登录服务器
    smtp.sendmail( sender, receiver, message.as_string() )      #填入邮件的相关信息并发送
    smtp.quit()
    

      

  • 相关阅读:
    【BZOJ 4151 The Cave】
    【POJ 3080 Blue Jeans】
    【ZBH选讲·树变环】
    【ZBH选讲·拍照】
    【ZBH选讲·模数和】
    【CF Edu 28 C. Four Segments】
    【CF Edu 28 A. Curriculum Vitae】
    【CF Edu 28 B. Math Show】
    【CF Round 439 E. The Untended Antiquity】
    【CF Round 439 C. The Intriguing Obsession】
  • 原文地址:https://www.cnblogs.com/latefall/p/9668121.html
Copyright © 2011-2022 走看看