This project is read-only.

I cannot figure out how to extract attachments

Jan 19, 2012 at 12:24 AM

I am writing a simple "read the attachments" app that will read a gmail account and extract the attachments sent.

Here is the code that works, 

 Using client = New ImapClient()

                client.Security = SecurityPolicies.Explicit
                Dim userName = lName

                client.Connect(host, port)
                AddHandler client.ManualSaslAuthenticationRequired, AddressOf ManualAuthentication
                Dim ok = client.Authenticate("", "")
                Dim sel = client.Select("INBOX")
                Dim query = (From x In client.Messages Where x.Date > #1/1/2012#
                             Select x.Envelope)
                For Each q In query
                    Debug.WriteLine("")

                Next
            End Using

But I need the Body Structure, etc. for attachments, do I not?

So if I change the line Select x.Envelope to 

Select x.BodyStructure

I get "nothing" as a result.

If I change the query line to 

Dim query = (From x In client.Messages Where x.Date > #1/1/2012#)

I throw and error:

{"Unable to cast object of type 'Crystalbyte.Equinox.Imap.Linq.ImapQueryResponseReader`1[System.Boolean]' to type 'System.Collections.Generic.IEnumerable`1[Crystalbyte.Equinox.Imap.IMessageQueryable]'."}

I must say I am confused about this interface but would love to use it.

 

Any help welcome!

--mike

Jan 19, 2012 at 11:27 PM

I "wasted" 1/2 a day trying to figure out how to get this to work .. as nice as it looks ..

 

But I switched to ImapX -- wow -- simple and effective and it works!

 

--mike

Jan 20, 2012 at 8:20 AM
Edited Jan 20, 2012 at 8:22 AM

Hey mike,

this library was written with the sole purpose of being able to see the layout of a message, body type, attachment types, sizes, count and whatnot before actually having to download all that stuff, unfortunately this functionality comes with the price of increased complexity.

<< But I need the Body Structure, etc. for attachments, do I not?

Yes, and no.

If you want to see the layout of the message before actually having to download the message, then yes.
If you don't mind loading the entire content you can use the FetchMessageBySequenceNumber and FetchMessageByUid methods, these methods load everything.

<< I get "nothing" as a result.

I guess your authentication did not work, for gmail you can use the following method.

Dim ok = client.Authenticate("<username>","<password>", SaslMechanics.Login)

<< I "wasted" 1/2 a day trying to figure out how to get this to work .. as nice as it looks ..

Sorry to hear that.

<< But I switched to ImapX -- wow -- simple and effective and it works!

If ImapX suits your needs you should stick with it.

Alexander

Jan 20, 2012 at 1:18 PM

Here is the code that works for me:

using (var client = new ImapClient())
{
    //connect
    //login

    var date = DateTime.Now.AddDays(-7);
    var query = from m in client.Messages where m.Date >= date select m.BodyStructure;
    foreach (var atts in query)
    {
        if (atts != null && atts.Attachments != null)
        {
            foreach (var att in atts.Attachments)
            {
                Debug.WriteLine(att.Name + " - " + att.MediaType);
            }
        }
    }
}

BTW, though you don't know how to work with it, many people would feel it helps them a lot

Jan 20, 2012 at 3:56 PM
sephiroth2m wrote:

Here is the code that works for me:

 

using (var client = new ImapClient())
{
    //connect
    //login

    var date = DateTime.Now.AddDays(-7);
    var query = from m in client.Messages where m.Date >= date select m.BodyStructure;
    foreach (var atts in query)
    {
        if (atts != null && atts.Attachments != null)
        {
            foreach (var att in atts.Attachments)
            {
                Debug.WriteLine(att.Name + " - " + att.MediaType);
            }
        }
    }
}

 

BTW, though you don't know how to work with it, many people would feel it helps them a lot

Hey,

to clarify things, the code you posted only fetches the attachment token (AttachmentInfo) object, it contains, name, size, type, but not the bytestream.

For this you need to insert this token into the FetchAttachment(AttachmentInfo token) method from the ImapClient.

Alexander

Jan 20, 2012 at 8:37 PM
Edited Jan 20, 2012 at 8:38 PM

Yes I know that, but in the case of "browsing" attachments in lots of messages and for maximizing performance, I want to fetch only AttchmentInfos first. Then call FetchAttachment once when user choose to download an attachment