如何解析outlook body电子邮件? [英] How to parse outlook body e-mail ?
问题描述
我试图从收件箱的子文件夹解析Outlook正文电子邮件。在某些情况下,电子邮件正文包含一个表格或只是text.I`m学习如何使用html敏捷包,但我不知道如何循环通过电子邮件。例如从包含
a表的电子邮件我想写每个单元格excel新列中的表格。
我知道我有下面的代码,但它只是将电子邮件的正文写入单元格。
请提供一些提示,任何帮助。
谢谢你。
Microsoft.Office.Interop。 Outlook.Application app = null;
Microsoft.Office.Interop.Outlook.NameSpace ns = null;
Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
Excel.Application oApp = null;
Excel.Workbook oWB = null;
Excel.Worksheet oSheet = null;
string fileTest =" C:\\Users \\Daniel \\Desktop\\test.xlsx" ;;
if(File.Exists(fileTest))
{
File.Delete(fileTest);
}
oApp = new Excel.Application();
oWB = oApp.Workbooks.Add();
oSheet =(Excel.Worksheet)oWB.Worksheets.get_Item(1);
尝试
{
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace(" MAPI");
ns.Logon("","",false,Missing.Value);
inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
subFolder = inboxFolder.Folders [" Octavian"]; //folder.Folders[1];也工作
Console.WriteLine(" Folder Name:{0},EntryId:{1}",subFolder.Name,subFolder.EntryID);
Console.WriteLine(" Num Items:{0}",subFolder.Items.Count.ToString());
for(int i = 1; i< = subFolder.Items.Count; i ++)
{
Microsoft.Office.Interop.Outlook.MailItem item =(Microsoft。 Office.Interop.Outlook.MailItem)subFolder.Items [I];
oSheet.Cells [i,1] = i.ToString();
oSheet.Cells [i,2] = item.Subject;
oSheet.Cells [i,3] = item.SentOn.ToLongDateString();
oSheet.Cells [i,4] = item.Body.ToString();
}
}
catch(System.Runtime.InteropServices.COMException ex)
{
Console.WriteLine(ex.ToString());
}
最后
{
oWB.SaveAs(fileTest);
oWB.Close();
oApp.Quit();
oApp = null;
ns = null;
inboxFolder = null;
app = null;
只是一个提示.... 。
MailItem对象有一个有效的HTMLBody。它将以HTML格式返回电子邮件正文。
获取HTML后,您可以使用HTML DOM对象或任何其他HTML解析器进行解析。
Hi ,
I`m trying to parse outlook body e-mails from a subfolder of inbox.The e-mail body in some cases contains a table or just text.I`m learning how to do it with html agility pack, but i have no clue how to loop thru e-mails.For example from an e-mail that contains a table i want to write every cell of the table in a new column of excel.
For know i have the code below but it`s just writes the body of the e-mail to a cell.
Please give some hints , any help.
Thank`s.
Microsoft.Office.Interop.Outlook.Application app = null; Microsoft.Office.Interop.Outlook.NameSpace ns = null; Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null; Excel.Application oApp=null; Excel.Workbook oWB=null; Excel.Worksheet oSheet=null; string fileTest = "C:\\Users\\Daniel\\Desktop\\test.xlsx"; if (File.Exists(fileTest)) { File.Delete(fileTest); } oApp = new Excel.Application(); oWB = oApp.Workbooks.Add(); oSheet = (Excel.Worksheet)oWB.Worksheets.get_Item(1); try { app = new Microsoft.Office.Interop.Outlook.Application(); ns = app.GetNamespace("MAPI"); ns.Logon("", "",false,Missing.Value); inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); subFolder = inboxFolder.Folders["Octavian"]; //folder.Folders[1]; also works Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID); Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString()); for (int i = 1; i <= subFolder.Items.Count; i++) { Microsoft.Office.Interop.Outlook.MailItem item = (Microsoft.Office.Interop.Outlook.MailItem)subFolder.Items[i]; oSheet.Cells[i, 1] = i.ToString(); oSheet.Cells[i, 2] = item.Subject; oSheet.Cells[i, 3] = item.SentOn.ToLongDateString(); oSheet.Cells[i, 4] = item.Body.ToString(); } } catch (System.Runtime.InteropServices.COMException ex) { Console.WriteLine(ex.ToString()); } finally { oWB.SaveAs(fileTest); oWB.Close(); oApp.Quit(); oApp = null; ns = null; inboxFolder = null; app = null;
Just a hint.....
The MailItem object has a propery HTMLBody. It will return body of email in HTML format.
After getting the HTML you can parse with HTML DOM object or any other HTML Parser.
这篇关于如何解析outlook body电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!