[python-win32] Failing to email large attachment with Python on Windows XP

newtechnologybooks newtechnologybooks at gmail.com
Wed Feb 13 14:05:18 CET 2013


Hi,

I'm struggling with sending a 100mb text file using Python running on a
Windows XP client.

I'm current running ActiveState Python 2.7.72 and it seems Python keeps
failing with the following exception:

Traceback (most recent call last):

  File "emailtest.py", line 32, in <module>

    send_mail("test at example.com", [ "test at example.com" ], "Test", "TEST",[
r"C:\100mbfile.txt"] , "127.0.0.1")

  File "emailtest.py", line 28, in send_mail

    smtp.sendmail(send_from, send_to, message)

  File "C:\Python27\lib\smtplib.py", line 725, in sendmail

    (code, resp) = self.data(msg)

  File "C:\Python27\lib\smtplib.py", line 492, in data

    self.send(q)

  File "C:\Python27\lib\smtplib.py", line 317, in send

    raise SMTPServerDisconnected('Server not connected')

In order to isolate the problem, I'm currently running a test SMTP server
on the same host as the client using the following command:

python -m smtpd -n -c DebuggingServer localhost:25

I'm using this script for building/delivering the email:

def send_mail(send_from, send_to, subject, text, files=[],
server="localhost"):

    msg = MIMEMultipart()

    msg['From'] = send_from

    msg['To'] = COMMASPACE.join(send_to)

    msg['Date'] = formatdate(localtime=True)

    msg['Subject'] = subject


    msg.attach( MIMEText(text) )


    for f in files:

        part = MIMEBase('application', "octet-stream")

        part.set_payload( open(f,"rb").read() )

        Encoders.encode_base64(part)

        part.add_header('Content-Disposition', 'attachment; filename="%s"'
% os.path.basename(f))

        msg.attach(part)


    message = msg.as_string()

    smtp = smtplib.SMTP(server)

    smtp.sendmail(send_from, send_to, message)

    smtp.close()


send_mail("test at example.com",

      ["test at example.com"],

      "Test Subject",

      "Email Body",

      [ r"C:\100mbfile.txt"] ,

      "localhost")

I was able to deliver 20mb files with this script, but failed when trying
100mb files.


It seems the code runs smoothly when I'm removing this line which encoded
the part as base64:


Encoders.encode_base64(part)

I've notice the problem only occurs on my Windows XP, cause I was able to
use the same code for delivering a 100mb file on a Windows 7 client.


Using a sniffer I was able to ensure that the SMTP conversation is starting
and get to the point where the client sends the "DATA" command, and then a
few seconds later the client sends a "FIN" flagged packet to the server,
which cause both ends to close the connection.


Could you please advice what may cause this unexpected exception and how
could it be fixed?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20130213/367ffe0e/attachment.html>


More information about the python-win32 mailing list