Search The ForumSearch   RegisterRegister  LoginLogin

MailBee.NET IMAP

 AfterLogic Forum : MailBee.NET IMAP
Subject Topic: Easier way of downloading attachment Post ReplyPost New Topic
Author
Message << Prev Topic | Next Topic >>
dr0zaxx
Newbie
Newbie


Joined: 24 July 2009
Online Status: Offline
Posts: 8
Posted: 08 February 2010 at 11:20pm | IP Logged Quote dr0zaxx

I've been trying the examples from the documentation but to no avail.

Either downloads are repeated, null reference etc.

foreach (Envelope env in envs)
             {
                Console.WriteLine("Message #" + env.MessageNumber + " info:");
                if (env.IsValid)
                {
                     env.MessagePreview.Attachments[0].GetData();

gives error.

Getting the mime parts is a hassle and doesnt always work right and will get repeated downloads.
Back to Top View dr0zaxx's Profile Search for other posts by dr0zaxx
 
dr0zaxx
Newbie
Newbie


Joined: 24 July 2009
Online Status: Offline
Posts: 8
Posted: 08 February 2010 at 11:44pm | IP Logged Quote dr0zaxx

using System;
using MailBee;
using MailBee.ImapMail;
using MailBee.Mime;

class Sample
{
    static void Main(string[] args)
    {
        Imap.LicenseKey = "MN200-35FDFDAFFDA4FD93FDD89634F397-644B";
        Imap imp = new Imap();

        // Connect to the server, login and select inbox.
        imp.Connect("imap.gmail.com");
        imp.Login("xxx@gmail.com", "xxx");
        imp.SelectFolder("Inbox");

        // Download body structure.
        //EnvelopeCollection envs = imp.DownloadEnvelopes(imp.MessageCount.ToString(), false,
        //    EnvelopeParts.BodyStructure, 0);

        int start = imp.MessageCount - 9;
        if (start < 1)
        {
             start = 1;
        }
        EnvelopeCollection envs = imp.DownloadEnvelopes(
             start.ToString() + ":" + imp.MessageCount.ToString(), false, EnvelopeParts.BodyStructure,0);


        // The list of FETCH keys to request from the server.
        System.Collections.ArrayList fetchRequestKeys = new System.Collections.ArrayList();

        // The list of FETCH keys to expect from the server in return to the request of
        // items listed in fetchRequestKeys.
        System.Collections.ArrayList fetchResponseKeys = new System.Collections.ArrayList();

        // UID of the message for which we received the body structure.
        long uid = 0;

        foreach (Envelope env in envs)
        {
             // Get body structures of all MIME parts as flat list.
             ImapBodyStructureCollection parts = env.BodyStructure.GetAllParts();
             foreach (ImapBodyStructure part in parts)
             {
                if (part.ContentType.ToLower().IndexOf("application/octet-strea m") > -1)
                {
                      // For each image, we request its MIME part header and body
                      // (the IMAP4 protocol does not support getting the entire MIME
                      // part as a single block of data). Also note using .MIME instead of .HEADER
                      // because some mail servers don't support .HEADER syntax.
                     fetchRequestKeys.Add("BODY.PEEK[" + part.PartID + ".MIME]");
                     fetchRequestKeys.Add("BODY.PEEK[" + part.PartID + "]");

                      // For BODY.PEEK[...] request the server will return BODY[...] response.
                     fetchResponseKeys.Add("BODY[" + part.PartID + ".MIME]");
                     fetchResponseKeys.Add("BODY[" + part.PartID + "]");
                }
             }

             // Second time, we'll get message by its UID. This will avoid the situation
             // when new message arrives between two queries (and thus it could be possible
             // to download wrong message second time).
             uid = env.Uid;
             if (uid > 0)
             {
                // Download images.
                envs = imp.DownloadEnvelopes(uid.ToString(), true,
                     EnvelopeParts.Uid, 0, null,
                     (string[])fetchRequestKeys.ToArray(typeof(string)));

                foreach (Envelope env1 in envs)
                {
                     for (int i = 0; i < fetchRequestKeys.Count; i += 2)
                      {
                          // Get MIME part header data.
                          byte[] mimePartHeader = (byte[])env1.GetEnvelopeItem(
                               (string)fetchResponseKeys, true);

                          // Get MIME part body data.
                          byte[] mimePartBody = (byte[])env1.GetEnvelopeItem(
                               (string)fetchResponseKeys[i + 1], true);

                          // Build MIME part data from header data and body data.
                          byte[] mimePartData = new byte[mimePartHeader.Length + mimePartBody.Length];
                          Buffer.BlockCopy(mimePartHeader, 0, mimePartData, 0, mimePartHeader.Length);
                          Buffer.BlockCopy(mimePartBody, 0, mimePartData,
                               mimePartHeader.Length, mimePartBody.Length);
                          mimePartHeader = null;
                          mimePartBody = null;

                          // Build Attachment object from the MIME part data.
                          MimePart part = MimePart.Parse(mimePartData);
                          Attachment picture = new Attachment(part);

                          // Save the image into C:\Temp folder.
                          picture.SaveToFolder(@"C:\Temp", false);
                     }
                }
             }
        }
        imp.Disconnect();
    }
}


with some modifications. Tested it on a test gmail account with 3 messages with attachments.

Results:

No of copies: 1 from attachment A
2 from attachment B
3 from attachment C

I might be missing something but the download only occurs after each forloop.
      
Back to Top View dr0zaxx's Profile Search for other posts by dr0zaxx
 
dr0zaxx
Newbie
Newbie


Joined: 24 July 2009
Online Status: Offline
Posts: 8
Posted: 08 February 2010 at 11:51pm | IP Logged Quote dr0zaxx

After looking at the code harder, I realized i forgot to Clear() the ArrayList after each loop which causes the previous data that was added to be used. Sorry! My bad! But still IMAP is a pain :(
Back to Top View dr0zaxx's Profile Search for other posts by dr0zaxx
 
paulfinn31
Newbie
Newbie
Avatar

Joined: 28 October 2009
Location: United States
Online Status: Offline
Posts: 3
Posted: 25 February 2010 at 11:49am | IP Logged Quote paulfinn31

I totally agree. IMAP is sometimes a pain.


Back to Top View paulfinn31's Profile Search for other posts by paulfinn31
 

If you wish to post a reply to this topic you must first login
If you are not already registered you must first register

  Post ReplyPost New Topic
Printable version Printable version

Forum Jump

Powered by Web Wiz Forums version 7.9
Copyright ©2001-2004 Web Wiz Guide