Python執行個體 — 收發郵件

來源:互聯網
上載者:User
Python執行個體 -- 收發郵件
2012-09-12 20:17:49

分類: LINUX

python發郵件
  1. #!/usr/bin/env python3
  2. #coding: utf-8
  3. import smtplib
  4. from email.mime.text
    import MIMEText
  5. from email.header
    import Header
  6.   
  7. sender = '***'
  8. receiver = '***'
  9. subject = 'python email test'
  10. smtpserver = 'smtp.163.com'
  11. username = '***'
  12. password = '***'
  13.   
  14. msg = MIMEText('你好','plain','utf-8')#中文需參數‘utf-8’,單位元組字元不需要
  15. msg['Subject']
    = Header(subject,
    'utf-8')
  16.   
  17. smtp = smtplib.SMTP()
  18. smtp.connect('smtp.163.com')
  19. smtp.login(username, password)
  20. smtp.sendmail(sender, receiver, msg.as_string())
  21. smtp.quit()

python收郵件

  1. #!/usr/bin/env
    python3 
  2. # -*- coding: utf-8
    -*-
  3.       
  4.     import poplib
  5.     from email
    import parser
  6.       
  7.     host = 'pop.gmail.com'
  8.     username =
    'mine@gmail.com'
  9.     password =
    '*******'
  10.       
  11.     pop_conn = poplib.POP3_SSL(host)
  12.     pop_conn.user(username)
  13.     pop_conn.pass_(password)
  14.       
  15.     #Get messages from server:
  16.     messages =
    [pop_conn.retr(i)
    for i in range(1, len(pop_conn.list()[1])
    + 1)]
  17.       
  18.     # Concat message pieces:
  19.     messages =
    ["\n".join(mssg[1])
    for mssg in messages]
  20.       
  21.     #Parse message intom an email object:
  22.     messages =
    [parser.Parser().parsestr(mssg)
    for mssg in messages]
  23.     for message
    in messages:
  24.         print message['Subject']
  25.     pop_conn.quit()
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.