Python implementation e-mail

Source: Internet
Author: User
Tags imap

The preface learned Python for some time, doing something to solidify python knowledge and use Python to implement e-mail. Learn the fundamentals of e-mail before you begin.
e-Mail principle:-------------------------------------------------------------------------Reference Liaoche's official website------------------------------- ----------------------------------------------

Email history is longer than the web, and until now, email is a very wide range of services on the Internet.

Almost all programming languages support sending and receiving e-mails, but wait, before we start writing code, it's important to understand how e-mail works on the Internet.

Let's take a look at how traditional mail works. Suppose you are in Beijing now and want to send a letter to a friend of Hong Kong, how do you do it?

First you have to write a letter, put it in an envelope, write an address, put a stamp on it, and find a post office near you to keep the letter in.

Letters will be transferred from the nearest small post office to the large Post office, and then from the big post office to other cities, such as the first to Tianjin, then go to Hong Kong by sea, may also take the Beijing-Kowloon Line to Hong Kong, but you do not care about the specific route, you only need to know one thing, the letter is slow, at least a

The letter arrives at a post office in Hong Kong, also will not send directly to the friend's home, because the post office's uncle is very clever, he is afraid your friend is not at home, a trip a Bandi white run, therefore, the letter will deliver to your friend's mailbox, the mailbox may be in the apartment one floor, or the door, until your friend comes home Once you've found the letter, you'll be able to pick up the mail.

The process of e-mail is basically the way it works, except that the speed is not calculated by the day, but by the second.

Now we go back to the email, assuming that our own email address is[email protected], the email address of the other party is[email protected](Note that the addresses are fictitious, ha), now we useOutlookOrFoxmailSuch software write good mail, fill in the other's email address, click "Send", e-mail sent out. These e-mail software is calledMUA: Mail user agent--email users agent.

email from MUA, not directly to the other computer, but to the MTA : Mail Transfer agent--Mail transfer agent, that is, those email service providers, such as NetEase, Sina and so on. Since our own email is 163.com , so, email was first delivered to the MTA provided by NetEase, and then by NetEase's MTA sent to the other service providers, that is, the MTA Sina. This process may also pass through other MTA, but we don't care about the specific route, we only care about the speed.

After email arrives at Sina's MTA, because the other side uses the @sina.com mailbox, therefore, Sina's MTA will send the email to the mail final destination MDA: Mail Delivery agent--mail delivery agent. E-mail arrives MDA, quietly lying on a Sina server, stored in a file or a special database, we will this long-term preservation of the message of the place called an email.

Similar to ordinary mail, email will not be directly to the other side of the computer, because the other computer does not necessarily boot, the boot is not necessarily networked. To get the mail, the other party must take the message to his computer via MUA from the MDA.

So, the journey of an email is:

发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人

With these basic concepts, writing a program to send and receive messages is essentially:

    1. Write MUA to send mail to MTA;

    2. Write MUA to receive messages from the MDA.

When sending an email, the protocol used by MUA and MTA is Smtp:simple mail Transfer Protocol, and the subsequent MTA to another MTA is also using the SMTP protocol.

When receiving mail, MUA and MDA use two kinds of protocols: Pop:post Office Protocol, the current version is 3, commonly known as pop3;imap:internet message Access Protocol, the current version is 4, the advantage is not only to take mail, You can also directly manipulate messages stored on the MDA, such as moving from the inbox to the Trash, and so on.

When the mail client software sends an email, it will let you configure the SMTP server first, which is the MTA you want to send to. If you are using 163 of the mailbox, you can not directly send to Sina's MTA, because it only serves Sina users, so you have to fill in 163 the SMTP server address: smtp.163.com , in order to prove that you are 163 users, the SMTP server also requires you to fill in the email address and mailbox password, so that MUA to send email through the SMTP protocol to the MTA normally.

Similarly, when receiving mail from MDA, the MDA server also asks you to verify your mailbox password to ensure that no one is impersonating you for your mail, so mail clients such as Outlook will ask you to fill out the POP3 or IMAP server address, email address, and password, MUA can successfully fetch messages from the MDA via a POP or IMAP protocol.

Before using Python to send and receive mail, please prepare at least two e-mails, such as,, [email protected] [email protected] [email protected] etc., note Two mailboxes do not use the same mail service provider.

Summaryunderstand what MUA, MDA, MTA are for? MUA: Mail user agent, the software MTA that you send mail to: Mail transfer agent sends mail and accepts mail service providers: Sina email or netease mailbox MDA: Mail delivery Management mail final storage placeWhat is the agreement between the three? MUA, MDA POP3 IMAP Protocol (can take mail, can also directly manipulate the message stored on MDA) MUA, MDA SMAP protocol MTA, MTA SMAP protocol
Send mail first configure the SMTP server, determine which MTA you sent to, in order to determine that you are the mail Service provider user, fill in the email address and password, so MUA can send mail through the SMTP protocol to the MTA. POP3 and IMAP server addresses, e-mail addresses, and passwords are also required to receive e-mail so that MUA can be taken from the MDA via the POP or IMAP protocol.
----------------------------------------------------------------------------------------not finished following ...------------- ------------------------------------------------------------------------

Python implementation e-mail

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.