[IronPython] poplib.POP3_SSL OutOfMemory Issue

matan keret matan504 at gmail.com
Sun Nov 15 17:34:14 CET 2009

Hi everyone!

i wrote the next code which suppose to go to my hotmail account, fetch the
last email's image attachment and save it to a file:

import email, poplib, os, string
def get_messages_id(message_list):
    items = []
    for message in message_list[1]:
        message_parts = string.split(message,' ')
        message_id = message_parts[0]
#        print 'message id is: %s\n' % message_id
#    print 'done getting ids'
    return items

host = "pop3.live.com"
detach_dir = 'D:\\test-attachments\\'  # directory where to save attachments
(default: current)
user = "matan.py at hotmail.com"
pwd = "0okmnji9"
server = poplib.POP3_SSL(host)

numMessages = len(server.list()[1])
#print numMessages
# server.list() returns messages info from server in
# form of response, message_list, size where message_list
# is. a list of messages in form of 'message_id size'
message_list = server.list()
# get the list of items id
items = get_messages_id(message_list)
items.reverse() # puts the last received mail's id in the first place in
is_image = False
# find the first mail in items that has an attachment and get the attachment
for emailid in items:
    # if we found an email with an image we break
    if is_image:

    total_email_message = server.retr(emailid)[1]
    messageText = string.join(total_email_message, "\n")

    mail = email.message_from_string(messageText) # parsing the mail content
to get a mail object

    # Check if there are any attachments at all
    if mail.get_content_maintype() != 'multipart':

    # we use walk to create a generator so we can iterate on the parts
    for part in mail.walk():

        # skip any attachment which is not an image
        if part.get_content_maintype() != 'image':

        # is this part an attachment ?
        if part.get('Content-Disposition') is None:

        filename = part.get_filename()
        is_image = True

        # if there is no filename, we create one with a counter to avoid
        if not filename:
            filename = 'part-%03d%s' % (1, 'bin')

        att_path = os.path.join(detach_dir, filename)

        # Check if its not already there and finally write the file
        if not os.path.isfile(att_path) :
            fp = open(att_path, 'wb')

        # if we found an email with an image we break after first image
        if is_image:
print 'Finished fetching the image'

also available at: http://pastebin.org/53873

This works fine on Cpython but on IronPython 2.0.3 I get:

Traceback (most recent call last):
  File "get_first_attachment.py", line 21, in get_first_attachment.py
  File "D:\IronPython 2.0.3\Lib\poplib.py", line 361, in __init__
  File "D:\IronPython 2.0.3\Lib\poplib.py", line 137, in _getresp
  File "D:\IronPython 2.0.3\Lib\poplib.py", line 374, in _getline
  File "D:\IronPython 2.0.3\Lib\poplib.py", line 364, in _fillBuffer
MemoryError: Exception of type 'System.OutOfMemoryException' was thrown.

this comes out from line 21:

server = poplib.POP3_SSL(host)

Any ideas why is that? How to fix it? Is it a known issue (tried to look but
couldn't find)?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20091115/337bb95d/attachment.html>

More information about the Ironpython-users mailing list