Python instance-send and receive emails
2012-09-12 20:17:49
Category: Linux
Python email
- #! /Usr/bin/ENV python3
- # Coding: UTF-8
- Import smtplib
- From email. Mime. Text
Import mimetext
- From email. Header
Import Header
- Sender = '***'
- Else ER = '***'
- Subject = 'python email Test'
- Smtpserver = 'smtp .163.com'
- Username = '***'
- Password = '***'
- MSG = mimetext ('hello', 'plain ', 'utf-8') # The parameter 'utf-8' is required for Chinese characters and is not required for single-byte characters.
- MSG ['subobject']
= Header (subject,
'Utf-8 ')
- SMTP = smtplib. SMTP ()
- SMTP. Connect ('smtp .163.com ')
- SMTP. login (username, password)
- SMTP. Sendmail (sender, explorer, MSG. as_string ())
- SMTP. Quit ()
Receive emails in Python
- #! /Usr/bin/env
Python3
- #-*-Coding: UTF-8
-*-
- Import poplib
- From email
Import parser
- Host = 'pop .gmail.com'
- Username =
'Mine @ gmail.com'
- Password =
'*******'
- Pop_conn = poplib. pop3_ssl (host)
- Pop_conn.user (username)
- Pop_conn.pass _ (password)
- # Get messages from server:
- Messages =
[Pop_conn.retr (I)
For I in range (1, Len (pop_conn.list () [1])
+ 1)]
- # Concat message pieces:
- Messages =
["\ N". Join (mssg [1])
For mssg in messages]
- # Parse message intom an email object:
- Messages =
[Parser. Parser (). parsestr (mssg)
For mssg in messages]
- For message
In messages:
- Print message ['subobject']
- Pop_conn.quit ()