Andrei Smolin

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<br />
 Imports Outlook = Microsoft.Office.Interop.Outlook<br />
 ...<br />
 Private Sub Reply(mailItem As Outlook._MailItem)<br />
     Dim response As Outlook._MailItem = mailItem.Reply()<br />
     response.Display()<br />
     Marshal.ReleaseComObject(response)<br />
 End Sub
using System.Runtime.InteropServices;<br />
 using Outlook = Microsoft.Office.Interop.Outlook;<br />
 ...<br />
 private void Reply(Outlook._MailItem mailItem)<br />
 {<br />
     Outlook._MailItem response = mailItem.Reply();<br />
     response.Display(Type.Missing);<br />
     Marshal.ReleaseComObject(response);<br />
 }

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<br />
Imports Outlook = Microsoft.Office.Interop.Outlook<br />
...<br />
Private Sub Reply(mailItem As Outlook._MailItem)<br />
    Dim actions As Outlook.Actions = mailItem.Actions<br />
    Dim action As Outlook.Action = actions("Reply")<br />
    Marshal.ReleaseComObject(actions)<br />
    action.ReplyStyle = Outlook.OlActionReplyStyle.olIncludeOriginalText<br />
    Dim response As Outlook._MailItem = CType(action.Execute(), Outlook.MailItem)<br />
    Marshal.ReleaseComObject(action)<br />
    response.Display()<br />
    Marshal.ReleaseComObject(response)<br />
End Sub<br />
using System.Runtime.InteropServices;<br />
using Outlook = Microsoft.Office.Interop.Outlook;<br />
...<br />
private void Reply(Outlook._MailItem mailItem)<br />
{<br />
    Outlook.Actions actions = mailItem.Actions;<br />
    Outlook.Action action = actions["Reply"];<br />
    Marshal.ReleaseComObject(actions);<br />
    action.ReplyStyle = Outlook.OlActionReplyStyle.olIncludeOriginalText;<br />
    Outlook._MailItem response = action.Execute() as Outlook.MailItem;<br />
    Marshal.ReleaseComObject(action);<br />
    response.Display();<br />
    Marshal.ReleaseComObject(response);<br />
}

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

  • https://secure.gravatar.com/avatar/02ae277e7a0dd87ac0560389b08c6cab?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G AAA says:

    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.

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello,

    I tested it by calling from a Ribbon button’s Click event. In what event do you call that code?

  • https://secure.gravatar.com/avatar/861c8ae6ee005d636807485e1c9286f0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G veeramanink says:

    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

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    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.

  • https://secure.gravatar.com/avatar/054e6826a6a9efa8c5bc64b728f38e5f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ric says:

    Hi,

    Is there any way that we can change to custom icons for “Reply”/”Forward” mails.

  • https://secure.gravatar.com/avatar/bbd979ae5b772d0bc1f82065aa4ba146?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Eugene Astafiev says:

    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).

  • https://secure.gravatar.com/avatar/054e6826a6a9efa8c5bc64b728f38e5f?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Ric says:

    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.

  • https://secure.gravatar.com/avatar/bbd979ae5b772d0bc1f82065aa4ba146?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Eugene Astafiev says:

    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?

  • https://secure.gravatar.com/avatar/95078ed47616723303b078ea3bf377e7?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andy Llewellyn says:

    Hi,
    Is there a way to make this macro do a ReplyAll instead of just a reply?

    Thanks

    Andy

  • https://secure.gravatar.com/avatar/29957f26ad2d8ba527fd9cc8cfa7b2e0?s=32&d=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D32&r=G Andrei Smolin (Add-in Express Team) says:

    Hello Andy,

    Of course. Just use mailItem.ReplyAll(). This method is described at http://msdn.microsoft.com/en-us/library/ff862498.aspx.

Post a comment

Have any questions? Ask us right now!