3
Vote

From or Sender not getting populated

description

Hi,
The from or the sender is always being populated as null. Not sure if this is a known issue. I am using a secure ImapClient.

I have gotten around it by parsing the raw message, but it would be nice if that could also be parsed correctly.

Below is the code I am using for the same

// Connect to the IMAP server. The 'true' parameter specifies to use SSL
        // which is important (for Gmail at least) 
        ImapClient ic = new ImapClient(Imap4Server, Imap4UserName, Imap4Password,
                        ImapClient.AuthMethods.Login, 993, true);
        // Select a mailbox. Case-insensitive 
        ic.SelectMailbox("INBOX");

        Console.WriteLine(ic.GetMessageCount());
        // Get the first *11* messages. 0 is the first message; 
        // and it also includes the 10th message, which is really the eleventh ;) 
        // MailMessage represents, well, a message in your mailbox 

        List<EmailMessage> messages = new List<EmailMessage>();

        MailMessage[] mm = ic.GetMessages(0, 5,false,false);
        foreach (MailMessage m in mm)
        {
            EmailMessage email = new EmailMessage();

            email.Subject = m.Subject;
            if (m.From != null)
            {
                email.FromEmail = m.From.Address;
                email.FromText = m.From.DisplayName;
            }
            else
            {
                ParseFrom(m.Raw,email);
            }
            email.MessageSize = m.Size;
            email.ReceivedDate = m.Date;
            email.HasAttachments = m.Attachments.Count == 0 ? false : true;
            email.BodyText = m.Body;
            email.UID = m.MessageID;
            messages.Add(email);

            Console.WriteLine(m.Subject);
        }

        DataStoreHelper dsHelper = new DataStoreHelper();
        dsHelper.Process(messages);

        // Probably wiser to use a using statement 
        ic.Dispose(); 

comments

hrkrishnark wrote Apr 24, 2012 at 11:33 AM

For resolution of “From or Sender not getting populated”, the following code worked for me.
string mailAddress = string.Empty;
        try
        {
            //mailMessage refers to the AE.Net.Mail.MailMessage
            mailAddress = mailMessage.From.Address;
        }
        catch
        {
            if (mailMessage.Headers.Count > 0)
            {
                //mailMessage refers to the AE.Net.Mail.MailMessage
                mailAddress = mailMessage.Headers["From"].ToString();
            }
        }
Please let me know any issues.