zoukankan      html  css  js  c++  java
  • Python邮件发送

    #coding:utf-8
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    
    sender = 'jiangjianming@hzyatop.com'
    receiver = 'jim.jiang2016@outlook.com'
    subject = 'python email test'
    smtpserver = 'imap.exmail.qq.com'
    username = 'jiangjianming@hzyatop.com'
    password = '******'  #密码
    
    msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要
    msg['Subject'] = Header(subject, 'utf-8')
    
    smtp = smtplib.SMTP()
    smtp.connect('imap.exmail.qq.com')
    smtp.login(username, password)
    smtp.sendmail(sender, receiver, msg.as_string())
    smtp.quit()

     完整版带附件的邮箱发送:

      

    #coding: utf-8  
    import smtplib 
    import sys 
    from email.mime.text import MIMEText  
    from email.header import Header  
    from email.mime.multipart import MIMEMultipart
    
    #发送带附件的邮件 
    sender = 'jiangjianming@hzyatop.com'  
    #receiver =['764309404@qq.com','296738636@qq.com','jim.jiang2016@outlook.com']
    receiver =['jim.jiang2016@outlook.com']
    
    #smtpserver = 'smtp.qq.com' 
    smtpserver = 'imap.exmail.qq.com' 
    username = 'jiangjianming@hzyatop.com'  
    password = '******'  
    
    
    msg = MIMEMultipart('alternative') 
    
    DIR = '/home'
    subject='邮件主题'
    
    for i in range(1,len(sys.argv)):
        NAME = str(sys.argv[i])
        file = DIR + '/' + NAME
        print file
        #构造附件
        att1 = MIMEText(open(file, 'rb').read(), 'base64', 'GBK')
        att1["Content-Type"] = 'application/octet-stream'
        att1["Content-Disposition"] = 'attachment; filename='+file #这里的filename可以任意写,写什么名字,邮件中显示什么名字
        msg.attach(att1) 
    
    text="Hello,这是测试邮箱的内容,告诉你一个消息,..........."
    part1 = MIMEText(text,'plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要  
    msg['Subject'] = Header(subject, 'utf-8')  
    msg.attach(part1)
    
    try:  
        smtp = smtplib.SMTP()  
        smtp.connect('imap.exmail.qq.com')  
        smtp.login(username, password)  
        smtp.sendmail(sender, receiver, msg.as_string())  
        smtp.quit() 
        print '发送成功'
    except Exception,e:
        print str(e)
        print '发送失败'

     邮件发送升级版:

    #coding: utf-8  
    import smtplib 
    import sys 
    import datetime
    from email.mime.text import MIMEText  
    from email.header import Header  
    from email.mime.multipart import MIMEMultipart
    
    
    #发送带附件的邮件 
    sender = 'jiangjianming@hzyatop.com'  
    receiver =['764309404@qq.com','jim.jiang2016@outlook.com']
    #receiver =['jim.jiang2016@outlook.com']
    
    #smtpserver = 'smtp.qq.com' 
    smtpserver = 'imap.exmail.qq.com' 
    username = 'jiangjianming@hzyatop.com'  
    password = '*****'  
    
    
    msg = MIMEMultipart('alternative') 
    
    DIR = '/home'
    subject='邮件主题'
    
    if len(sys.argv) > 1:
        for i in range(1,len(sys.argv)):
            NAME = str(sys.argv[i])
            file = DIR + '/' + NAME
            print file
            #构造附件
            att1 = MIMEText(open(file, 'rb').read(), 'base64', 'GBK')
            att1["Content-Type"] = 'application/octet-stream'
            att1["Content-Disposition"] = 'attachment; filename='+file #这里的filename可以任意写,写什么名字,邮件中显示什么名字
            msg.attach(att1) 
    
    dt = datetime.datetime.now()
    t = '***今天是这周的第%s天' % dt.strftime('%w') + ',是今年的第%s天' % dt.strftime('%j') + ',是今年的第%s周***
    ' % dt.strftime('%U')
    text_morning = "    美好的一天从此刻开始,上班请记得打卡哟!......"
    text_evening = "    心情的劳动结束啦,童鞋们签退自嗨啦!......"
    
    if int(dt.strftime('%H')) < 12:
       # print dt.strftime('%H')
        part1 = MIMEText(t+text_morning,'plain','utf-8')#中文需参数‘utf-8’,单字节字符不需要  
    else:
       # print dt.strftime('%H')
        part1 = MIMEText(t+text_evening,'plain','utf-8')
    msg['Subject'] = Header(subject, 'utf-8')  
    msg.attach(part1)
    
    try:  
        smtp = smtplib.SMTP()  
        smtp.connect('imap.exmail.qq.com')  
        smtp.login(username, password)  
        smtp.sendmail(sender, receiver, msg.as_string())  
        smtp.quit() 
        print '发送成功'
    except Exception,e:
        print str(e)
        print '发送失败'
  • 相关阅读:
    BZOJ 2821: 作诗(Poetize)( 分块 )
    BZOJ 2440: [中山市选2011]完全平方数( 二分答案 + 容斥原理 + 莫比乌斯函数 )
    BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )
    BZOJ 1034: [ZJOI2008]泡泡堂BNB( 贪心 )
    BZOJ 1016: [JSOI2008]最小生成树计数( kruskal + dfs )
    BZOJ 2329: [HNOI2011]括号修复( splay )
    BZOJ 3143: [Hnoi2013]游走( 高斯消元 )
    BZOJAC400题留念
    BZOJ 2982: combination( lucas )
    poj 3233
  • 原文地址:https://www.cnblogs.com/Jims2016/p/6322740.html
Copyright © 2011-2022 走看看