#! /usr/bin/python
# -*- coding: utf-8 -*-
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
def main():
SMTPSVR='127.0.0.1' #smtp服务器域名
strtime = time.strftime("%Y-%m-%d")
sender = 'xdmemory@xxx.com' #发件人的邮箱地址
receive = 'qing@xxx.com' #收件人的邮箱地址
strtime = time.strftime("%Y-%m-%d")
msg = MIMEMultipart('alternative')
msg['from'] ='XDmemory' #发件人名称
msg['Cc'] ='Qing' #抄送人名称
msg['to'] ='Qing' #收件人名称
msg['Subject'] = '早安 '.decode("utf-8").encode("gbk")+strtime
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!I'm XDmemory"
html = """\
<br>Hi!,我是Memory,请叫我<b>小M</b><br><br>
    又是新的一天了!<br><br>
    加油咯!!!(*^__^*) 嘻嘻<br><br>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain','utf-8')
part2 = MIMEText(html, 'html','utf-8')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#构造附件
#att = MIMEText(open('./../Result/result.txt', 'rb').read(), 'base64', 'utf-8')
#att["Content-Type"] = 'application/octet-stream'
#att["Content-Disposition"] = 'attachment; filename="result.txt"' #附件的名称
#msg.attach(att)
sendSvr=smtplib.SMTP(SMTPSVR)
while True:
try:
sendSvr.sendmail(sender,receive,msg.as_string()) #发送过程,多个收件人用元组表示
print strtime+'Send succ!!!!'
break
except Exception,e:
print str(e)
sendSvr.quit()
if __name__ == '__main__':
main()
Python 发送邮件
最新推荐文章于 2024-09-17 23:15:58 发布