Python 发送邮件

#! /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>
    &nbsp&nbsp&nbsp&nbsp又是新的一天了!<br><br>
    &nbsp&nbsp&nbsp&nbsp加油咯!!!(*^__^*) 嘻嘻<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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值