C# 优化Outlook加载项中搜索电子邮件的代码
我有下一个将特定电子邮件信息打印到Excel文件的解决方案。我选择在日期之前必须写哪些电子邮件C# 优化Outlook加载项中搜索电子邮件的代码,c#,outlook,C#,Outlook,我有下一个将特定电子邮件信息打印到Excel文件的解决方案。我选择在日期之前必须写哪些电子邮件 [STAThread] public void Summary(DateTime startDate, DateTime finishDate, string fileSaveAdress) { try { Outlook.Application oApp = new Outlook.Application();
[STAThread]
public void Summary(DateTime startDate, DateTime finishDate, string fileSaveAdress)
{
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNs = oApp.GetNamespace("mapi");
oNs.Logon(Missing.Value, Missing.Value, false, false);
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(Missing.Value);
Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Outlook.MAPIFolder pickedFolder = oApp.Session.PickFolder();
Outlook.Items pickedFolderItems = pickedFolder.Items;
int iX = 1;
int iY = 1;
int iAdjustColomn = 0;
foreach (object obj in pickedFolderItems)
{
if (obj is Outlook.MailItem)
{
Outlook.MailItem oMsg = (Outlook.MailItem)obj;
if (oMsg.ReceivedTime.ToUniversalTime() > startDate.ToUniversalTime() &&
oMsg.ReceivedTime.ToUniversalTime() < finishDate.ToUniversalTime())
{
xlWorkSheet.Cells[iY, iX] = oMsg.ReceivedTime.ToShortDateString() + " " +
oMsg.ReceivedTime.ToShortTimeString();
xlWorkSheet.Cells[iY, ++iX] = oMsg.Sender.Name;
for (int i = 1; i <= oMsg.Recipients.Count; i++)
{
xlWorkSheet.Cells[iY, ++iX] = oMsg.Recipients[i].Name;
}
xlWorkSheet.Cells[iY, ++iX] = oMsg.Subject;
Outlook.Attachments AttachmentArray = oMsg.Attachments;
if (AttachmentArray.Count != 0)
{
foreach (Outlook.Attachment attachment in AttachmentArray)
{
xlWorkSheet.Cells[iY, ++iX] = attachment.DisplayName;
}
}
iAdjustColomn += iX;
iY += 2;
iX = 1;
}
}
}
for (int i = 1; i < iAdjustColomn; i++)
{
AutoFitColumn(xlWorkSheet, i);
}
xlWorkBook.SaveAs(fileSaveAdress, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlWorkBook.Close(true, Missing.Value, Missing.Value);
xlApp.Quit();
oNs.Logoff();
}
catch (Exception e)
{
ErrorReport er = new ErrorReport(e.Message);
er.ShowDialog();
}
}
[STAThread]
公共作废摘要(DateTime startDate、DateTime finishDate、字符串文件存储地址)
{
尝试
{
Outlook.Application oApp=新建Outlook.Application();
Outlook.NameSpace-oNs=oApp.GetNamespace(“mapi”);
oNs.Logon(Missing.Value,Missing.Value,false,false);
Excel.Application xlApp=新的Excel.Application();
Excel.Workbook xlWorkBook=xlApp.Workbooks.Add(缺少.Value);
Excel.Worksheet xlWorkSheet=(Excel.Worksheet)xlWorkBook.Worksheets.get_项(1);
Outlook.MapipFolder pickedFolder=oApp.Session.PickFolder();
Outlook.Items pickedFolderItems=pickedFolder.Items;
int iX=1;
int-iY=1;
int=0;
foreach(pickedFolderItems中的对象对象)
{
if(obj是Outlook.MailItem)
{
Outlook.MailItem oMsg=(Outlook.MailItem)obj;
如果(oMsg.ReceivedTime.ToUniversalTime()>startDate.ToUniversalTime())&&
oMsg.ReceivedTime.ToUniversalTime() for(int i=1;i我将使用计数器将使用Outlook对象的foreach调用替换为for循环。对于Outlook数据,使用foreach通常是一种不好的做法
另外,一种更快的方法是将表对象与Folder.GetTable一起使用,您可以在其中设置迭代中要检索的列/字段的最小数量,以最小化内存使用
来自Microsoft()的示例:
我真的不确定它是否能解决问题。也许从Outlook本身筛选任何其他通用集合中的邮件会更有用。我的意思是先复制一份。我真的不确定内存膨胀可能发生在哪里,但您应该始终使用for语句,而不是foreach处理Outlook对象。另外,一种更快的方法是将表对象与Folder.GetTable一起使用,您可以在其中设置迭代要检索的列/字段的最小数量,以最大限度地减少内存使用。谢谢。我将在我的项目中使用您的建议。它非常有用!
Sub RemoveAllAndAddColumns()
'Declarations
Dim Filter As String
Dim oRow As Outlook.Row
Dim oTable As Outlook.Table
Dim oFolder As Outlook.Folder
'Get a Folder object for the Inbox
Set oFolder = Application.Session.GetDefaultFolder(olFolderInbox)
'Define Filter to obtain items last modified after May 1, 2005
Filter = "[LastModificationTime] > '5/1/2005'"
'Restrict with Filter
Set oTable = oFolder.GetTable(Filter)
'Remove all columns in the default column set
oTable.Columns.RemoveAll
'Specify desired properties
With oTable.Columns
.Add ("Subject")
.Add ("LastModificationTime")
'PR_ATTR_HIDDEN referenced by the MAPI proptag namespace
.Add ("http://schemas.microsoft.com/mapi/proptag/0x10F4000B")
End With
'Enumerate the table using test for EndOfTable
Do Until (oTable.EndOfTable)
Set oRow = oTable.GetNextRow()
Debug.Print (oRow("Subject"))
Debug.Print (oRow("LastModificationTime"))
Debug.Print (oRow("http://schemas.microsoft.com/mapi/proptag/0x10F4000B"))
Loop
End Sub