2012-02-18 90 views
0

我是新的2 C#和我已經被賦予了一項任務...我必須編寫一個C#代碼,將發送的電子郵件附件和電子郵件從Outlook 2007的主題下載到本地驅動器或任何指定的位置。我怎麼做?我能夠獲取收件箱中的附件。任何人都可以幫助我獲得通過Outlook發送的郵件嗎?從Outlook獲取附件

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.olFolderSentMail); 
    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"); 
     } 
    } 
} 

由於提前

回答

5

我測試你的代碼,Attachment類實例的SaveAsFile()方法拋出:

{System.IO.DirectoryNotFoundException:無法保存附件。 路徑不存在。驗證路徑是否正確。在 Microsoft.Office.Interop.Outlook.Attachment.SaveAsFile(字符串路徑) ...}

因此,有必要確保用於保存附件的目錄存在。

private void ThisApplication_NewMail() 
{ 
    const string destinationDirectory = @"C:\TestFileSave"; 

    if (!Directory.Exists(destinationDirectory)) 
    { 
     Directory.CreateDirectory(destinationDirectory); 
    } 

    MAPIFolder sentMail = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail); 
    Items sentMailItems = sentMail.Items; 
    try 
    { 
     foreach (object collectionItem in sentMailItems) 
     { 
      MailItem newEmail = collectionItem as MailItem; 
      if (newEmail == null) continue; 

      if (newEmail.Attachments.Count > 0) 
      { 
       for (int i = 1; i <= newEmail.Attachments.Count; i++) 
       { 
        string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName); 
        newEmail.Attachments[i].SaveAsFile(filePath); 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex); 
    } 
} 

希望這會有所幫助。

+1

謝謝塞爾:-) – 2012-02-19 06:12:57

+0

,如果我想在收件箱中選擇,從電子郵件附件? – SANDEEP 2013-05-16 06:35:23

0

維護一個將存儲發件人姓名的列表,然後檢查最近郵件發件人在列表中是否包含下載的條件。

private void ThisApplication_NewMail() 
{ 
    const string destinationDirectory = @"C:\TestFileSave"; 
    List<string> senderList = new List<string>(); 
    if (!Directory.Exists(destinationDirectory)) 
    { 
     Directory.CreateDirectory(destinationDirectory); 
    } 

    MAPIFolder sentMail = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderSentMail); 
    Items sentMailItems = sentMail.Items; 
    try 
    { 
     foreach (object collectionItem in sentMailItems) 
     { 
      MailItem newEmail = collectionItem as MailItem; 
      senderEmailAdd = newEmail.SenderEmailAddress; 
      if (newEmail == null) continue; 

      if (newEmail.Attachments.Count > 0 && senderList.Contains(senderEmailAdd)) 
      { 
       for (int i = 1; i <= newEmail.Attachments.Count; i++) 
       { 
        string filePath = Path.Combine(destinationDirectory, newEmail.Attachments[i].FileName); 
        newEmail.Attachments[i].SaveAsFile(filePath); 
       } 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex); 
    } 
} 
相關問題