从当前电子邮件获取附件

本文关键字:获取 电子邮件 | 更新日期: 2023-09-27 18:37:08

我正在使用 C# 和 Visual Studio 2012 创建一个 Outlook 2013 附加组件,我想在单击按钮时从当前(打开的)邮件窗口中获取附件文件并将其保存到本地目录。

此操作是否有任何示例?

谢谢。

从当前电子邮件获取附件

首先,您需要获取当前邮件项目的对象。之后,您可以循环浏览邮件项目的.Attachments并使用.SaveAsFile(filePath)保存它们。

var _thisApp = this.Application;
Outlook.MailItem _email;
// Get current email
if(_thisApp.ActiveWindow() is Outlook.Inspector)
{
    Outlook.Inspector insp = _thisApp.ActiveWindow() as Outlook.Inspector;
    _email = insp.CurrentItem as Outlook.MailItem;
}
else if(_thisApp.AcitveWindow() is Outlook.Explorer)
{
    Outlook.Explorer exp = _thisApp.ActiveExplorer();
    if(exp.Selection.Count > 0)
        _email = exp.Selection[1] as Outlook.MailItem;
}
// Loop through the attachments
foreach(Outlook.Attachment attachment in _email.Attachments)
{
    // Some other stuff
    string filePath = @"C:'Saved Attachments'" + attachment.FileName;
    attachment.SaveAsFile(filePath);
}

编辑:检索此示例的示例。应用

private Outlook.Application _thisApp;
private Outlook.Inspectors _inspectors;
// Function in ThisAddin.cs 
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    _thisApp = this.Application;
    _inspectors = _thisApp.Inspectors;
    _inspectors.NewInspector +=
    new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
}