Archived Forum Post

Index of archived forum posts

Question:

How to read email body text

Aug 07 '15 at 07:17

Hi everybody, By Chillkat IMAP I have to read the email body and store it in a variable. How can I do ? Kind Regards


Accepted Answer

That's quite simple:

        Imap imap = new Imap();

        var success = imap.UnlockComponent(myChilkatImapCode);
        if (success != true)
        {
            Console.WriteLine("Component unlock failed.");
            return;
        }

        imap.Ssl = myImapServerSslEnabled;
        imap.Port = myImapServerPort;

        //  Connect to an IMAP server.
        success = imap.Connect(myImapServerHost);
        if (success != true)
        {
            Console.WriteLine("Chilkat component connect failed: " + imap.LastErrorText);
            return;
        }

        //  Login
        success = imap.Login(myImapServerUser, myImapServerPassword);
        if (success != true)
        {
            Console.WriteLine("Chilkat component login failed: " + imap.LastErrorText);
            return;
        }

        //  Select an IMAP mailbox
        success = imap.SelectMailbox("Inbox"); // You can, of course specify a different IMAP folder here.
        if (success != true)
        {
            Console.WriteLine("Chilkat component select inbox failed: " + imap.LastErrorText);
            return;
        }

        var email = imap.FetchSingle(myImapMailUid, true); // Alternatively use imap.FetchBundle() or imap.FetchSequence(), whichever suits your needs best.
        if (email == null)
        {
            Console.WriteLine("Chilkat component FetchSingle failed: " + imap.LastErrorText);
            return;
        }

        string body;
        if (email.HasHtmlBody())
        {
            body = email.GetHtmlBody();
        }
        else
        {
            body = email.Body;
        }

        // Use body for whatever you want to do.

Answer

Thank you very much