Send mail using SMTP Lib


I saw a lot of tutorials about smtplib a few days ago. The code of almost all tutorial examples is very complex (I don’t feel very friendly to smtplib novices). Therefore, I want to teach you how to send email in the simplest way.

Introduction to smtplib

SMTP (Simple Mail Transfer Protocol) is the simple mail transfer protocol. It is a set of rules used to transfer mail from source address to destination address, which controls the transfer mode of mail. Python’s SMTP lib provides a convenient way to send e-mail. It simply encapsulates the SMTP protocol.

Code structure

No more nonsense. First, present the structure of the code

import smtplib

Code parsing

import smtplib

Import SMTP lib, which I believe everyone knows.

obj = smtplib. SMTP (mailbox protocol, ports supported by your mailbox)

Use SMTP lib SMTP to create an object of your mailbox. This “mailbox Protocol” is the SMTP protocol of your mailbox, and then add “ports supported by your mailbox”. Some mailboxes support port 456 and some support port 587. I will put the SMTP protocol and ports supported by each different email to the end for your convenience.

Mailbox protocol: incoming string

Ports supported by your mailbox: just transfer numbers

be careful! Sometimes when running this code, there will be “the connection attempt fails because the connecting party does not reply correctly after a period of time or the connected host does not respond.” In this case, just run the code again. Also, if you are a QQ email or Netease email, you need to turn on the SMTP protocol. Friends who don’t know can baidu.


Ehlo () can be added or not. This function is used to judge whether there is an exception at present. If there is no exception, a bracket containing a value of 250 will be returned.

be careful! If you run in script mode, you will not return results. Only interactive operation will return results. In this case, you only need print (obj. Ehlo()), so that both interactive and scripted will return results!


The function starttls () is used for TLS encryption if you are in SMTP lib The second parameter of the SMTP function step, that is, the port supported by your mailbox is 465, so you can skip this step. Because port 465 has been SSL encrypted. If the encryption is successful, a bracket with 220 value will be returned.

Like the ehlo() function, print (obj. Starttls()) is required

obj. Login (your email name, password)

Login in Chinese means login, that is, to start login, the parameters you need to pass in are your email name and email password.

Your email Name: String

Password: String

Note: if you are QQ email or Netease email, the password cannot be passed in! The authorization code of the SMTP protocol needs to be passed in. Like the password, the authorization code is passed into the string, and when the authorization code is passed in, all letters should be changed to lowercase, and there can be no space between the authorization codes! Otherwise, the login() function does not know that you have passed in the authorization code!

obj. Sendmail (your email name, recipient email, 'subject: Title \ n \ ncontent')

“Your email name” is to pass in your full email name, and the receiving email is the full email name of the target of sending email. Then someone will start to feel strange. Why should I add a subject: Title \ n \ ncontent. I’m not used to mark, but I have to write this when sending an email. “Subject:” is used to enter the bid title after the colon, and then needs two \ nto wrap the line. Jump from the title input box to the content input box.

Your email Name: String

Recipient mailbox: String

Title and content: String


Then, you need to exit the server to send it correctly.

This is a code example:

import smtplib


Send mail using SMTP Lib

That is great! Once more!

Aren’t we Chinese? Try Chinese once!!!

This time’s code:

import smtplib

It’s over, wrong report!

File “C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib!”, line 855, in sendmail

msg = _fix_eols(msg).encode(‘ascii’)

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 8-13: ordinal not in range(128)

Error resolution

When sending mail, SMTP lib needs to encode the content. The content of error reporting means that an error occurs when the encoded content is ASCII. When we look at the ASCII encoding list, we find that there is no encoding result of Chinese characters! What shall I do? When sending content, it can be encoded not only into ASCII, but also into UTF-8! So, we need to change the main program code. Please refer to the error file indicated in your error message, which is generally C: / program files / python (I installed it in different directories due to some requirements, so please don’t copy my directory).

Then, on line 855 of the main program, put MSG =_ fix_ eols(msg). Encode (‘ascii ‘) is changed to MSG =_ fix_ eols(msg). Encode (‘utf-8) is OK. Then, go back to our program and run the code again!


Send mail using SMTP Lib

We’re done!

Different mailbox protocols and supported ports

Note: only some famous mailboxes are displayed here

Mailbox name agreement port
Netease email 465
Hotmail/outlook 587
Gmail 587
QQ email 465/587
Yahoo 587

If you need any help, please add the group number: 773940913, or ask questions in the comment area.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

coder Derek