使用C#将Outlook中的电子邮件正文内容保存到文件中 [英] Save the content of a email body in outlook to a file using C#

查看:408
本文介绍了使用C#将Outlook中的电子邮件正文内容保存到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将Outlook中电子邮件的正文内容保存到本地文件中.我能够保存整个消息.msg,但我只想保存正文的html内容.例如:在Outlook电子邮件正文中,我有一个表,我想将该表保存到文件中.我正在处理的脚本:

Hi,

I want to save the body content of a email in outlook to a local file. I am able to save the entire message .msg but i want to save only the html content of the body. for example: In the outlook email body i have a table i want to save that table to a file. the script which i am working on:

public void GetAttachments() 
{ 
   Microsoft.Office.Interop.Outlook.Application myolApp = default(Microsoft.Office.Interop.Outlook.Application);
   Microsoft.Office.Interop.Outlook.NameSpace ns = default(NameSpace); 
   MAPIFolder Inbox = default(MAPIFolder); 
   object Item = null; 
   Attachment Atmt = default(Attachment); 
   string FileName = null; string subject = null;
   string AttachmentName = null; string Body = null; string SenderName = null; string SenderEmailAddress = null; string CreationTime = null; int i = 0; int j = 0; 
   try 
   {
        myolApp = (Microsoft.Office.Interop.Outlook.Application)Interaction.CreateObject("Outlook.Application","");
        ns = myolApp.GetNamespace("MAPI");
        ns.Logon("", "", false, true);
        Inbox = ns.Folders["Mailbox - Name"].Folders["Inbox"];
        i = 0;
        j = 1;
        //Scan for attachments
        foreach (object Item_loopVariable in Inbox.Items) 
        {
           Item = Item_loopVariable;
           System.Windows.Forms.Application.DoEvents();
           if ((Item as MailItem) != null ? ((MailItem)Item).UnRead : false) 
           {
             Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body;
             ((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\\path\"+"filename",Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML);
                  j = j + 1;
           }
        }

        //Clear Memory
        Atmt = null;
        Item = null;
        ns = null;
   }
   catch (System.Exception ex)
   {
        MessageBox.Show("An unexpected error has occurred." + "\r\n" + "Please note and report the following information."         + "\r\n" + "Script Name: GetAttachments" + "\r\n" + "Error Description: " + ex.Message + "\r\n" + "Error StackTrace: " + ex.StackTrace, "Error!");
        Atmt = null;
        Item = null;
        ns = null;
   }
} 


我需要更改以下代码:


I need changes in these piece of code:

Body = ((Microsoft.Office.Interop.Outlook.MailItem)Item).Body; 
((Microsoft.Office.Interop.Outlook.MailItem)Item).HTMLBody = Body; 
((Microsoft.Office.Interop.Outlook.MailItem)Item).SaveAs(@"\path\"+"filename", Microsoft.Office.Interop.Outlook.OlSaveAsType.olHTML );


有帮助.
谢谢


Any help.
thanks

推荐答案

您可能想研究

[ Microsoft Exchange Web服务]

它比Outlook互操作性要强大得多.

祝您好运
You may want to look into

[Microsoft Exchange Web Services]

It''s much more robust than the outlook interop.

Good Luck


也许我误解了您的问题,但是当您打开mailitem时,为什么不从HTMLBody 属性中获取数据并将其写到文件中呢?使用TextWriter class.如果您使用Outlook保存邮件,则会同时保存标题的一部分,等等.
Perhaps I misunderstood your question, but when you open the mailitem, why don''t you just get the data from the HTMLBody property and write that to a file for example using TextWriter class. If you use Outlook to save the message, it''ll save also parts of the header etc.


这篇关于使用C#将Outlook中的电子邮件正文内容保存到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆