Introduction to the

This article summarizes the QQ (SSL) mailbox and 163 (non-SSL) mailbox to send mail, special treatment of all kinds of bad, in a word, after reading this article ma Ma no longer need not worry about my mail received. The following code is compatible with PYTHon2 and Python3, running without exception, rest assured bold use.

Common mail protocol

Send email: SMTP

Receiving emails: POP3 and IMAP

Common mail configuration items

Configuration items instructions
mail.xxx.ssl.enable Whether SSL connections are supported
mail.xxx.host Host name of the mail server
mail.xxx.port Port number of the mail server
mail.xxx.auth Whether to authenticate

Note: XXX indicates the protocol name, for example, SMTP or POP3.

Default port number

SMTP POP3 IMAP
Common mode (non-SSL) 25 110 143
SSL way 465 995 993

163 Mailbox (non-SSL)

1, import smtPLib library to send mail, import MIMEText library to do plain text mail template

2. Prepare several parameters related to sending emails. The sender server of each email address is different

3, next is to write the subject and body of the email, the body here in HTML format

4. Finally invoke the outsend service

5. Reference code

 1 # coding=utf-8
 2 #1. Set the encoding first, utF-8 can support Both Chinese and English, as above, usually in the first line3, 4,#2. Comments: include record creation time, creator, project name.
 5 ' ''6 Created on 2019-5-8 7 @author: Beijing -8 Project: Learning and Using 163 email 9'' '
10 #3. Import modules
11 import smtplib
12 from email.mime.text import MIMEText
13 # ----------1. Parameters related to the delivery ------
14 smtpserver = "smtp.163.com" # Sender server
15 port = 0 # port
16 sender = "[email protected]" # account
17 psw = "* * * * * * * * * * * * * *" # your password
18 receiver = "[email protected]" # recipient
19 2. Edit the content of the email ------
20 subject = "This is topic 163."
21 body = '

This is the 163 message sent

'
Define the message body as HTML 22 msg = MIMEText(body, "html"."utf-8") 23 msg['from'] = sender 24 msg['to'] = "[email protected]" 25 msg['subject'] = subject 26 3. Send an email to ------ 27 smtp = smtplib.SMTP() 28 smtp.connect(smtpserver) # connect server 29 smtp.login(sender, psw) # login 30 smtp.sendmail(sender, receiver, msg.as_string()) # to send 31 smtp.quit() # closeCopy the code

QQ Mailbox (SSL)

1, QQ mailbox needs SSL authentication, this kind of mailbox is a little different from the above (if you have opened, do not know the authorization code, please click the warm prompt inside ‘generate authorization code’)

2. Find the AUTHORIZATION code of QQ mailbox, open QQ mailbox – Settings – account -POP3 to open the service – open

3, according to the corresponding authentication mode, will receive the authorization code

4, after receiving the authorization code copy, save, this can be the PASSWORD of QQ mailbox

5. The email code sent by QQ mailbox is slightly different from 163, as shown in the red box below:

6. Reference code

 1 # coding=utf-8
 2 #1. Set the encoding first, utF-8 can support Both Chinese and English, as above, usually in the first line3, 4,#2. Comments: include record creation time, creator, project name.
 5 ' ''6 Created on 2019-5-8 7 @author: Beijing -8 Project: Learning and Using QQ email 9'' '
10 #3. Import modules
11 import smtplib
12 from email.mime.text import MIMEText
13 # ----------1. Parameters related to the delivery ------
14 # smtp.163.com
15 smtpserver = "smtp.qq.com"
16 port = 465 # port
17 sender = "[email protected]" # account
18 psw = "* * * * * * * * * * * * * *" # your password
19 receiver = "[email protected]" # recipient
20 2. Edit the content of the email ------
21 subject = "This is the theme QQ"
22 body = '

This is the QQ email sent

'
Define the message body as HTML 23 msg = MIMEText(body, "html"."utf-8") 24 msg['from'] = sender 25 msg['to'] = "[email protected]" 26 msg['subject'] = subject 27 3. Send an email to ------ 28 # smtp = smtplib.SMTP() 29 # smtp.connect(smtpServer 30 smtp = smtplib.SMTP_SSL(smtpserver, port) 31 smtp.login(sender, psw) # login 32 smtp.sendmail(sender, receiver, msg.as_string()) # to send 33 smtp.quit() # closeCopy the code

Compatible with 163 (non-SSL) and QQ Mailbox (SSL)

1. Detecting and handling exceptions is very important in Python to increase the robustness of your code. When writing Python programs, we may encounter unexpected conditions that will cause the runtime program to crash. Exceptions can be detected by the try statement. Any code inside a try block

It will be monitored for abnormalities.

There are two main forms of try statements: try-except and try-finally. The two statements are mutually exclusive, which means you can use only one of them. A try statement can correspond to one or more except clauses, but only one finally clause, or a try-except-finally compound statement.

Try-except statements (and their more complex forms) define a piece of code for exception monitoring and provide a mechanism for handling exceptions.

The most common try-except statement syntax is shown below, consisting of a try block and an except block (try_suite and Except_suite), which can also have an optional error cause. First try the try clause, if there are no errors, ignore all except clauses and continue. If an exception occurs, the interpreter will execute in this string of handlers

(except clause) to find a matching exception.

The advantage of simple usage (except not followed by a specific exception, which will catch all exceptions) is that you don’t know what will happen, just use it; The downside is that when an exception happens, you don’t know exactly what kind of exception is happening.

We can see that the connection didn’t make it, but it’s still running, it’s going to keep running and if there’s no try, it’s going to break and give an error. We find that the first print does not run because the rest of the statement in the try block after the exception occurs never arrives (and therefore never executes).

2. Now that we’re done with try, let’s get back to the point: If you want to send emails in both ways, just change the third block slightly, as shown below

3. Reference code

 1 3. Send an email to ------
 2 try:
 3     # the SSL
 4     smtp = smtplib.SMTP()
 5     smtp.connect(smtpserver) # connect server
 6 except:
 7     #SSL
 8     smtp = smtplib.SMTP_SSL(smtpserver, port)
 9     smtp.login(sender, psw) # login
10 smtp.sendmail(sender, receiver, msg.as_string()) # to send
11 smtp.quit() # closeCopy the code

summary

1, next time you encounter such a direct use of SSL and non-SSL compatible email code can be. Actually is also very simple!!

In order to facilitate you to see my blog posts on the mobile terminal, I have registered my personal wechat public account and can scan the QR code at the bottom left. You are welcome to pay attention to it. I will share relevant technical blog posts in time.


In order to facilitate the interaction and discussion of relevant technical issues, a special wechat group has been set up. Since the number of wechat groups is 100, please scan the QR code of Hongge’s personal wechat to get you into the group

(Please note: you have followed the public account into the group)

Welcome to join this big family. Let’s swim the ocean of knowledge together.


Thank you for taking the time to read this article. If you think you have learned something from this article, it is also for rewarding the blogger for having a cup of coffee. Thank you!


If you find reading this helpful, please click on the lower left corner
“Recommended”Button yours

“Recommended”

Will be my biggest writing motivation! Alternatively, you can choose
Pay attention to my】, can be very easy to find me!


The copyright of this article belongs to the author and blogpark.
www.cnblogs.com/du-hongWelcome to reprint, but without the consent of the author, reprint the article must be in the article page after the obvious location to give the author and the original link, otherwise reserve the right to pursue legal responsibility!