0

I have made a C# program that lets me fill in blanks to an email template that then writes the email to send. I need it to instead write this email as a reply to a certain email in an outlook subfolder that can be uniquely identified by a part of it's subject line.

I have already created the email to send using Microsoft.Office.Interloop.Outlook.Application

string s = "";
OutlookApp outlookApp = new OutlookApp();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.To = "email";
mailItem.CC = "email";
mailItem.Subject = $"RE: Bulk Error {bulkNo}";
s = $"<html><body>Bulk Number: &emsp; &emsp; &emsp; {bulkNo}<br />"
    + $"Account Number: &emsp; &emsp; {acntNo}<br />" 
    + $"Policy Number: &emsp; &emsp; &ensp; &nbsp; {polNo}<br />" 
    + $"Trans Type: &emsp; &emsp; &emsp; &emsp; {transType}<br />"
    + $"LOB: &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; &nbsp; {lob}<br />"
    + $"Form Number: &emsp; &emsp; &emsp; {formNo}<br />"
    + $"Error: &emsp; &emsp; &emsp; &emsp; &emsp; &emsp; {error}<br />"
    + $"Root Cause: &emsp; &emsp; &emsp; &ensp; &nbsp; {rootCause}<br />"
    + $"XML Status: &emsp; &emsp; &emsp; &ensp; {xmlStatus}</body></html>"
    + ReadSignature();
mailItem.HTMLBody = s;
mailItem.Display();

The current result is just a new email. I'm trying to get it to be a response to an existing email.

My biggest issues are that I don't know how to

  1. make this message a reply

or

  1. How to identify the email i want to reply to.
Evan
  • 13
  • 3
  • For 1) find the MailItem you want and [Reply](https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.reply(method)), For 2) use the [Application.AdvancedSearch](https://learn.microsoft.com/en-us/office/vba/api/outlook.application.advancedsearch) – Filburt Aug 13 '19 at 13:28
  • I found advanced search. I'm just unsure how to use it to find what i need – Evan Aug 13 '19 at 14:10
  • There are already some examples: [Using AdvancedSearch for Outlook with C# Returns Zero Results](https://stackoverflow.com/q/40867024/205233) **or** [Perform search query in Outlook](https://stackoverflow.com/q/11160610/205233) – Filburt Aug 13 '19 at 16:55

1 Answers1

1

Call MailItem.Reply - it will returns a new MailItem object with the body and recipients properly populated. You can then merge the new message body (whcih includes the old body and the header) with your new body.

To retrieve the currently selected message in Outlook, use Application.ActiveExplorer.Selection[1]. If you need to find the message first, retreive its parent folder (you can use Application.Session.GetDefaultFolder(olFolderInbox) in case of the Inbox folder) and use Items.Find/FindNext or Items.Restrict (where Items comes from the MAPIFolder.Items collection).

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • What's a MAPIFolder? Is it used by adding it to the top "using" list? – Evan Aug 13 '19 at 21:06
  • MAPIFolder is the object you will get back from calling Application.Session.GetDefaultFolder(olFolderInbox). See https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.outlook.mapifolder?view=outlook-pia – Dmitry Streblechenko Aug 13 '19 at 21:54