How To: Respond to an Outlook email programmatically
Several Outlook item types provide the Forward, Reply and ReplyAll methods. The table below lists the item types providing these methods as well as the item types that the methods return.
Item type | Reply() | ReplyAll() | Forward() |
JournalItem | MailItem | MailItem | MailItem |
MailItem | MailItem | MailItem | MailItem |
MeetingItem | MailItem | MailItem | MeetingItem |
MobileItem (Outlook 2010 only) | MobileItem | MobileItem | MobileItem |
PostItem | MailItem | N/A | MailItem |
SharingItem (Outlook 2007 -2010 only) | MailItem | MailItem | SharingItem |
Let’s see how to use the information above to respond to a MailItem. In the code below, MailItem.Display is used for a demo; you can also call MailItem.Send.
The simplest way
Here’s one of the options of creating a reply to an email:
Imports System.Runtime.InteropServices Imports Outlook = Microsoft.Office.Interop.Outlook ... Private Sub Reply(mailItem As Outlook._MailItem) Dim response As Outlook._MailItem = mailItem.Reply() response.Display() Marshal.ReleaseComObject(response) End Sub
using System.Runtime.InteropServices; using Outlook = Microsoft.Office.Interop.Outlook; ... private void Reply(Outlook._MailItem mailItem) { Outlook._MailItem response = mailItem.Reply(); response.Display(Type.Missing); Marshal.ReleaseComObject(response); }
The method above gets a response mail item from MailItem.Reply, calls MailItem.Display to show the response and releases the COM object by calling Marshal.ReleaseComObject. The need for the last call is discussed in When to release COM objects in a managed COM add-in?
Problems & solutions
There are problems with the mail items that are created using Forward, Reply and ReplyAll methods. Say, when using these methods, the “Close original message on reply or forward” flag doesn’t apply. This seems to be rational, however. If you need to emulate this behavior, you’ll need to read the value called CloseOrig in the following registry key:
HKEY_CURRENT_USER\Software\Microsoft\Office\{version}.0\Outlook\Preferences.
We were also reported about Outlook 2003-2007 ignoring the options “Mark my comments with” and “Ignore original message text in reply or forward”.
Actions
In fact, the Forward, Reply and ReplyAll methods perform the corresponding actions basing on the user settings. The settings can be found in these locations in Outlook:
- Outlook 2010, on the File tab you choose Options and, on the Mail tab, see Replies and Forwards
- In Outlook 2007, 2003, 2002 and 2000, from the Tools menu you choose Options, click the Preferences tab and click E-mail Options
You can overwrite the settings to create a response in a non-default style. You do this as follows:
Imports System.Runtime.InteropServices Imports Outlook = Microsoft.Office.Interop.Outlook ... Private Sub Reply(mailItem As Outlook._MailItem) Dim actions As Outlook.Actions = mailItem.Actions Dim action As Outlook.Action = actions("Reply") Marshal.ReleaseComObject(actions) action.ReplyStyle = Outlook.OlActionReplyStyle.olIncludeOriginalText Dim response As Outlook._MailItem = CType(action.Execute(), Outlook.MailItem) Marshal.ReleaseComObject(action) response.Display() Marshal.ReleaseComObject(response) End Sub
using System.Runtime.InteropServices; using Outlook = Microsoft.Office.Interop.Outlook; ... private void Reply(Outlook._MailItem mailItem) { Outlook.Actions actions = mailItem.Actions; Outlook.Action action = actions["Reply"]; Marshal.ReleaseComObject(actions); action.ReplyStyle = Outlook.OlActionReplyStyle.olIncludeOriginalText; Outlook._MailItem response = action.Execute() as Outlook.MailItem; Marshal.ReleaseComObject(action); response.Display(); Marshal.ReleaseComObject(response); }
Problems & solutions
Note however that running the code above modifies the source mail item. This produces the message box “The properties of the message %SUBJECT% have been changed. Do you want to save changes to this message?” when you close the Inspector window or, if the item wasn’t opened, when you close Outlook.
To prevent the message from occurring, your code should restore the settings of the action and save the item (MailItem.Save) before the user has a chance to close the inspector. Or, in the case of inspector window, you may choose to close the inspector discarding the changes (Inspector.Close).
Good luck!
10 Comments
Hi,
The mailItem.Reply() in the first section(described as simple way) throws an error — COM Exception. “Could not send mail” .
Please help why this happening like that?
Thanks in advance.
Hello,
I tested it by calling from a Ribbon button’s Click event. In what event do you call that code?
i want to reply from vb.net(win app) itself, that means include the original mail content with format(same way outlook did) bottom of richtextbox and reply.
if i add body like this
RichTextBox1.Text = newMail.Body
format missing
if i add body like this
RichTextBox1.Text = newMail.HTMLBody
all the script included still format missing
please help on this
MailItem.Body returns the plain-test version of the email. Formatting is stored in MailItem.HTMLBody; also, Outlook 2010 introduced MailItem.RTFBody. To check the email type, you use MailItem.BodyFormat; it is not available in Outlook 2000.
Hi,
Is there any way that we can change to custom icons for “Reply”/”Forward” mails.
Ric,
Sorry, but I don’t quite understand your task. Could you describe your goal in detail with screenshots?
Please drop me an e-mail to our support address and I will continue to help you with this issue. You can find the support e-mail address in the readme.txt file (it is included in our product packages).
I tried changing icons of mail items(Read/Unread) in Outlook having message class IPM.Note by creating my own custom message class say IPM.Xyz.
But for the (Reply/Forward) mails i am unable to change the icons using the same custom message class.
Hello Ric,
It looks like the message class of the item is changed when you click the Reply/Forward button in Outlook. Am I right?
Hi,
Is there a way to make this macro do a ReplyAll instead of just a reply?
Thanks
Andy
Hello Andy,
Of course. Just use mailItem.ReplyAll(). This method is described at https://msdn.microsoft.com/en-us/library/ff862498.aspx.