I finally did it

D

dorutzu

Hi,

After about 3 days and nights of stuggle, I finally succeded emedding
images in Outlook(2003), from VC++ 6.
I'd like to thank Sue Mosher for his answers, Neo for his
http://www.outlookcode.com/d/code/htmlimg.htm, Serhat Gurel for his
post at http://www.dotnet247.com/247reference/msgs/29/147927.aspx, and
Google for existing.

So, here's the code(I hope it's good to be compiled, because I made
some chages in the mean time...:), I hope it will help someone,
someday:

//this is from my StdAfx.h:
#import "C:\Program Files\Common Files\System\MSMAPI\1033\cdo.dll"
using namespace MAPI;


//and from my Addin function, for creating a new e-mail:
CComPtr<IDispatch> spDisp;

//create the new mail item
m_spApp->CreateItem(olMailItem, &spDisp);
if(spDisp == NULL) return;
CComQIPtr<Outlook::_MailItem> spNewMail(spDisp);
if(spNewMail == NULL) return;

//in the html, the src must be something like
src=cid:a_unique_value_in_the_html
spNewMail->put_HTMLBody(T2W("the image:<img src=cid:myimage>"));

CComVariant vEntryID;
CComPtr<Outlook::Attachments> spAttachments;
HRESULT hr = spNewMail->get_Attachments(&spAttachments);
if(FAILED(hr))
return;
if(spAttachments == NULL)
return;

//here you can also add multiple images, just watch out for their
types/cids lower...
CComPtr<Outlook::Attachment> spAttach;
CComVariant vSource("c:\image path\image name.gif");
CComVariant vType(olByValue);
CComVariant vPos(1);
CComVariant vDispName("");
hr = spAttachments->Add(vSource, vType, vPos, vDispName, &spAttach);
if(FAILED(hr))
return;
if(spAttach == NULL)
return;
spAttach.Release();
delete spAttach;



spNewMail->Close(olSave);
spNewMail->get_EntryID(&vEntryID.bstrVal); //you will use this to
retrive this item and edit it's proprieties with cdo

spNewMail.Release();
delete spNewMail;
spAttachments.Release();
delete spAttachments;


//the CDO part....
MAPI::MessagePtr pMapiMessage;
MAPI::AttachmentsPtr pMapiAttachments = NULL;
MAPI::AttachmentPtr pMapiAttachment = NULL;
MAPI::FieldsPtr pMapiFields;

if(m_pMapiSession == NULL) //on first run, create it and logon
{
m_pMapiSession.CreateInstance("MAPI.Session"); //this is global,
defined like MAPI::_SessionPtr m_pMapiSession -> logon is
time-consuming...you only do it once ;)
m_pMapiSession->Logon("Outlook"); //or whatever
}
if(m_pMapiSession == NULL)
return;
CComVariant vEntryID2(vEntryID.bstrVal);
pMapiMessage = m_pMapiSession->GetMessage(vEntryID2); //get the mail
item generated earlyer, with attachements...
if(pMapiMessage == NULL)
return;
pMapiAttachments = pMapiMessage->GetAttachments();
if(pMapiAttachments == NULL)
return;

pMapiAttachment = pMapiAttachments->GetItem((long)1); //if we have
only 1 att
if(pMapiAttachment == NULL)
return;
pMapiFields = pMapiAttachment->GetFields();
if(pMapiFields == NULL)
return;
//set the MIME tags needed for image embedding
CComVariant vImageType("image/gif"); //for my image...but it can be
anything else
pMapiFields->Add((long)CdoPR_ATTACH_MIME_TAG, vImageType);
CComVariant vCidName("myimage"); //the same as the one in the html
pMapiFields->Add((long)0x3712001E, vCidName);
//this is, I read somewhere, to make Outlook hide the paperclip icon
next to the mailitem -> hidding the attachments, but I can't tell for
sure :)
CComVariant vName3("{0820060000000000C000000000000046}0x8514");
pMapiFields->Add(vName3, (long)11, true);

pMapiMessage->Update();
CComVariant vEntryID3(pMapiMessage->ID); //get the ID, to reopen the
edited mailitem

pMapiFields.Release(); delete pMapiFields;
pMapiAttachment.Release(); delete pMapiAttachment;
pMapiAttachments.Release(); delete pMapiAttachments;
pMapiMessage.Release(); delete pMapiMessage;

spDisp.Release(); //!!! this is very important to be released,
because if it's not done, the inspector will show the e-mail without
the CDO changes!!!


//back to outlook -> get our mailitem, and display it...
CComPtr<Outlook::_NameSpace> spNSmapi;
hr = m_spApp->GetNamespace(T2W("MAPI"), &spNSmapi);
if(FAILED(hr)) return;
if(spNSmapi == NULL) return;

CComPtr<IDispatch> spDispItem;
CComVariant vNull("");
spNSmapi->GetItemFromID(vEntryID3.bstrVal, vNull, &spDispItem);
if(spDispItem == NULL) return;

CComQIPtr<Outlook::_MailItem> spNewMail2(spDispItem);
if(spNewMail2 == NULL) return;

spNewMail2->Display();
spNewMail2.Release();

//EASY! :)).....took me only 3 nights of no sleep :)

//you can reach me on yahoo messenger-> my id is dorutzu

Doru
 

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