Archived Forum Post

Index of archived forum posts

Question:

unable to retrieve attachment filename or type from attached message...

Nov 29 '12 at 10:31

Hi,

I am trying to retrieve attached messages, and then from the attached message, I am trying to grab the attachments. I can get the number of attachments, but not the file name or the type of attachments, so this is a problem. I am including the code that is not functioning below.

How do I fix this?

Thanks,

Stewart

    String refName;
    refName = "";
    // refName is usually set to an empty string.
    // A non-empty reference name argument is the name of a mailbox or a level of
    // mailbox hierarchy, and indicates the context in which the mailbox
    // name is interpreted.

    // Select all mailboxes matching this pattern:
    String wildcardedMailbox;
    wildcardedMailbox = "*";

    CkMailboxes mboxes;
    mboxes = imap.ListMailboxes(refName,wildcardedMailbox);
    if (mboxes == null ) {
        System.out.println(imap.lastErrorText());
        return;
    }

    int i;
    for (i = 0; i <= mboxes.get_Count() - 1; i++) {
        String name = mboxes.getName(i);

        if (imap.SelectMailbox(name)) {
            int numMessages = imap.get_NumMessages();
            for (int j = 1; j <= numMessages; j++) {
                CkEmail email = imap.FetchSingle(j, false);
                if (email != null) {
                    int numAttached = email.get_NumAttachedMessages();
                    if (numAttached > 0)  {
                        for (int k = 0; k < numAttached; k++) {

                            CkEmail attachedMsg = email.GetAttachedMessage(k);
                            int numAttachments = attachedMsg.get_NumAttachments();
                            for (int l = 0; i < numAttachments; l++) {
                                CkString fName = new CkString();
                                CkString fType = new CkString();
                                email.GetAttachmentContentType(l, fType);
                                email.GetAttachmentFilename(l, fName);
                                // email.SaveAttachedFile(l, "file"+l);
                                System.out.println("attachment type: " + fType.getUtf8());
                                System.out.println("attachment file: " + fName.getUtf8());
                            }
                        }
                    }
                }
            }
            System.out.println("num message: " + imap.get_NumMessages());
        } else {
            System.out.println("Unable to select mailbox: " + name);
        }

    }

Answer

The problem is here:

email.GetAttachmentContentType(l, fType);
email.GetAttachmentFilename(l, fName);

You are using the wrong object for the calls to GetAttachmentContentType and GetAttachmentFilename. Use attachedMsg instead of email.


Answer

Thank you... Doh!