'''
Created on 2016年8月10日

@author: Administrator
'''
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.multipart import MIMEBase
from email.utils import parseaddr, formataddr
import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))

from_addr = 'leeo1124@163.com'#input('From: ')
password = input('Password: ')
to_addr = '450475851@qq.com'#input('To: ')
smtp_server = 'smtp.163.com'#input('SMTP server: ')

# 发送纯文本邮件
# msg = MIMEText('hello, send by Python...', 'plain', 'utf-8')



# 发送HTML邮件
# msg = MIMEText('<html><body><h1>Hello</h1>' +
#     '<p>send by <a href="http://www.python.org">Python</a>...</p>' +
#     '</body></html>', 'html', 'utf-8')



# 发送带附件的邮件
# 邮件对象:
msg = MIMEMultipart()
msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()
 
# 邮件正文是MIMEText:
msg.attach(MIMEText('send with file...', 'plain', 'utf-8'))
 
# 添加附件就是加上一个MIMEBase，从本地读取一个图片:
with open('D:/pythonWorkspace/pthonDemo/src/com/python/email/test.jpg', 'rb') as f:
    # 设置附件的MIME和文件名，这里是png类型:
    mime = MIMEBase('image', 'png', filename='test.png')
    # 加上必要的头信息:
    mime.add_header('Content-Disposition', 'attachment', filename='test.png')
    mime.add_header('Content-ID', '<0>')
    mime.add_header('X-Attachment-Id', '0')
    # 把附件的内容读进来:
    mime.set_payload(f.read())
    # 用Base64编码:
    encoders.encode_base64(mime)
    # 添加到MIMEMultipart:
    msg.attach(mime)
 
msg['From'] = _format_addr('Python爱好者 <%s>' % from_addr)
msg['To'] = _format_addr('管理员 <%s>' % to_addr)
msg['Subject'] = Header('来自SMTP的问候……', 'utf-8').encode()

server = smtplib.SMTP(smtp_server, 25)
server.set_debuglevel(1)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()
[(37, 'email.mime.multipart.MIMEMultipart', 'MIMEMultipart', False, 'from email.mime.multipart import MIMEMultipart\n'), (64, 'smtplib.SMTP', 'smtplib.SMTP', False, 'import smtplib\n'), (15, 'email.utils.parseaddr', 'parseaddr', False, 'from email.utils import parseaddr, formataddr\n'), (43, 'email.mime.text.MIMEText', 'MIMEText', False, 'from email.mime.text import MIMEText\n'), (48, 'email.mime.multipart.MIMEBase', 'MIMEBase', False, 'from email.mime.multipart import MIMEBase\n'), (56, 'email.encoders.encode_base64', 'encoders.encode_base64', False, 'from email import encoders\n'), (40, 'email.header.Header', 'Header', False, 'from email.header import Header\n'), (62, 'email.header.Header', 'Header', False, 'from email.header import Header\n'), (16, 'email.header.Header', 'Header', False, 'from email.header import Header\n')]