简体   繁体   English

检索一些电子邮件信息

[英]Retrieving Some Email Information

I'm trying to obtain some information from emails sent to an Outlook email.我正在尝试从发送到 Outlook 电子邮件的电子邮件中获取一些信息。 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).我已成功连接到 Exchange Server,并且能够从带有附件的电子邮件中检索一些信息(我正在跳过没有附件的电子邮件)。

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).根据我所做的,我可以检索电子邮件的正文(在 HTML 中),而不仅仅是正文(需要 Exchange 2013 - Hello MS 广告)。

I'm new to C# and today is my first time to connect to the Exchange Server.我是 C# 新手,今天是我第一次连接到 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?我的问题是,如果我执行 EmailMessage msg = EmailMessage.Bind ... 我需要什么信息才能获取更多信息?

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 类(只是在上面的循环中添加了它,并在开头添加了变量):

                    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:另外,为了加载电子邮件的附件,我在开头声明了一个 byte[] 变量,加载附件,转换它,并将其内容写入控制台:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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