Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

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
                Outlook.ApplicationEvents_11_NewMailExEventHandler(Application_NewMailEx);

            //  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);
            }
            else
            {
                MessageBox.Show("You've got mail.");
            }
        }
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
            _Explorers = null;
            _Inspectors = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }

        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
Posted
Comments
Aditya Mangipudi 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 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

Same code worked..
Nothing need to be changes
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900