3
votes

I am new 2 C# and I have been given a task... I have to write a C# code to download the sent email attachments and subject of email from Outlook 2007 to a local drive or any specified location. How do I do that? I am able to get the attachments which are in inbox. Can any one please help me getting the mails sent through 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");
        }
    }
}

Thanks in Advance

2

2 Answers

7
votes

I've tested your code, the SaveAsFile() method of Attachment class instance throws:

{System.IO.DirectoryNotFoundException: Cannot save the attachment. Path does not exist. Verify the path is correct. at Microsoft.Office.Interop.Outlook.Attachment.SaveAsFile(String Path) ... }

So, it is necessary to make sure that the directory for saving attachments exists.

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);
    }
}

Hope this helps.

0
votes

Maintain a list which will store the sender's name then check a condition that the recent mail's sender contains in the list if yes than Download.

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);
    }
}