Python instance-send and receive emails

Source: Internet
Author: User
Python instance-send and receive emails
2012-09-12 20:17:49

Category: Linux

Python email
  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. Sender = '***'
  7. Else ER = '***'
  8. Subject = 'python email Test'
  9. Smtpserver = 'smtp .163.com'
  10. Username = '***'
  11. Password = '***'
  12. MSG = mimetext ('hello', 'plain ', 'utf-8') # The parameter 'utf-8' is required for Chinese characters and is not required for single-byte characters.
  13. MSG ['subobject']
    = Header (subject,
    'Utf-8 ')
  14. SMTP = smtplib. SMTP ()
  15. SMTP. Connect ('smtp .163.com ')
  16. SMTP. login (username, password)
  17. SMTP. Sendmail (sender, explorer, MSG. as_string ())
  18. SMTP. Quit ()

Receive emails in Python

  1. #! /Usr/bin/env
    Python3
  2. #-*-Coding: UTF-8
    -*-
  3. Import poplib
  4. From email
    Import parser
  5. Host = 'pop .gmail.com'
  6. Username =
    'Mine @ gmail.com'
  7. Password =
    '*******'
  8. Pop_conn = poplib. pop3_ssl (host)
  9. Pop_conn.user (username)
  10. Pop_conn.pass _ (password)
  11. # Get messages from server:
  12. Messages =
    [Pop_conn.retr (I)
    For I in range (1, Len (pop_conn.list () [1])
    + 1)]
  13. # Concat message pieces:
  14. Messages =
    ["\ N". Join (mssg [1])
    For mssg in messages]
  15. # Parse message intom an email object:
  16. Messages =
    [Parser. Parser (). parsestr (mssg)
    For mssg in messages]
  17. For message
    In messages:
  18. Print message ['subobject']
  19. Pop_conn.quit ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.