Saving mail content when a mail is sent from Outlook

To save the content of a mail item when it is sent, here is the procedure.

Private WithEvents itms As Outlook.Items

Private WithEvents SntFldr As Outlook.Folder

SntFldr = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)

itms = SntFldr.Items

Private Sub SaveMail() Handles itms.ItemAdd

‘ DO SOMETHING

End Sub

Before, I handled Application.Send Event and checked whether the Item is MailItem or not and then retrieved the content of that. But, with this, we couldn’t get the Date/Time and sending account information. So, opted for the above procedure.

UPDATE: Here is the updated sample code.

Private WithEvents SntFldr As Outlook.Folder
Private WithEvents itms As Outlook.Items

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    SntFldr = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail)
    itms = SntFldr.Items
End Sub
Private Sub SaveMailAsMsg() Handles itms.ItemAdd
    If TypeOf itms.Item(itms.Count) Is Outlook.MailItem Then
        Dim olMail As Outlook.MailItem = itms.Item(itms.Count)
        MessageBox.Show("Item Added. " & vbCrLf & "Subject: " & olMail.Subject & vbCrLf & "From: " & olMail.SenderEmailAddress)
    End If
End Sub

Hope this helps!

About these ads
This entry was posted in Outlook, VSTO and tagged , , by Pranav. Bookmark the permalink.

About Pranav

A technology enthusiast and .NET Geek who loves to experiment cutting edge features. Basically a .NET Developer whose expertise includes VSTO, Windows Mobile, Windows Apps, ASP.NET, Windows Phone 7, jQuery, Metro Apps etc.

15 thoughts on “Saving mail content when a mail is sent from Outlook

  1. Hey,

    Can you please add more detail on the section above. I have been looking for something to save a copy of a sent email and cant find anything. This is the closest but dont fully understand it.

    Thanks

  2. I have a vsto add in that when i clich on a file on my inbox i csn save it to a .msg.

    iwant to be able to do the same bit as soon as i send an email i want that email to be saved on my pc in a directory as aj .msg file.

    i have been looking for a solution for months an no one can help ot would be beyond awesome if you had a solution.

    many thanks

      • Hi,

        Still having trouble to do what i am trying to get to. Your code works fine with the send event and pops up with the MsgBox.

        What I have done in my little app is to have a dialog box pop up asking the user do you want to save this email. Yes/No. If Yes is clicked then a new dialog opens where the user can select a location to save the email. (I will look in alternative saving formats later, thanks for the link on that)

        So, when I have an item selected its easy because the “Selected Item” is the active item to save, but when you send one the selected item is still the item that has been selected last.

        So in my Add_In on start I have a command bar that is added with a single button. If clicked you can save a selected item. On the send event it brings up the dialog in the send even.

        But then when I get to the Actual dialog to save the email in my location and trying to Identify the last sent item (or item that has just been sent) is what I cant figure out how to do. :-(

        Thanks again for the help!!! Apprecaite it a lot.

  3. That’s what I have done in the code. itms.Item(itms.Count) gives you the last (latest) item in the Sent Items Folder.

    You just comment MessageBox line and write what ever you want. It will server your purpose.

  4. Hi Pranav

    You itms.Item(itms.Count) gives you the last item in the sent items folder. Is this same in Outlook 2003 Standard Ed SP3 and Outlook 2003 Professional Ed Sp2?

    Thanks
    Vick

  5. Hi Pranav

    It seems your solution is very close to what we need, maybe you can help us find exactly what we are looking for?
    Basically we have an app that opens outlooks default mail editor to let the user type the message but after (or during) the “Send” button is clicked we want to discover the subject line and message body that the user entered to store it in a database for a history record. Can you tell us how this can be done?

    thanks

    Steve

    • Hi Steve,

      You can use ItemSend Event as well. This will fire when an item is sent. Here is the sample code…
      private void ThisAddIn_Startup(object sender, System.EventArgs e)
      {
      this.Application.ItemSend += new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
      }

      void Application_ItemSend(object Item, ref bool Cancel)
      {
      if (Item is Outlook.MailItem)
      {
      Outlook.MailItem mail = (Outlook.MailItem)Item;
      ‘ You can get all the attributes of mail from the mail object
      }
      }

      But with the above code, we are not sure that the mail is successfully sent or failed. That’s why I have done my work once it is added to ‘Sent Items’ folder.

  6. Hi Pranav

    Thank you, in fact I have now got your code to do exactly (almost) what i need. It works great! But what if the message being sent has an image embedded in it? how can I include the image in the saved message body?

    thanks

    Steve

  7. Hi again

    I think I have also sorted out the picture question but I now have 3 more questions that I hope you can help with.

    1) In your example code when the message is displayed I get senders user name instead of the actual email address, how do I get to see the actual emaill address?
    2) Can you please add the correct lines to display the list of recipients email addresses in the message too?
    3) What if there is an attachment(s) how do I get their names?

    Thanks again

    Steve

    • 1) Is it an exchange mail box? If so, you have to get it from the AddressEntry. Have a look at this article – http://wp.me/pvaar-41

      2) For getting recipients of a Mail Item, you need to iterate through the Recipients object of it. Have a look at this – http://wp.me/pvaar-5Z

      3) objMailItem.Attachments gives you Outlook.Attachments

      For i As Integer = 1 To objMailItem.Attachments.Count
      Dim saveAttachment As Outlook.Attachment = objMailItem.Attachments(i)
      ‘ saving attachment to local folder
      objMailItem.Attachments(i).SaveAsFile(“C:\MyFolder\” & _
      (objMailItem.Attachments(i).FileName))
      Next i

      objMailItem.Attachments(i).FileName – This will give you the name of the attachment

      Hope this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s