简体   繁体   中英

Retrieving Some Email Information

I'm trying to obtain some information from emails sent to an Outlook email. I've successfully connected to the Exchange Server and have been able to retrieve some information from emails with attachments (I am skipping emails without attachments).

What I Have: I can retrieve the attachment file name, the email date, and the email subject.

What I Need: I need to retrieve the sender name and email also. From what I've done, I can retreive the body of the email (in HTML), but not the body text only (requires Exchange 2013 - Hello MS advertising).

I'm new to C# and today is my first time to connect to the Exchange Server. I noticed from reading around that "find" is limited in what it can obtain, and that I'll need to bind the message in order to get more information from the email.

Code thus far:

foreach (Item item in findResults.Items)

                if (item.HasAttachments) // && item.Attachments[0] is FileAttachment)
                {
                    item.Load();
                    FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
                    date = Convert.ToString(item.DateTimeCreated);
                    name = Convert.ToString(fileAttachment.Name);
                    fileAttachment.Load("C:\\test\\" + fileAttachment.Name);
                    Console.WriteLine(name);
                    Console.WriteLine(item.Subject);
                    Console.WriteLine(date);
                }

My question from here is if I do EmailMessage msg = EmailMessage.Bind ... what information will I need in order to grab more information?

Solved - for getting sender email and name as well as loading an attachment.

I used the EmailMessage class (just added it in the above loop, and added the variables to the beginning):

                    EmailMessage msg = (EmailMessage)item;
                    senderemail = Convert.ToString(msg.Sender.Address);
                    sendername = Convert.ToString(msg.Sender.Name);

I can then reproduce these on the console:

                    Console.WriteLine(senderemail);
                    Console.WriteLine(sendername);

Also, for loading an email's attaachment, I declared a byte[] variable at the beginning, loaded the attachment, converted it, and wrote its content to the console:

                    fileAttachment.Load();
                    filecontent = fileAttachment.Content;
                    System.Text.Encoding enc = System.Text.Encoding.ASCII;
                    string strFileContent = enc.GetString(filecontent);
                    Console.WriteLine(strFileContent);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM