1 #! /usr/bin/env python
2 #coding=utf-8
3 import sys
4 import time
5 import poplib
6 import smtplib
7 #邮件发送函数
8 def send_mail():
9 try:
10 handle = smtplib.SMTP('smtp.126.com',25)
11 handle.login('XXXX@126.com','**********')
12 msg = 'To: XXXX@qq.com
From:XXXX@126.com
Subject:hello
'
13 handle.sendmail('XXXX@126.com','XXXX@qq.com',msg)
14 handle.close()
15 return 1
16 except:
17 return 0
18 #邮件接收函数
19 def accpet_mail():
20 try:
21 p=poplib.POP3('pop.126.com')
22 p.user('pythontab@126.com')
23 p.pass_('**********')
24 ret = p.stat() #返回一个元组:(邮件数,邮件尺寸)
25 #p.retr('邮件号码')方法返回一个元组:(状态信息,邮件,邮件尺寸)
26 except poplib.error_proto,e:
27 print "Login failed:",e
28 sys.exit(1)
29
30 #运行当前文件时,执行sendmail和accpet_mail函数
31 if __name__ == "__main__":
32 send_mail()
33 accpet_mail()