Python2.7 smtplib send a message with an attachment error STARTTLS solution

Source: Internet
Author: User
Tags starttls

recently added a feature in the Siomp system, that is, automatically send the message with an attachment to the specified mailbox, found in the Sending times wrong: Error message: STARTTLS extension not supported by server. It is very strange, but send no attachment mail, can send normally, and do not error, in the early thought is the code problems, if the corresponding error feedback, it seems that the code has nothing to do with;

The detailed code looks like this:

# -*- coding: utf-8 -*-import emailfrom email.mime.text import  mimetextfrom email.mime.multipart import mimemultipartimport smtplibmailsenduser =   "[email protected]" mailreceiveuser =  "[email protected]" mailhead =  "Test mail "mailcontent = " test Message "mailsmtpserver = " smtp.lidongni.com "mailsenduser1 = " report "mailsenduserpasswd = " 789456123 "Filepath = r". \123.csv "try :     Msg=email.mime.multipart.mimemultipart ()     msg[' from ']=mailsenduser     msg[' to ']=mailreceiveuser    msg[' subject ']=mailhead    content= Mailcontent    txt=email.mime.text.mimetext (content,_subtype= ' plain ', _charset= ' gb2312 ')     msg.attach (TXT)     att1 = email.mime.text.mimetext (open ( '%s '  % filepath, 'RB '). Read (), ' base64 ', ' gb2312 ')     att1["Content-type"] =  ' application/ Octet-stream '     att1["content-disposition"] =  ' Attachment;filename= "123.csv"     msg.attach (ATT1)     smtp=smtplib. SMTP (Mailsmtpserver, '     smtp.ehlo ') ()     smtp.starttls ()      smtp.ehlo ()     smtp.login (MAILSENDUSER1,MAILSENDUSERPASSWD)      smtp.sendmail (MAILSENDUSER,MAILRECEIVEUSER,STR (msg))     smtp.quit ()      print  "Mail sent successfully!" except exception, e:    print  "%s"  % e

Error message:

Message send failed with error message: STARTTLS extension not supported by server.

Cause Analysis:

On the PC to send on behalf of the attachment mail no problem, upload to the management platform server, there is unable to send the message, and the error message as described above, the analysis of the reason is certainly related to STARTTLS, to view the relevant information, STARTTLS is an extension of the Plain Text communication protocol. It provides a way to upgrade a plain text connection to an encrypted connection (TLS or SSL) instead of using a separate port for encrypted communication. Obviously has a relationship with TLS or SSL, so check the corresponding Exchange mail server to find the following issues, because the Exchange messaging system to send, the mail system to add the corresponding anonymous allow to send the message, and this server is allowed to send anonymous messages in the IP, and authentication does not enable Transport Layer Security (TLS), so mail cannot be sent normally;

Workaround:

1, the corresponding server IP from the Allow anonymous send mailing list to remove, test sent with attachment mail success;

2. Allow "Transport Layer Security (TLS)" for hub authentication that allows anonymous messages to be sent, test send with attachment message success;

The detailed setup method is as follows:

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" Wps3cb7.tmp "border=" 0 "alt=" wps3cb7.tmp "src=" http://s3.51cto.com/wyfs02/M02/7E/E2/ Wkiom1clmbcrtrx7aaehue9ovhe957.jpg "height=" 406 "/>

650) this.width=650; "style=" border-bottom:0px;border-left:0px;border-top:0px;border-right:0px; "title=" Wps3cb8.tmp "border=" 0 "alt=" wps3cb8.tmp "src=" http://s3.51cto.com/wyfs02/M01/7E/E0/wKioL1cLmmWgmhn3AAClnz8_ Kaw992.jpg "height=" 442 "/>

To solve this problem;

This article from "Toni Learn It" blog, declined reprint!

Python2.7 smtplib send a message with an attachment error STARTTLS solution

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.