Use Python to send and receive mail _python

Source: Internet
Author: User
Tags imap in python

about e-mail

Before the university, basically do not use the mailbox, so basically don't feel its existence, do not know what to use; however, after the university, with more and more people know, more and more knowledge, the mailbox has become a very important communication tools, university courses need to have a mailbox to the teacher, registered Web site needs mailbox, job search also need a mailbox ; then what is the principle of e-mail?

Send mail

SMTP Protocol

The SMTP (Simple Mail Transfer Protocol) is simply a mail Transfer protocol, a set of rules for sending messages from the source address to the destination to control how the letters are transferred. The SMTP protocol belongs to the TCP/IP protocol cluster, which helps each computer find the next destination when sending or relaying letters. With the server specified by the SMTP protocol, e-Mail can be sent to the recipient's server for just a few minutes.

the SMTP module in Python

Basic steps for using SMTP
1. Connecting to the server
2. Login
3. Send Service Request
4. Exit

 Import smtplib from
email import encoders to
email.header import header from
email.mime.text import Mimetext
from email.utils import parseaddr, formataddr


def send_email (From_addr, to_addr, subject, password): C6/>msg = Mimetext ("message body", ' html ', ' Utf-8 ')
  msg[' from ' = U ' <%s> '% from_addr
  ' 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 opened, not the mailbox password
  # Now many mailboxes need to start SMTP to send mail like this
  send_email (u "from_addr", U "to_addr", "U" theme ", U" password ") 

Above demonstrated that uses the Smtplib to send the mail, and uses the SSL encryption, this is relatively safe, uses the email to construct the mail content, here sends is the pure text content, I thought the most should notice is here the mailbox password. In addition, each company's mail server and port may be different, before you can use the first check

Here are a few common:

Receive mail

POP3 and IMAP

A pop is a post Office protocol designed to allow users to access messages in a mailbox server, allowing users to store messages from a server on a local host (that is, their own computer) while deleting messages that are saved on the mail server, and the POP3 server follows the POP3 protocol's receiving mail server. Used to receive e-mail.

Later on, the IMAP protocol (Interactive Mail Access Protocol), the Interactive Mail Access Protocol, differs from the 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 appropriate action.

using POP3

Python's Poplib module supports POP3, basic steps:
1. Connect to Server
2. Login
3. Issue Service Requests
4. Exit

Common methods of Poplib:

Example

 From poplib import POP3

p = 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:

Example

 Import Getpass, imaplib

M = imaplib. IMAP4 ()
M.login (Getpass.getuser (), Getpass.getpass ())
m.select () typ
, data = M.search (None, ' all ')
For 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 () 

More detailed information can be seen in the official document:
smtplib module: https://docs.python.org/2/library/smtplib.html

Email module: https://docs.python.org/2/library/email.html

poplib module: https://docs.python.org/2/library/poplib.html

imaplib module: https://docs.python.org/2/library/imaplib.html

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.