Click here to Skip to main content
11,720,321 members (80,368 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET Outlook AddIn

I want to save a copy of email in C:\Temp folder as soon as a new mail arrive in inbox.

For this i used outlook addin in Visual studio,

Created NewMailEx method.
I am able to get message box when new mail arrive

PROBLEM: I am not able to copy mail to specified path

Below code will give more idea.

public partial class ThisAddIn
        private Outlook.Explorers _Explorers;  // the Outlook Explorers collection
        private Outlook.Inspectors _Inspectors;  // the Outlook Inspectors collection
        private Outlook.NameSpace outlookNamespace;
        private Outlook.Application outlookObj;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
            _Explorers = this.Application.Explorers;
            _Inspectors = this.Application.Inspectors;
            _Explorers.Application.NewMailEx += new
            //  this.Application.ItemLoad +=
            //new Outlook.ApplicationEvents_11_ItemLoadEventHandler(GetMailItemLocation);

            outlookNamespace = this.Application.GetNamespace("MAPI");
        private void Application_NewMailEx(string EntryID)
            Outlook.MailItem newMail = (Outlook.MailItem)_Explorers.Application.Session.GetItemFromID(
                EntryID, System.Reflection.Missing.Value);
            string Path = @"C:\Temp\" + newMail.Subject + ".msg";
            //Alternate way
            string uPath = @"C:\Temp\";
            newMail.SaveAs(Path, Outlook.OlSaveAsType.olMSG);
            newMail.SaveAs(uPath, Outlook.OlSaveAsType.olMSG);
            if (newMail.Subject != null)
                MessageBox.Show("From: " + newMail.SenderEmailAddress + "\nSubject: " +
                    newMail.Subject, "New Email", MessageBoxButtons.OK);
                MessageBox.Show("You've got mail.");
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
            _Explorers = null;
            _Inspectors = null;
        private void InternalStartup()
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
Posted 2-Aug-12 11:54am
Aditya Mangipudi at 3-Aug-12 10:32am
I am not sure if it would be a helpful comment but did you try saving it to different location NOT c:\temp. The reason i mentioned this is because, in some operating systems, you need to be an admin to write to c: drive.I would also recommend running the IDE as an admin.
Vineet_2109 at 4-Aug-12 8:19am
Thanks for your time and reply..!!
Very next day i tried to run the code and it was working.. seems some magic happened in night.. ;) hehehe..

Thanks once again..!!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Same code worked..
Nothing need to be changes

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,029
1 OriginalGriff 844
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 4 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100