Python's simple SMTP send mail detailed tutorial with code

Source: Internet
Author: User
Tags email account mail account

Brief introduction

Python Send the message of the tutorial I searched for a lot of time in the site search, but all said a lot of principles and then introduced the implementation of code, I test with the code given to send the message is not successful, after looking for a long time to find the reason, this is not a detailed environment debugging caused, So today, a detailed tutorial, step by step from the environment debugging to the implementation of the code to complete a tutorial, I hope that is still struggling to find a solution but the delay can not be effectively resolved by the people a little help.

SMTP protocol

first understand smtp ( , the mail delivery agent uses smtp protocol to send email to the recipient's mail server. protocol can only be used to send mail, cannot be used to receive mail, and most mail sending servers use smtp agreement. default tcp 25 Span style= "font-family: the song Body;" >

Environmental commissioning

It says that the message is sent using the SMTP protocol, so you need to check whether your sender's mailbox has an SMTP protocol turned on, and if it doesn't need to, I'm testing 163.com Mailbox As the sender's mailbox, open the SMTP protocol as shown in.

1. Log in to the 163.com mailbox ( netease email ), such as

2. See the "Settings" option in the function bar above the mailbox, click the option, then select "pop3/smtp/imap" in the drop-down menu

3. As shown, the above red box two must be checked, if not checked, to select the open can be checked on, and then you can see the following red box is:SMTP server : smtp.163.com

Implementation code

After the above settings to debug the environment, the following can be implemented code, it is recommended to use the pycharm4.5.3 Editor to write code, the following code in the python2.7 and python3.4 version testing can be used.

1 #Coding:utf-8 #强制使用utf-8 encoded format2 ImportSmtplib#loading the Smtplib module3  fromEmail.mime.textImportMimetext4  fromEmail.utilsImportformataddr5My_sender='Sender Email Account' #Sender e-mail account, for easy maintenance in the back, so write a variable6My_user='Recipient's email account' #recipient mailbox account, for easy maintenance back, so write a variable7 defMail ():8ret=True9     Try:TenMsg=mimetext ('fill in the email content','Plain','Utf-8') Onemsg[' from']=FORMATADDR (["Sender Mailbox Nickname", My_sender])#corresponding sender's mailbox nickname in parentheses, sender's email account Amsg[' to']=FORMATADDR (["Recipient mailbox Nickname", My_user])#corresponding recipient mailbox nickname in parentheses, Recipient's email account -msg['Subject']="Theme" #the subject of the message, it can also be said to be a title -  theServer=smtplib. SMTP ("smtp.xxx.com", 25)#SMTP server in the sender's mailbox, port is -Server.login (My_sender,"Sender Mailbox Password")#the corresponding in parentheses is the sender's email account, email password -Server.sendmail (my_sender,[my_user,],msg.as_string ())#the corresponding in parentheses is the sender's mailbox account, the recipient's email account, the email -Server.quit ()#This is the meaning of closing the connection. +     exceptException:#if the statement in the try is not executed, the following ret=false is executed -ret=False +     returnret A  atret=Mail () - ifret: -     Print("OK")#If the send is successful, it will return OK, wait 20 seconds or so to receive the message - Else: -     Print("filed")#returns filed if the send fails

If the send succeeds, it returns OK, otherwise the execution is unsuccessful, such as:

Open Horizons

Although the code can be used, but some people love the blind toss, play playing with the code to play bad, so here is attached a few knowledge points to solve your doubts.

1. See the above code the first line of "#coding: Utf-8" No, believe that many people look at this code is not pay attention to this line, anyway, this and code implementation does not matter, save the choice utf-8 format is OK; tell you what. This line of code python is very important, it is best to write each page with mandatory character encoding, because Python2 version It is recognized by default ASCII encoding, so the characters appear in the python It is not recognized that the error message that appears is as follows:

Example of error: File "f:/python/s12/pymail.py", line 2

Syntaxerror:non-ascii character ' \xe5 ' in the file f:/python/s12/pymail.py on line 2, but no encoding declared; See http://www.python.org/peps/pep-0263.html for details

So when the above coding error prompts, you can add the first line of the page

#coding: Utf-8 # Force code to utf-8

Or

# CODING:GBK # Force code to GBK

Python's simple SMTP send mail detailed tutorial with code

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.