2012-02-28 122 views
-1

嗨,我想获得Outlook发送的附件和收件人主题的邮件发送......我能够通过Outlook的默认文件夹得到它。 如何在发送邮件之前检索由VSTO发送的邮件。展望发送邮件

现在我这样做

namespace OutlookAddInAttachment 
{ 
    public partial class ThisAddIn 
    { 
     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 SentMail = this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox); 
     Outlook.Items SentMailItems = SentMail.Items; 
     Outlook.MailItem newEmail = null; 
     //SentMailItems = SentMailItems.Restrict("[Unread] = true"); 
     try 
     { 
      foreach (object collectionItem in SentMailItems) 
      { 
       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"); 
      } 
     } 
    } 

回答

1

我不知道你想在这里做什么。

您正在监听NewMail事件,该事件在INBOX中侦听新收到的邮件,然后扫描已发送邮件文件夹,根据定义它只包含已发送的邮件。

如果你想要做的是拦截发送新邮件的attachemnts,你需要的是ItemSend事件,这将让你赶上它在行动前,它发出:

public partial class ThisAddIn 
{ 

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

private void ThisApplication_ItemSend(object item, bool cancel) 
{ 
    Outlook.MailItem newEmail = item as MailItem; 
    if (newEmail != null) 
    { 
     foreach (var attachment in newEmail.Attachments) 
     { 
      attachment.SaveAsFile(@"C:\TestFileSave\" + attachment.FileName); 
     } 
    } 
} 

}