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!
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
Hi Grin,
Could you please tell me what exactly you are trying to do so that I can try to help you out?
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 Grim,
I have updated the post with sample code. Try it and please let me know whether it worked for your purpose or not.
You might have read the reply from Sue in MSDN Forum. MSG is not a good backup format.
http://blogs.msdn.com/b/stephen_griffin/archive/2008/01/08/no-msg-for-you.aspx
Instead of this you can use EML files for backup. Let me know if your are interested in this. Will share you more info regarding it.
Happy Coding!
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.
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.
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
Hi Vick,
Yes. Its just a simple logic which fetches the last item in the collection. It worked for me in both Outlook 2003 and 2007.
Can I get this code in C#