从Outlook Compose窗口中拾取附件

时间:2016-07-26 15:15:04

标签: c# outlook-addin

我的Addin在New Mail Window中打开,这是通过Outlook以及MAPI或mailTo打开的。现在用户可以使用此邮件添加附件,但我通过代码发送邮件(是我的要求),现在我尝试从当前打开的新邮件窗口中选择附件: -

private Outlook.Attachments GetAttachments()
{
    Outlook.Inspector inspector = this.inspectorWrapper.Inspector;
    Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
    var attachments = myMailItem.Attachments;
    return attachments;
}

然后在我的发送方法中,我尝试以这种方式访问​​它: -

for (int i = 0; i <= attachments.Count; i++)
{
    eMail.Attachments.Add(attachments[i].PathName, Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue, 1, attachments[i].FileName);
}

附件是通过上述方法返回的相同对象,它确实显示了正确的附件计数(例如,在我的示例中为2)。

但它会抛出一个&#34;索引超出数组错误的范围&#34;当我尝试在上面的代码片段中访问thistatachments [i]时。 不知道为什么会这样。

非常感谢任何帮助

修改

我打开了撰写窗口,手动或从excel等附加文件,然后我尝试以这种方式从当前的新邮件项目检查器中获取附件: -

private Outlook.Attachments GetAttachments()
{
    Outlook.Inspector inspector = this.inspectorWrapper.Inspector;
    Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;
    var attachments = myMailItem.Attachments;
    return attachments;
}

当我附加两个文件时,我的附件计数为2,当我试图访问它时,名称也在那里附件[1] .FileName但路径将变为空。 如何将这些附件保存到我试图以编程方式发送的邮件中?

1 个答案:

答案 0 :(得分:0)

Outlook中的所有集合(包括附件)都是基于1而不是0:

for (int i = 1; i <= attachments.Count; i++)