Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET Outlook AddIn
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 2-Aug-12 12:54pm
Comments
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
good
Please Sign up or sign in to vote.

Solution 1

Same code worked..
Nothing need to be changes
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 4 Aug 2012
Copyright © CodeProject, 1999-2014
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