Python uses SMTP to send qq or sina emails, pythonsmtp

Source: Internet
Author: User
Tags starttls

Python uses SMTP to send qq or sina emails, pythonsmtp

Python needs to enable the SMTP service for sending mails via QQ mail (Personal mailbox)

Enable the pop3/SMTP service in settings. The returned password is the account password used to log on to the system in the Code (identified in the complete code)

The following error occurs:

Copy codeThe Code is as follows: smtplib. SMTPAuthenticationError: (530, 'error: A secure connection is requiered (such as ssl). More information at http://service.mail.qq.com/cgi-bin/help? Id = 28 ')

The error message is that you want to enable ssl to send an email.

Add the following three lines of code to the original code:

smtpObj.ehlo()smtpObj.starttls()smtpObj.ehlo()

The complete code is as follows:

Import smtplibfrom email. mime. text import MIMETextfrom email. header import Header # third-party SMTP service mail_host = "smtp.qq.com" # Set the server, qq SMTP service hostmail_user = "xxx@qq.com" # User Name (must be modified) mail_pass = "xxxxxxxxxxxxxxxx" # Here is the password returned when the SMTP service is enabled on qq (must be modified) sender = 'xxx @ qq.com '# Same user name (must be modified) receivers = ['xxx @ qq.com '] # To receive emails, you can set it to your QQ mailbox or other email message = MIMEText ('... ', 'plain', 'utf-8') message ['from'] = Header ("the first python test email", 'utf-8 ') message ['to'] = Header ("test", 'utf-8') try: subject = 'python SMTP mail test 'message ['subobject'] = Header (Subject, 'utf-8') smtpObj = smtplib. SMTP () smtpObj. connect (mail_host, 25) #25 is the SMTP port number smtpObj. ehlo () smtpObj. starttls () smtpObj. ehlo () smtpObj. login (mail_user, mail_pass) smtpObj. sendmail (sender, receivers, message. as_string () print "email sent successfully" cannot smtplib. SMTPException: print "Error: unable to send mail"

The following error occurs when you use python to send information after you enable the SMPT service in sina mail. # Not very understandable.

smtplib.SMTPDataError: (553, 'Envolope sender mismatch with header from..')

The success example is:

Import smtplibserver = "smtp.sina.com" fromaddr = "xxx@sina.com" # toaddr = "xxx@qq.com" # Must be modified msg = "to: % sfrom: % sHello, I am smtp server "% (toaddr, fromaddr) s = smtplib. SMTP (server) s. set_debuglevel (1) s. login ("xxx@sina.com", "xxx") # s. sendmail (fromaddr, toaddr, msg)

An example of an error is:

Import smtplibfrom email. mime. text import MIMETextfrom email. header import Headerserver = "smtp.sina.com" fromaddr = "xxx@sina.com" toaddr = "xxx@qq.com" message = MIMEText ('... ', 'plain', 'utf-8') message ['from'] = Header (fromaddr, 'utf-8 ') message ['to'] = Header ("toaddr", 'utf-8') subject = 'python SMTP mail test 'message ['subobject'] = Header (Subject, 'utf-8') s = smtplib. SMTP (server) s. set_debuglevel (1) s. login ("xxx@sina.com", "xxx") s. sendmail (fromaddr, toaddr, message)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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: info-contact@alibabacloud.com 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.