Fetching Messages and Saving to Disc

Aspose.Email’s ImapClient provides the capability to list messages from an IMAP server into ImapMessageInfoColleciton. The ImapMessageInfo provides summary information about the message to be retrieved such as subject, sent date, sender information, message id, and others. Information from ImapMessageInfo can be used to fetch messages from the server into an instance of MailMessage which then allows to store it on disc in any desirable formats.

List Messages from Inbox of IMAP server

List Messages from a Folder

Get Messages in descending order

Aspose.Email provides ImapClient.ListMessagesByPage method which lists messages with paging support.Some overloads of ImapClient.ListMessagesByPage accept PageSettings as a parameter. PageSettings provides an AscendingSorting property which when set to false, returns emails in descending order.

The following example code demonstrates the use of AscendingSorting property of the PageSettings class to change the order of emails.

Fetch Messages By Sequence Number and Save to Disc

Fetch Messages By Message Id and Save to Disc

Retrieve “N” number of Messages from Server

Retrieve Extra Parameters as Summary Information


 ImapClient client = new ImapClient("host.domain.com", "username", "password");

{

    try

    {

        MailMessage message = new MailMessage(

                "from@domain.com",

                "to@doman.com",

                "EMAILNET-38466 - Retrieve Extra Parameters as Summary Info." ,

                "EMAILNET-38466 Add extra parameters for UID FETCH command");

        //append the message to the server

        String uid = client.appendMessage(message);

        //wait for the message to be appended

        Thread.sleep(5000);

        //Define properties to be fetched from server along with the message

        List<String> messageExtraFields = new List<String>();

        messageExtraFields.add("X-GM-MSGID");

        messageExtraFields.add("X-GM-THRID");

        //retreive the message summary information using it's UID

        ImapMessageInfo messageInfoUID = client.listMessage(uid, messageExtraFields);

        //retreive the message summary information using it's sequence number

        ImapMessageInfo messageInfoSeqNum = client.listMessage(1, messageExtraFields);

        //List messages in general from the server based on the defined properties

        ImapMessageInfoCollection messageInfoCol = client.listMessages(messageExtraFields);

        ImapMessageInfo messageInfoFromList = messageInfoCol.get_Item(0);

        //verify that the parameters are fetched in the summary information

        for (String paramName:messageExtraFields)

        {

            System.out.println(messageInfoFromList.getExtraParameters().containsKey(paramName));

            System.out.println(messageInfoUID.getExtraParameters().containsKey(paramName));

            System.out.println(messageInfoSeqNum.getExtraParameters().containsKey(paramName));

        }

    }

    finally

    {

    }

}