2015-05-06 108 views
0

我正在使用Outlook.Application和Outlook.MailItem对象在我的C#桌面应用程序中打开Outlook。虽然当我向自己发送邮件时,我的Outlook未显示附件,但我收到带有附件的邮件。但是在发送邮件之前(outlook打开时)不显示。我使用Outlook 2007的下面是我的代码:Outlook未显示附件

Outlook.Application oApp = new Outlook.Application(); 
Outlook.NameSpace oNS = oApp.GetNamespace("mapi"); 
// Log on by using a dialog box to choose the profile. 
oNS.Logon(Missing.Value, Missing.Value, true, true); 


// Create a new mail item. 
     Outlook.MailItem oMsg =   (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
...... 
//Check if we need to add attachments 
if (_files.Count > 0) 
{ 
    foreach (string attachment in _files) 
    { 
      oMsg.Attachments.Add(attachment,Outlook.OlAttachmentType.olByValue,null,null); 
    } 
} 

oMsg.Save(); 
oMsg.Display(false); 
+0

这将是很好的格式,这更清楚。我会自己做,但编辑按钮变灰。 –

+0

代码现在已格式化。谢谢。 – Awadesh

+0

我能够通过将附件行替换为以下来解决上述问题:oMsg.Attachments.Add(attachment,Outlook.OlAttachmentType.olByValue,Type.Missing,Type.Missing); – Awadesh

回答

0

当然,Type.Missing用于省略该参数,并使用COM的默认值加载项。

此外,我会建议打破调用链并声明每个属性或方法调用在单独的代码行上。它将允许立即释放每个底层的COM对象。使用System.Runtime.InteropServices.Marshal.ReleaseComObject在完成使用后释放Outlook对象。如果您的加载项尝试枚举存储在Microsoft Exchange Server上的集合中的超过256个Outlook项目,这一点尤其重要。如果您没有及时释放这些物品,您可以达到Exchange对任何时候打开的物品的最大数量施加的限制。然后在Visual Basic中将变量设置为Nothing(C#中的空值)以释放对该对象的引用。您可以在MSDN的Systematically Releasing Objects文章中阅读更多。