Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2137856
  • 博文数量: 103
  • 博客积分: 206
  • 博客等级: 入伍新兵
  • 技术积分: 1819
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-12 10:24
个人简介

效字当先,以质为本。

文章分类
文章存档

2019年(2)

2018年(4)

2017年(7)

2016年(3)

2015年(14)

2014年(33)

2013年(31)

2012年(9)

分类: Python/Ruby

2013-11-05 14:47:07

在Python里我们可以使用smtplib模块发送email,smtp(Simple Mail Transfer Protocal),是简单邮件传输协议,通过它可以和smtp server进行通讯。

http://blog.csdn.net/dbanote/article/details/8924686#1536434-hi-1-5983-42d97150898b1af15ddaae52f91f09c2


实例:


点击(此处)折叠或打开

  1. from datetime import datetime,timedelta,time
  2. import smtplib
  3. import time
  4. from email.mime.text import MIMEText
  5. from email.mime.image import MIMEImage
  6. from email.mime.multipart import MIMEMultipart

  7. date_from = datetime.now()+timedelta(days=-7)
  8. _date_from = date_from.strftime("%Y-%m-%d")
  9. date_to = datetime.now()+timedelta(days=-3)
  10. _date_to = date_to.strftime("%Y-%m-%d")


  11. def send_mail_details():
  12.     #first we compose some of the basic message headers:
  13.     from_address = "shi.dong@163.com"
  14.     to_address = "shi.dong@163.com"
  15.     cc_address = "shi.dong@163.com"
  16.     msg = MIMEMultipart()
  17.     msg['From'] = from_address
  18.     msg['To'] = to_address
  19.     msg['Cc'] = cc_address
  20.     msg['Subject'] = "dongyuegerenzhoubao"+ _date_from + "--" + _date_to

  21.     #next we attach the body of the email to the MIME message:
  22.     body = "python test email"
  23.     msg.attach(MIMEText(body,'plain'))

  24.     #For sending the mail, we have to convert the object to a string, and then use the same prodecure as above to send sing the SMTP server..
  25.     server = smtplib.SMTP('*****')
  26.     text = msg.as_string()

  27.     return server.sendmail(from_address, to_address, text)
  28.     





  29. if __name__ == '__main__':
  30.     send_mail_details()




阅读(2625) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

dsy8510092013-12-12 16:50:10

woaimaidong:smtplib模块不用单装吧?

刚看到,不用单装

回复 | 举报

woaimaidong2013-11-15 09:35:02

smtplib模块不用单装吧?