moster67
moster67

Reputation: 850

Reply to an (open) outlook mail from an external program

In an external app I'd like to reply to an e-mail (Outlook is the email client). The email is already opened on the computer-screen. In the reply I would like to insert a reply generated in code from the external app. Instead of having the message to reply to an already opened email in a separate outlook-window, I could alternatively also search for the specific mail and then using code for replying.

Any ideas what to look for among the outlook objects? Any code-examples (vb.net or c#)?

I already know how to create a new e-mail in outlook from my external app through code but I am unsure how to reply to an existing email.

Upvotes: 0

Views: 2250

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

The Reply method of Outlook items creates a reply, pre-addressed to the original sender, from the original message. You just need to get the currently opened email, call the Reply method on it and use the Send method to send the e-mail message.

To get the currently displayed email in the explorer window you need to use the Selection property of the Explorer class (see the ActiveExplorer function of the Application class). In case of the Inspector windows you can use the CurrentItem property of the Inspector class (see the ActiveInspector function of the Application class). See How to: Programmatically Determine the Current Outlook Item for more information and sample code in C#.

Outlook.Inspector inspector = null;
Outlook.MailItem sourceMail = null;
Outlook.MailItem replyMail = null;
try
{
    inspector =  Application.ActiveInspector();
    sourceMail = inspector.CurrentItem as MailItem;
    replyMail = sourceMail.Reply();
    // any modifications if required    
    replyMail.Send(); // just change mail to replyMail because mail variable ///is not declare 
}
catch (Exception ex)
{
    System.Windows.Forms.MessageBox.Show(ex.Message,
        "An exception is occured in the code of add-in.");
}
finally
{
    if (sourceMail != null) Marshal.ReleaseComObject(sourceMail);
    if (replyMail != null) Marshal.ReleaseComObject(replyMail);
    if (inspector != null) Marshal.ReleaseComObject(inspector);
}

Also you may find the How To: Create and send an Outlook message programmatically article helpful.

Upvotes: 1

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Use Application.ActiveExplorer.CurrentItem to access the currently open message, then call MailItem.Reply to get the reply MailItem object, modify its message body (MailItem.Body), call MailItem.Display to show it to the user.

Upvotes: 1

Related Questions