#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 '发送失败'