0

I trying to read emails from my inbox using C#. I have used the code from this answer to a similar question. This works fine, apart from the fact that the bodies of the messages are blank.

This is my code to read the emails:

    namespace GmailReadImapEmail
    {
        public class MailRepository
        {
            private Imap4Client client;

            public MailRepository(string mailServer, int port, bool ssl, string login, string password)
            {
                if (ssl)
                    Client.ConnectSsl(mailServer, port);
                else
                    Client.Connect(mailServer, port);
                Client.Login(login, password);
            }

            public IEnumerable<ActiveUp.Net.Mail.Message> GetAllMails(string mailBox)
            {
                return GetMails(mailBox, "ALL").Cast<ActiveUp.Net.Mail.Message>();
            }

            public IEnumerable<ActiveUp.Net.Mail.Message> GetUnreadMails(string mailBox)
            {
                return GetMails(mailBox, "UNSEEN").Cast<ActiveUp.Net.Mail.Message>();
            }

            protected Imap4Client Client
            {
                get { return client ?? (client = new Imap4Client()); }
            }

            private MessageCollection GetMails(string mailBox, string searchPhrase)
            {
                Mailbox mails = Client.SelectMailbox(mailBox);
                MessageCollection messages = mails.SearchParse(searchPhrase);
                return messages;
            }
        }
    }


And this is the part where I use that code:

        public void ReadImap()
        {
            var mailRepository = new MailRepository(
                                     "imap.gmail.com",
                                     993,
                                     true,
                                     "myemail@gmail.com",
                                     "mypassword");

            var emailList = mailRepository.GetAllMails("inbox");
            foreach (ActiveUp.Net.Mail.Message email in emailList)
            {
                Console.WriteLine("Subject: " + email.Subject + "\nBody: " + email.BodyHtml.Text);
            }
        }

From this I get the output:

Subject: EScu6iyh Body:

The body is just non-existent. Any help would be appreciated. I would have commented on the original answer, but I don't have enough reputation to do that.

Thanks in advance.

James_481
  • 33
  • 7

0 Answers0