从Outlook Compose窗口拾取附件

本文关键字:窗口 Outlook Compose | 更新日期: 2023-09-27 17:58:46

"我的加载项"会在"新邮件窗口"中打开,它是通过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);
}

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

但是,当我尝试访问上面代码片段中的thisapattachments[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中,但路径将为null。如何将这些附件保存到我试图按语法发送的邮件中?

从Outlook Compose窗口拾取附件

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

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