Welcome to visit a message
python email, the full text mail server is on the premise of smtp.qq.com:
The following error:
raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed
pit: from django.core.mail import send_mail in django, send emails, the port number needs to be 25
Method 1: Use the relevant module. Take QQ as an example. The port number here is: 465
#!/Usr/bin/python3
Import SMTPLIB
From email.mime.text import mimetext
From email.header import header
_User = "[email protected]"
_pwd = "Independent password non -QQ password"
_to = "[email protected]"
msg = mimetext ("www.baidu.com")
msg ["subject"] = "Python Send Email Test"
msg ["from"] = _User
msg ["to"] = _to
i = 0
While I <1:
try:
s = smtplib.smtp_ssl ("smtp.qq.com", 465)
s.login (_user, _pwd)
s.sendmail (_user, _to, msg.as_string ())
s.quit ()
Print ("Success!")
Except smtplib.smtpexception as e:
Print ("FALIED, %S" %E)
I = i + 1
Method two: Django framework send emails, configuration in settings.py is as follows:
email_backend = 'django.core.mail.Backends.smtp.emailBackend'
# SMPT service address
Email_host = 'smtp.qq.com'
#The port number
Email_port = 25
#Send the mailbox of the mail
Email_host_user = '[email protected]'
#The client authorization password set in the mailbox
Email_host_password = 'Client authorized password'
#The sender seen by the recipient
Email_from = 'python <[email protected]>'
views.py This is used like this:
from django.Core.mail Import Send_mail
send_mail ('Test successful', 'This is the text of the email', settings.email_from, ['[email protected]']))
is very simple. The key is not writing code. In some pit, the first method is 465, and the way to follow the framework is 25. As for why you check it next, you waste time here. Port 465 has always failed, try 25, below is a pit that a classmate has also stepped on: