Outlook Add-In to add a CC address to outgoing email

K

Kevin Tambascio

Hi,

I'm working on an Add-In (Visual Studio 2003, ATL/C++) to automatically
append an email address to the CC field when an email is sent. So far
I have successfully hooked the event that is generated when the email
is sent. I am able to read the various elements without issue (get_To,
get_CC, etc). When I try to set the CC field using put_CC, I am
getting what looks like some quoting issues. The email never makes it
to the CC destination. If I look at the email that the "To" field was
pointing to, I see this in the headers:

To: <[email protected]>
Cc: "'(e-mail address removed)'"

It looks like the CC field has one single quote and one double quote
before and after the address. I'm loading the fields like this:

void __stdcall CConnect::OnNewMailMessageSent(IDispatch *pItem,
VARIANT_BOOL* pbCancel)
{
CComQIPtr<Outlook::_MailItem> pMailItem(pItem);
if(pMailItem)
{
CComBSTR bstrCC;
miPtr->get_CC(&bstrCC);
if(bstrCC.Length())
{
bstrCC += ", (e-mail address removed)";
}
else
{
bstrCC = "(e-mail address removed)";
}

miPtr->put_CC(bstrCC);
}
}

The other interesting thing I've found is that when I use this code:

CComBSTR cTo("(e-mail address removed)");
CComBSTR cCC("(e-mail address removed)");
CComBSTR cBody("this is a test mail");
CComBSTR cSub("this is a test mail");

Outlook::_MailItemPtr miPtr;
m_spApp->CreateItem(Outlook::eek:lMailItem,
reinterpret_cast<IDispatch**>(&miPtr));

miPtr->put_To(cTo);
miPtr->put_CC(cCC);
miPtr->put_Body(cBody);
miPtr->put_Subject(cSub);
HRESULT hr = miPtr->Send();

The mail message is sent properly, and both the "To" and "CC" fields
work fine. It's only when the user creates the email (versus creating
it programmatically).

Any thoughts?

Thanks,
Kevin
 
D

Dmitry Streblechenko

Try to use a semicolon instead of comma:
bstrCC += "; (e-mail address removed)";

Secondly, a much better approcach is to use the Recipients collection:
In VB:

set Recip = pMailItem.Recpients.Add " (e-mail address removed)"
Recip.Resolve

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top