Search code examples
c#outlookoffice-interopmsg

How to save a sent email immediately after Send?


I have a seperate .Net application from Outlook. I would like to use Office.Interop (avoid redmeption.dll) to send and save the sent email from that application. But when I try to Save the email after Send() function, I got the exception that : "The item has been moved or deleted."

It seems Outlook is moved the email item after sent.

so, can anyone suggest me the best ways how to save the "Sent" email item to disk ?

I need the saved msg file have status 'sent'. so, saving Item_Send event is not working for my case. Thank you !

Here is my codes

     public bool SendAndSavAs(Outlook.Account emailAccount, string toEmailAddress)
    {
        Outlook.MailItem mailItem = null;
        try
        {
            mailItem = OutlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            mailItem.Subject = "Test Subject";
            mailItem.BodyFormat = Outlook.OlBodyFormat.olFormatPlain;
            mailItem.Body = "Test Body";

            // Add recipient using display name, alias, or smtp address
            mailItem.Recipients.Add(toEmailAddress);
            mailItem.Recipients.ResolveAll();
            mailItem.SendUsingAccount = emailAccount;

            // send email
            mailItem.Send();

            // save the Sent mail to local disk  (****but have exception occurs ****)
            mailItem.SaveAs(@"c:\temp\test.msg");

            return true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            ReleaseComObject(mailItem);
        }
    }

Solution

  • You need to handle the ItemAdd event on the Sent Items folder. In the event handler you can call the SaveAs method.

    Also you may consider using the SaveSentMessageFolder property of the MailItem class which allows to set a Folder object that represents the folder in which a copy of the e-mail message will be saved after being sent.