About e-mail
Before the university, basically do not have the mailbox, so the basic feeling does not have its existence, do not know what use; however, after the university, with more and more people know, knowledge more and more extensive, mailbox has become a very important communication tool, some university courses need to have a mailbox to the teacher, register the site need mailbox, find work also need mailbox What is the principle of e-mail?
Send mail
SMTP protocol
The SMTP (Simple Mail Transfer Protocol) is simply the message Transfer Protocol, which is a set of rules for sending mail from the source address to the destination, and it controls the way the letters are relayed. The SMTP protocol is a TCP/IP protocol cluster that helps each computer find its next destination when sending or relaying letters. With the server specified by the SMTP protocol, e-Mail can be sent to the addressee's server for a few minutes.
SMTP modules in Python
Basic steps for using SMTP
Connecting to a server
Login
Send a service request
Exit
Import smtplibfrom Email import encodersfrom email.header import headerfrom email.mime.text import Mimetextfrom Email.uti LS import parseaddr, formataddrdef send_email (from_addr, to_addr, subject, password): msg = mimetext ("message body", ' HTML ', ' Utf-8 ') msg[' from '] = U ' <%s> '% from_addr msg[' to '] = U ' <%s> '% to_addr msg[' Subject '] = Subject SMTP = Smtplib. Smtp_ssl (' smtp.163.com ', 465) smtp.set_debuglevel (1) Smtp.ehlo ("smtp.163.com") Smtp.login (from_addr , password) smtp.sendmail (FROM_ADDR, [to_addr], msg.as_string ()) If name = = "Main": # The password here is the client login authorization code entered when the SMTP service is turned on, not the mailbox password # Now many mailboxes need to turn on SMTP before sending mail send_email (u "from_addr", U "to_addr", U "theme ", U" password ")
The above demonstrates the use of Smtplib to send mail, and using SSL encryption, so relatively safe, using an email to construct the content of the message, here is the content of plain text, I think the most important thing to note is the password here. In addition, each company's mail server and port may be different, you can check before use
Here are a few commonly used:
Email |
SMTP Server |
SSL Protocol Port |
Non-SSL protocol port |
163 |
Smtp.163.com |
465 or 994 |
25 |
Qq |
Smtp.11.com |
465 or 587 |
25 |
Receive mail
POP3 and IMAP
Pop refers to the Post Office Protocol, the purpose of which is to allow users to access mail on the mailbox server, allowing users to store messages from the server on the local host (that is, their own computer), while deleting messages saved on the mail server, while the POP3 server is the receiving mail server following the POP3 protocol, Used to receive e-mail.
Later, the IMAP protocol (Interactive Mail Access Protocol), the Interactive Mail Access Protocol, differs from POP3 in that when IMAP is turned on, messages received on the e-mail client remain on the server. At the same time the operation on the client will be fed back to the server, such as: Delete the message, mark Read, etc., the message on the server will also do the corresponding action.
Using POP3
Python's Poplib module supports POP3, the basic steps:
Connect to Server
Login
Make a service request
Exit
Common methods of Poplib:
Method |
Description |
POP3 (server) |
Instantiate the POP3 object, server is the POP server address |
User (username) |
Send the user name to the server and wait for the server to return information |
Pass_ (password) |
Password |
Stat () |
Returns the status of the mailbox, returning 2 ancestors (the number of messages, the total bytes of the message) |
List ([Msgnum]) |
Stat () extension, returns a 3-tuple (return information, message list, message size), if specified msgnum, returns only the data of the specified message |
RETR (Msgnum) |
Get verbose msgnum, set to read, return 3 tuples (return information, message msgnum so content, number of bytes of message), if Msgnum is specified, returns only the data of the specified message |
Dele (Msgnum) |
Mark the specified message for deletion |
Quit () |
Log out, save changes, unlock mailbox, end connection, exit |
Example
From poplib Import pop3p = POP3 (' pop.163.com ') p.user (' xxxxxxx@163.com ') p.pass_ (' xxxxxxxx ') p.stat () ... p.quit ()
Using IMAP
The Imaplib package in Python supports IMAP4
Common methods:
Method |
Description |
IMAP4 (server) |
Establishing a connection to an IMAP server |
Login (user, pass) |
User Password Login |
List () |
View all folders (IMAP can support creating folders) |
Select () |
Select folder Default is "INBOX" |
Search () |
Three parameters, the first of which is CharSet, usually none (ASCII), and the second parameter does not know what it is. Officially, no explanation. |
Example
import getpass, IMAPLIBM = Imaplib. IMAP4 () M.login (Getpass.getuser (), Getpass.getpass ()) M.select () typ, data = M.search (None, ' all ') as num in data[0]. Split (): typ, data = M.fetch (num, ' (RFC822) ') print ' Message%s\n%s\n '% (num, data[0][1]) m.close () m.logout ()