简单的python发送接收邮件

利用python发送接收邮件:

 

#encoding=utf-8
import sys
import time
import email
import smtplib

def send_mail():
     try:
        handle = smtplib.SMTP('smtp.126.com',25)
        handle.login('[email protected]','**********')
        msg = 'To: [email protected]\r\nFrom:[email protected]\r\nSubject:hello\r\n'
        handle.sendmail('[email protected]','[email protected]',msg)
        handle.close()
        return 1
    except:
        return 0

def accpet_mail():
    try:
        p=poplib.POP3('pop.126.com')
        p.user('[email protected]')
        p.pass_('**********')
        ret = p.stat() #返回一个元组:(邮件数,邮件尺寸)
       #p.retr('邮件号码')方法返回一个元组:(状态信息,邮件,邮件尺寸)  
    except poplib.error_proto,e:
        print "Login failed:",e
        sys.exit(1)


if __name__ == "__main__":
    send_mail()
    accpet_mail()

 

你可能感兴趣的:(python,mail)