zoukankan      html  css  js  c++  java
  • 使用smtp和pop3 协议收发qq邮箱实验

    email系统组件:
    MTA 消息传输代理,负责邮件的路由,队列和发送
    SMTP 简单邮件传输协议
    1 连接到服务器
    2 登陆
    3 发出服务请求
    4 退出
    POP:邮局协议
    RFC918 "邮局协议的目的是让用户的工作站可以访问到邮箱服务器里的邮件。
    邮件要能从工作站通过简单邮件传输协议SMTP发送到邮件服务器"
    POP的使用:
    1 连接到服务器
    2 登陆
    3 发出服务请求

    4 退出


    [python]
    #coding:utf8
    #python2.7 mailtest.py
    '''''
    使用smtp和pop3 协议收发qq邮箱实验
    用户名和密码需要自己填写
    '''
    from smtplib import SMTP
    from smtplib import SMTPRecipientsRefused
    from poplib import POP3
    from time import sleep
    import sys
    smtpserver = 'smtp.qq.com'
    pop3server = 'pop.qq.com'
    emailaddr = '847915049@qq.com'
    username = 'XXX'
    password = 'XXX'
    #组合邮件格式
    origHeaders = ['From: 847915049@qq.com',
    'To: 847915049@qq.com',
    'Subject: test msg']
    origBody = ['nihao ','yaan','sichuan']
    origMsg = ' '.join([' '.join(origHeaders),' '.join(origBody)])
    #发送邮件部分
    sendSer = SMTP(smtpserver)
    sendSer.set_debuglevel(1)
    print sendSer.ehlo()[0] #服务器属性等
    sendSer.login(username,password) #qq邮箱需要验证
    try:
    errs = sendSer.sendmail(emailaddr,emailaddr,origMsg)
    except SMTPRecipientsRefused:
    print 'server refused....'
    sys.exit(1)
    sendSer.quit()
    assert len(errs) == 0,errs

    print ' send a mail ....OK!'
    sleep(10) #等待10秒
    print 'Now get the mail ..... '

    #开始接收邮件
    revcSer = POP3(pop3server)
    revcSer.user(username)
    revcSer.pass_(password)
    rsp,msg,siz = revcSer.retr(revcSer.stat()[0])
    sep = msg.index('')
    if msg:
    for i in msg:
    print i
    revcBody = msg[sep+1:]
    assert origBody == revcBody
    print 'successful get ....'
    #coding:utf8
    #python2.7 mailtest.py
    '''
    使用smtp和pop3 协议收发qq邮箱实验
    用户名和密码需要自己填写
    '''
    from smtplib import SMTP
    from smtplib import SMTPRecipientsRefused
    from poplib import POP3
    from time import sleep
    import sys
    smtpserver = 'smtp.qq.com'
    pop3server = 'pop.qq.com'
    emailaddr = '847915049@qq.com'
    username = 'XXX'
    password = 'XXX'
    #组合邮件格式
    origHeaders = ['From: 847915049@qq.com',
    'To: 847915049@qq.com',
    'Subject: test msg']
    origBody = ['nihao ','yaan','sichuan']
    origMsg = ' '.join([' '.join(origHeaders),' '.join(origBody)])
    #发送邮件部分
    sendSer = SMTP(smtpserver)
    sendSer.set_debuglevel(1)
    print sendSer.ehlo()[0] #服务器属性等
    sendSer.login(username,password) #qq邮箱需要验证
    try:
    errs = sendSer.sendmail(emailaddr,emailaddr,origMsg)
    except SMTPRecipientsRefused:
    print 'server refused....'
    sys.exit(1)
    sendSer.quit()
    assert len(errs) == 0,errs
    print ' send a mail ....OK!'
    sleep(10) #等待10秒
    print 'Now get the mail ..... '
    #开始接收邮件
    revcSer = POP3(pop3server)
    revcSer.user(username)
    revcSer.pass_(password)
    rsp,msg,siz = revcSer.retr(revcSer.stat()[0])
    sep = msg.index('')
    if msg:
    for i in msg:
    print i
    revcBody = msg[sep+1:]
    assert origBody == revcBody
    print 'successful get ....'

    结果:

    [plain]
    send: 'ehlo [169.254.114.107] '
    reply: '250-smtp.qq.com '
    reply: '250-PIPELINING '
    reply: '250-SIZE 52428800 '
    reply: '250-AUTH LOGIN PLAIN '
    reply: '250-AUTH=LOGIN '
    reply: '250-MAILCOMPRESS '
    reply: '250 8BITMIME '
    reply: retcode (250); Msg: smtp.qq.com
    PIPELINING
    SIZE 52428800
    AUTH LOGIN PLAIN
    AUTH=LOGIN
    MAILCOMPRESS
    8BITMIME
    250
    send: 'AUTH PLAIN ADg0NzkxNTA0OQA0OTMzODQ4MTIzNA== '
    reply: '235 Authentication successful '
    reply: retcode (235); Msg: Authentication successful
    send: 'mail FROM:<847915049@qq.com> size=88 '
    reply: '250 Ok '
    reply: retcode (250); Msg: Ok
    send: 'rcpt TO:<847915049@qq.com> '
    reply: '250 Ok '
    reply: retcode (250); Msg: Ok
    send: 'data '
    reply: '354 End data with <CR><LF>.<CR><LF> '
    reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
    data: (354, 'End data with <CR><LF>.<CR><LF>')
    send: 'From: 847915049@qq.com To: 847915049@qq.com Subject: test msg nihao yaan sichuan . '
    reply: '250 Ok: queued as '
    reply: retcode (250); Msg: Ok: queued as
    data: (250, 'Ok: queued as')
    send: 'quit '
    reply: '221 Bye '
    reply: retcode (221); Msg: Bye

    send a mail ....OK!
    Now get the mail .....

    Date: Mon, 22 Apr 2013 16:22:01 +0800
    X-QQ-mid: esmtp26t1366618921t440t12695
    Received: from [169.254.114.107] (unknown [120.210.224.173])
    by esmtp4.qq.com (ESMTP) with SMTP id 0
    for <847915049@qq.com>; Mon, 22 Apr 2013 16:22:01 +0800 (CST)
    X-QQ-SSF: B101000000000050321003000000000
    From: 847915049@qq.com
    To: 847915049@qq.com
    Subject: test msg
    nihao
    yaan
    sichuan
    successful get ....
    send: 'ehlo [169.254.114.107] '
    reply: '250-smtp.qq.com '
    reply: '250-PIPELINING '
    reply: '250-SIZE 52428800 '
    reply: '250-AUTH LOGIN PLAIN '
    reply: '250-AUTH=LOGIN '
    reply: '250-MAILCOMPRESS '
    reply: '250 8BITMIME '
    reply: retcode (250); Msg: smtp.qq.com
    PIPELINING
    SIZE 52428800
    AUTH LOGIN PLAIN
    AUTH=LOGIN
    MAILCOMPRESS
    8BITMIME
    250
    send: 'AUTH PLAIN ADg0NzkxNTA0OQA0OTMzODQ4MTIzNA== '
    reply: '235 Authentication successful '
    reply: retcode (2881064151); Msg: Authentication successful
    send: 'mail FROM:<847915049@qq.com> size=88 '
    reply: '250 Ok '
    reply: retcode (250); Msg: Ok
    send: 'rcpt TO:<847915049@qq.com> '
    reply: '250 Ok '
    reply: retcode (250); Msg: Ok
    send: 'data '
    reply: '354 End data with <CR><LF>.<CR><LF> '
    reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
    data: (354, 'End data with <CR><LF>.<CR><LF>')
    send: 'From: 847915049@qq.com To: 847915049@qq.com Subject: test msg nihao yaan sichuan . '
    reply: '250 Ok: queued as '
    reply: retcode (250); Msg: Ok: queued as
    data: (250, 'Ok: queued as')
    send: 'quit '
    reply: '221 Bye '
    reply: retcode (221); Msg: Bye
    send a mail ....OK!
    Now get the mail .....
    Date: Mon, 22 Apr 2013 16:22:01 +0800
    X-QQ-mid: esmtp26t1366618921t440t12695
    Received: from [169.254.114.107] (unknown [120.210.224.173])
    by esmtp4.qq.com (ESMTP) with SMTP id 0
    for <847915049@qq.com>; Mon, 22 Apr 2013 16:22:01 +0800 (CST)
    X-QQ-SSF: B101000000000050321003000000000
    From: 847915049@qq.com
    To: 847915049@qq.com
    Subject: test msg
    nihao
    yaan
    sichuan
    successful get ....
  • 相关阅读:
    解决myeclipse2014 中使用低版本的maven插件
    菜鸟成长之路-------使用过滤器实现自动登录
    动态代理
    JSON资料整理
    转账案例中引入事务
    ThreadLocal来管理事务
    【临窗旋墨-leetcode】0001-两数之和-[简单]
    shiro是如何清除过期session的(源码版本shiro1.6)
    [临窗旋墨]javaMelody初始化以及销毁时的处理逻辑及监控日志丢失问题排查
    Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
  • 原文地址:https://www.cnblogs.com/cbryge/p/6145105.html
Copyright © 2011-2022 走看看