outlook新的电子邮件到Excel行

我想创build一个C#控制台应用程序(或一个服务 – 不知道如何开发一个服务):

1)在收件箱> LOTALogs文件夹中收到新电子邮件时知道。 此电子邮件由移动应用程序发送,包含附件和客户遇到的一些问题。

2)使用逗号分隔的新电子邮件内容parsing附加内容到已设置列的Excel工作表中。

我设法创造:

1)parsing器:

public static string[] emailContentsArray() { string content = "Username = Customer1,User ID = 362592,Unit ID = 805618,Date = Mar 12, 2017,Device = Android LGE LG-H990,OS version = 7.0 (API 24),App version = 1.0.0.56,Description = some description,Message = some message"; string[] contentArray = content.Split(','); // Case where date format includes another comma if (contentArray.Length > 10) { // Parsing headers contentArray[0] = contentArray[0].Substring(11); contentArray[1] = contentArray[1].Substring(10); contentArray[2] = contentArray[2].Substring(10); contentArray[3] = contentArray[3].Substring(7) + ", " + contentArray[4].Substring(1); contentArray[4] = contentArray[5].Substring(9); contentArray[5] = contentArray[6].Substring(13); contentArray[6] = contentArray[7].Substring(14); contentArray[7] = contentArray[8].Substring(14); contentArray[8] = contentArray[9].Substring(10); contentArray[9] = null; for (int i = 0; i < contentArray.Length; i++) { Console.Write(contentArray[i] + ","); } } //else //{ //} return contentArray; } 

2)访问文件夹并计算项目数量:

  public static string[] emailContent() { string[] content = null; 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 logFolder = null; app = new Microsoft.Office.Interop.Outlook.Application(); ns = app.GetNamespace("MAPI"); inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); logFolder = app.ActiveExplorer().CurrentFolder = inboxFolder.Folders["LOTALogs"]; int itemCount = logFolder.Items.Count; Console.WriteLine("\n\nFolder Name: {0}, Num Items: {1}\n", logFolder.Name, itemCount); return content; } 

3)打开并打印电子表格的内容:

  Excel.Application xlApp = new Excel.Application(); string path = "C:\\SomeUser\\BugReports"; Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@path); Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; Excel.Range xlRange = xlWorksheet.UsedRange; for (int i = 1; i <= xlRange.Row + xlRange.Rows.Count - 1; i++) { for (int j = 1; j <= xlRange.Column + xlRange.Columns.Count - 1; j++) { if (j == 1) Console.Write("\r\n"); if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null) Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t"); } } xlWorkbook.Save(); xlWorkbook.Close(); xlApp.Quit(); Console.ReadLine(); 

我现在有点迷路了:)

我仍然需要:

1)创build一个事件监听器(我认为这就是所谓的),所以我可以告诉电子邮件主体分析器去获取电子邮件内容。

2)从电子邮件中提取电子邮件正文。

得到这个使用

  Console.WriteLine(logFolder.Items[1].Body); 

3)把电子邮件的内容,并将其附加到电子表格。

4)我应该创build这个Windows服务?

PS – 我不是一个开发人员,只是摆弄代码,尽量提高效率。 当我们看到技术解决scheme时,我不想手动填写这个电子表格。 如果您对代码效率更高,build议的方式有所不同,请发表评论意见。

看起来对我坚实。 我会避免使用这项服务,但这很大程度上取决于您的用户。 除非你的客户真的想在整个过程中“失明”,否则会增加许多不必要的麻烦。

至于附加到电子表格…

 int lastRow = xlWorksheet.UsedRange.Rows; Excel.Range xlRange = xlWorksheet.Cells[lastRow + 1, 1]; xlRange.Value = stuffFromInbox; 

如果您只是将一个项目添加到电子表格,这将工作正常。 对于使用电子表格进行批量读/写操作(如“打开并打印电子表格的内容”),将整个RangeValueValue读入object[,]将会更有效率。 然后遍历本地数组。