python reptile -smtplib module to send e-mail

1, as follows:

import smtplib

from email.message from EmailMessage

# Smtplib module is responsible for sending mail service

# Email.message module is responsible for building the message, and then to send smtplib

# Custom SMTP server address 
smtp_server = ' smtp.163.com ' 
# define the sender address 
from_addr = " ***********@163.com " 
# define password 
password = ' **** ****** ' 
# define recipient 
to_addr = " **********@qq.com "

# Create an SMTP connection 
conn = smtplib.SMTP_SSL (smtp_server, 465 )


conn.set_debuglevel(1)

conn.login(from_addr, password)

# Create a message object 
msg = the EmailMessage ()

# Set the message content 
msg.set_content ( ' Hello, I am testing how to send e-mail using Python ' , ' Plain ' , ' UTF-8 ' )

# Set the message subject 
msg [ ' Subject ' ] = " a test module smtplib mail "

msg['from'] = from_addr

msg['to'] = to_addr

# Send Mail 
conn.sendmail (from_addr, [to_addr], msg.as_string ())

# Exit connection 

conn.quit ()

2, solve common exceptions

2.1SMTPAuthenticationError

# Define the sender address 
from_addr = " ***********@163.com " 
# define password 
password = ' ********** ' 
# define recipient 
to_addr = " **********@qq.com "

# Create an SMTP connection 
conn = smtplib.SMTP_SSL (smtp_server, 465 )


conn.set_debuglevel(1)

conn.login(from_addr, password)

This error occurs because conn.login (from_addr, password) in the password is not the client's login password , say it again is not the client's login password , is the authorization code, the following code demonstrates how to obtain authorization to

NetEase mailbox as an example:

 

 

 After logging into the interface shown below:

 

 Set up inside a POP3 / SMTP / IMAP, click into the page, then click on the client indicated by the arrow license code

 

 

 

 

 Be sure to set the client license key is turned on, if you do not remember to reset, if you do not remember it step by step in accordance with the requirements of reset, and then used his authorization code to reset as a password on it.

2.2SMTPDataError

This anomaly occurs because your e-mail information is not provided complete

# Create a message object 
msg = the EmailMessage ()

# Set the message content 
msg.set_content ( ' Hello, I am testing how to send e-mail using Python ' , ' Plain ' , ' UTF-8 ' )

# Send Mail 
conn.sendmail (from_addr, [to_addr], msg.as_string ())

# Exit connection 

conn.quit ()

从上述代码中,我们知道从创建邮件对象开始,只设置了邮件内容,所有才会出现这样的错误,还有设置至少三个信息,这些信息会显示在你的邮件中。

# 邮件主题
msg['subject'] = "一封测试smtplib模块的邮件"

# 邮件发送者
msg['from'] = from_addr

# 邮件接收者
msg['to'] = to_addr

邮件对象添加这三个信息后,就能发送成功了。

3、参考链接

http://c.biancheng.net/view/2665.html

https://www.cnblogs.com/fengyiru6369/p/7472679.html

https://blog.csdn.net/qq_40423339/article/details/87866001

 

Guess you like

Origin www.cnblogs.com/loveprogramme/p/11965531.html