c#outlook get attachement

时间:2016-12-21 08:44:01

标签: c# email outlook attachment

为了减少公司的邮件流量,我正在为Outlook开发一个自定义功能区加载项。

这应该自动打开文件,以便他们的接收者知道他必须保存它们的位置。因为它是一个特定的程序。

如何获取特定邮件附件中的文件“路径”(双击邮件打开的文件)?

我有一个带有自定义按钮的功能区。但不能继续,因为我首先需要attachemnts。

arr.push(config[field].map(cb => cb(formData[field], formData)))

1 个答案:

答案 0 :(得分:0)

here

private void SaveMailAttachments(Outlook.MailItem mailItem)
{
    Outlook.Attachments attachments = mailItem.Attachments;
    if (attachments != null && attachments.Count > 0)
    {
        for (int i = 1; i <= attachments.Count; i++)
        {
            Outlook.Attachment attachment = attachments[i];
            if (attachment.Type == Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue)
            {
                string filename = Path.Combine(@"d:\", attachment.FileName);
                attachment.SaveAsFile(filename);
            }
        }
    }
}

here

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    this.Application.NewMail += new Microsoft.Office.Interop.Outlook
        .ApplicationEvents_11_NewMailEventHandler(ThisApplication_NewMail);
}

private void ThisApplication_NewMail()
{
    Outlook.MAPIFolder inBox = this.Application.ActiveExplorer()
        .Session.GetDefaultFolder(Outlook
        .OlDefaultFolders.olFolderInbox);
    Outlook.Items inBoxItems = inBox.Items;
    Outlook.MailItem newEmail = null;
    inBoxItems = inBoxItems.Restrict("[Unread] = true");
    try
    {
        foreach (object collectionItem in inBoxItems)
        {
            newEmail = collectionItem as Outlook.MailItem;
            if (newEmail != null)
            {
                if (newEmail.Attachments.Count > 0)
                {
                    for (int i = 1; i <= newEmail
                        .Attachments.Count; i++)
                    {
                        newEmail.Attachments[i].SaveAsFile
                            (@"C:\TestFileSave\" +
                            newEmail.Attachments[i].FileName);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        string errorInfo = (string)ex.Message
            .Substring(0, 11);
        if (errorInfo == "Cannot save")
        {
            MessageBox.Show(@"Create Folder C:\TestFileSave");
        }
    }
}