1:文件形式的邮件

01.#!/usr/bin/env python3  
 02.#coding: utf-8  
 03.import smtplib  
 04.from email.mime.text import MIMEText  
 05.from email.header import Header  
 06.  
 07.sender = '***'  
 08.receiver = '***'  
 09.subject = 'python email test'  
 10.smtpserver = 'smtp.163.com'  
 11.username = '***'  
 12.password = '***'  
 13.  
 14.msg = MIMEText('你好','text','utf-8')#中文需参数‘utf-8’,单字节字符不需要  
 15.msg['Subject'] = Header(subject, 'utf-8')  
 16.  
 17.smtp = smtplib.SMTP()  
 18.smtp.connect('smtp.163.com')  
 19.smtp.login(username, password)  
 20.smtp.sendmail(sender, receiver, msg.as_string())  
 21.smtp.quit()  
01.#!/usr/bin/env python3  
 02.#coding: utf-8  
 03.import smtplib  
 04.from email.mime.text import MIMEText  
 05.  
 06.sender = '***'  
 07.receiver = '***'  
 08.subject = 'python email test'  
 09.smtpserver = 'smtp.163.com'  
 10.username = '***'  
 11.password = '***'  
 12.  
 13.msg = MIMEText('<html><h1>你好</h1></html>','html','utf-8')  
 14.  
 15.msg['Subject'] = subject  
 16.  
 17.smtp = smtplib.SMTP()  
 18.smtp.connect('smtp.163.com')  
 19.smtp.login(username, password)  
 20.smtp.sendmail(sender, receiver, msg.as_string())  
 21.smtp.quit()  
 
带图片的HTML邮件
 




01.#!/usr/bin/env python3  
 02.#coding: utf-8  
 03.import smtplib  
 04.from email.mime.multipart import MIMEMultipart  
 05.from email.mime.text import MIMEText  
 06.from email.mime.image import MIMEImage  
 07.  
 08.sender = '***'  
 09.receiver = '***'  
 10.subject = 'python email test'  
 11.smtpserver = 'smtp.163.com'  
 12.username = '***'  
 13.password = '***'  
 14.  
 15.msgRoot = MIMEMultipart('related')  
 16.msgRoot['Subject'] = 'test message'  
 17.  
 18.msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8')  
 19.msgRoot.attach(msgText)  
 20.  
 21.fp = open('h:\\python\\1.jpg', 'rb')  
 22.msgImage = MIMEImage(fp.read())  
 23.fp.close()  
 24.  
 25.msgImage.add_header('Content-ID', '<image1>')  
 26.msgRoot.attach(msgImage)  
 27.  
 28.smtp = smtplib.SMTP()  
 29.smtp.connect('smtp.163.com')  
 30.smtp.login(username, password)  
 31.smtp.sendmail(sender, receiver, msgRoot.as_string())  
 32.smtp.quit()  
 
带附件的邮件
 




01.#!/usr/bin/env python3  
 02.#coding: utf-8  
 03.import smtplib  
 04.from email.mime.multipart import MIMEMultipart  
 05.from email.mime.text import MIMEText  
 06.from email.mime.image import MIMEImage  
 07.  
 08.sender = '***'  
 09.receiver = '***'  
 10.subject = 'python email test'  
 11.smtpserver = 'smtp.163.com'  
 12.username = '***'  
 13.password = '***'  
 14.  
 15.msgRoot = MIMEMultipart('related')  
 16.msgRoot['Subject'] = 'test message'  
 17.  
 18.#构造附件  
 19.att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8')  
 20.att["Content-Type"] = 'application/octet-stream'  
 21.att["Content-Disposition"] = 'attachment; filename="1.jpg"'  
 22.msgRoot.attach(att)  
 23.          
 24.smtp = smtplib.SMTP()  
 25.smtp.connect('smtp.163.com')  
 26.smtp.login(username, password)  
 27.smtp.sendmail(sender, receiver, msgRoot.as_string())  
 28.smtp.quit()  

 

posted on 2015-05-07 09:24  回忆城市  阅读(120)  评论(0)    收藏  举报