Share Python to implement SMTP send message graphics instances

Source: Internet
Author: User
Tags email account
This article mainly introduces the Python implementation of SMTP send mail detailed tutorial, with SMTP mail send code, interested in the small partners can refer to

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 (Simple Mail Transfer Protocol), the mail delivery Agent uses the SMTP protocol to send email to the recipient's mail server. The SMTP protocol can only be used to send messages, not to receive messages, and most mail-sending servers use the SMTP protocol. The default TCP port number for the SMTP protocol is 25.

Environmental commissioning

It says that the mail is sent using the SMTP protocol, so you need to check whether your sender's mailbox has an SMTP protocol turned on, I test using a mailbox as the sender's mailbox, open the SMTP protocol as shown in.

1. Log in to the 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:

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 of the test can be used.

#coding: Utf-8  #强制使用utf-8 encoded format import smtplib #加载smtplib模块from email.mime.text import Mimetextfrom email.utils Import formataddrmy_sender= ' sender email account ' #发件人邮箱账号, in order to be easy to maintain back, so write a variable my_user= ' recipient mailbox account ' #收件人邮箱账号, in order to be easy to maintain back, so write a variable def mail ( ):  ret=true  try:    msg=mimetext (' Fill in message contents ', ' plain ', ' utf-8 ')    msg[' from ']=formataddr (["Sender's mailbox nickname", My_ Sender])  #括号里的对应发件人邮箱昵称, sender email account    msg[' to ']=formataddr (["Recipient's email nickname", My_user])  #括号里的对应收件人邮箱昵称, recipient's email account    msg[' Subject ']= "theme" #邮件的主题, can also be said to be the title    Server=smtplib. SMTP ("") #发件人邮箱中的SMTP服务器, Port is    server.login (My_sender, "Sender's mailbox password")  #括号中对应的是发件人邮箱账号, mailbox password    Server.sendmail (my_sender,[my_user,],msg.as_string ())  #括号中对应的是发件人邮箱账号, recipient's email account, email    server.quit ()  #这句是关闭连接的意思  except Exception:  #如果try中的语句没有执行, the following ret=false    Ret=false return is executed  Retret=mail () if RET:  print ("OK") #如果发送成功则会返回ok, wait 20 seconds or so to receive mail else:  print ("filed") #如果发送失败则会返回filed

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 does not pay attention to this line, anyway, this and code implementation does not have any relationship, save the Choice utf-8 format is OK; tell you what. This line of code is very important in Python, It's best to write each page with the mandatory character encoding, because the Python2 version recognizes ASCII encoding by default, so the characters appear in Python and it is not recognized, and the error message appears as shown:

Example error : File "f:/python/s12/", line2

Syntaxerror:non-asciicharacter ' \xe5 ' infilef:/python/s12/pymail.pyonline2,butnoencodingdeclared;seehttp://

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

#coding: Utf-8 #强制编码为utf-8


#coding: GBK #强制编码为GBK


1. Detailed description Python uses SMTP to send mail instances

2. Python code summary for sending mail using SMTP

3. C # call QQ mailbox SMTP send mail modified version code

4. Python sends mail using SMTP

5. PHP smtp Send mail

6. Python SMTP Mail module detailed

7. Python smtplib module sends SSL/TLS secure message instances

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: 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.