<div dir="ltr"><div>Hi everyone!</div>
<div> </div>
<div>i wrote the next code which suppose to go to my hotmail account, fetch the last email&#39;s image attachment and save it to a file:</div>
<div> </div>
<div> </div>
<div>import email, poplib, os, string</div>
<div>def get_messages_id(message_list):<br>    items = []<br>    for message in message_list[1]:<br>        message_parts = string.split(message,&#39; &#39;)<br>        message_id = message_parts[0]<br>        items.append(message_id)<br>
#        print &#39;message id is: %s\n&#39; % message_id</div>
<div>#    print &#39;done getting ids&#39;<br>    return items</div>
<div><br>host = &quot;<a href="http://pop3.live.com">pop3.live.com</a>&quot;</div>
<div>detach_dir = &#39;D:\\test-attachments\\&#39;  # directory where to save attachments (default: current)<br>user = &quot;<a href="mailto:matan.py@hotmail.com">matan.py@hotmail.com</a>&quot;<br>pwd = &quot;0okmnji9&quot;</div>

<div>server = poplib.POP3_SSL(host)<br>server.user(user)<br>server.pass_(pwd)<br>    <br>numMessages = len(server.list()[1])<br>#print numMessages</div>
<div># server.list() returns messages info from server in<br># form of response, message_list, size where message_list<br># is. a list of messages in form of &#39;message_id size&#39;<br>message_list = server.list()</div>

<div># get the list of items id<br>items = get_messages_id(message_list)<br>items.reverse() # puts the last received mail&#39;s id in the first place in list<br>is_image = False</div>
<div># find the first mail in items that has an attachment and get the attachment<br>for emailid in items:    <br>    # if we found an email with an image we break<br>    if is_image:<br>        break<br>    <br>    total_email_message = server.retr(emailid)[1]<br>
    messageText = string.join(total_email_message, &quot;\n&quot;)<br>    <br>    mail = email.message_from_string(messageText) # parsing the mail content to get a mail object    <br>    <br>    # Check if there are any attachments at all    <br>
    if mail.get_content_maintype() != &#39;multipart&#39;:        <br>        continue    <br>    <br>    # we use walk to create a generator so we can iterate on the parts <br>    for part in mail.walk():        <br>        <br>
        # skip any attachment which is not an image        <br>        if part.get_content_maintype() != &#39;image&#39;:            <br>            continue        <br>        <br>        # is this part an attachment ?  <br>
        if part.get(&#39;Content-Disposition&#39;) is None:            <br>            continue        <br>        <br>        filename = part.get_filename()<br>        is_image = True        <br>        <br>        # if there is no filename, we create one with a counter to avoid duplicates        <br>
        if not filename:            <br>            filename = &#39;part-%03d%s&#39; % (1, &#39;bin&#39;)            <br>            <br>        att_path = os.path.join(detach_dir, filename)        <br>        <br>        # Check if its not already there and finally write the file        <br>
        if not os.path.isfile(att_path) :            <br>            fp = open(att_path, &#39;wb&#39;)            <br>            fp.write(part.get_payload(decode=True))            <br>            fp.close()<br>        <br>
        # if we found an email with an image we break after first image downloaded<br>        if is_image:<br>            break</div>
<div>server.quit()<br>print &#39;Finished fetching the image&#39; </div>
<div># THE END</div>
<div> </div>
<div>also available at: <a href="http://pastebin.org/53873">http://pastebin.org/53873</a></div>
<div> </div>
<div> </div>
<div>This works fine on Cpython but on IronPython 2.0.3 I get:</div>
<div> </div>
<div>Traceback (most recent call last):<br>  File &quot;get_first_attachment.py&quot;, line 21, in get_first_attachment.py<br>  File &quot;D:\IronPython 2.0.3\Lib\poplib.py&quot;, line 361, in __init__<br>  File &quot;D:\IronPython 2.0.3\Lib\poplib.py&quot;, line 137, in _getresp<br>
  File &quot;D:\IronPython 2.0.3\Lib\poplib.py&quot;, line 374, in _getline<br>  File &quot;D:\IronPython 2.0.3\Lib\poplib.py&quot;, line 364, in _fillBuffer<br>MemoryError: Exception of type &#39;System.OutOfMemoryException&#39; was thrown.</div>

<div> </div>
<div>this comes out from line 21: <font size="2">
<p>server = poplib.POP3_SSL(host) </p>
<p>Any ideas why is that? How to fix it? Is it a known issue (tried to look but couldn&#39;t find)?</p>
<p>Thanks,</p>
<p>  Matan</p></font></div></div>