Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#.NETOutlookAddIn
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 11:54am
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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 450
1 Sergey Alexandrovich Kryukov 325
2 Shai Vashdi 318
3 OriginalGriff 265
4 Peter Leow 171
0 Sergey Alexandrovich Kryukov 9,109
1 OriginalGriff 5,290
2 Peter Leow 4,010
3 Maciej Los 3,535
4 Abhinav S 3,263


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