0

I am writing an Outlook 2007 Add-in which composes a business quote in response to an email query. I compose the quote using Windows forms. Everything works fine until I get to the point of replying to the original message with the quote information.

private void btnSend_Click(object sender, EventArgs e) 
{
    Outlook.MailItem theMail = ((Outlook._MailItem)quote.mailItem).Reply();
    theMail.Subject = "This is the quote";
    theMail.Body = <Some html composed elsewhere>;

    Outlook.Recipient rcp = theMail.Recipients.Add("Joe Blow");
    Outlook.AddressEntry ae = rcp.AddressEntry;
    ae.Address = "joe@blow.com";
}

Where quote.mailItem is the incoming email request. When I run the code, it throws an exception executing rcp.AddressEntry. The error is

'An object cannot be found'

. What I need to be able to do is add and delete addressees as well as setting CC and BCC fields on the quote before I send it out. Addressees may not be in the address book. I have done this with other mail libraries and it should be simple, but I seem to be barking up the wrong tree for Outlook.

EDIT Found it - thanks Dmitry for pointing me in the right direction.

Outlook.Recipient rcp = theMail.Recipients.Add("joe blow <joe@blow.com>");
rcp.Type = (int)Outlook.OlMailRecipientType.olTo;
Jon
  • 703
  • 3
  • 15
  • 34
  • If the answer of dimitry is correct, please rate and accept it :) – etalon11 Feb 05 '16 at 10:46
  • The problem was not address resolution as provided in Dmitry's answer, but he did get me into the proper area of the documentation for which I thanked him. – Jon Feb 05 '16 at 15:51

1 Answers1

1

The recipient must be resolved first . And you cannot set the AddressEntry.Address property - even if it were settable, it does not point back to the message recipients table.

Outlook.Recipient rcp = theMail.Recipients.Add("Joe Blow <joe@blow.com>");
rcp.Resolve();
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Thanks Dmitry - am I to infer that a recipient must be in the address book before Outlook will send mail to him? – Jon May 02 '13 at 23:01
  • No, "Joe Blow " will be resolved to a one-off SMTP recipient. This is not any different from manually typing the address in the To edit box in Outlook and hitting Ctrl+K to resolve it. – Dmitry Streblechenko May 03 '13 at 00:53