从当前电子邮件获取附件

时间:2015-01-12 10:19:20

标签: c# visual-studio add-in outlook-2013

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

这个动作有什么例子吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

首先,您需要获取当前邮件项的对象。之后,您可以循环浏览邮件项的.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);
}