How to embed image in HTML email from Delphi via Outlook?

I figured out how to create an html email and send it from Delphi using Outlook.  What I can't figure out is how to get the image to embed in the email without showing the attachment in Outlook.  My client does not want to use 3rd party components, and I'd rather avoid the complexities of MAPI (as I'm not familiar with it).  Is it possible with the regular Delphi Outlook components/automation?  

I need the image to show in Outlook, but not the attachment.  Also if anyone has tips on how to prevent extra Outlook processes being created, please feel free to critique - can't figure out what I'm not freeing.

Here's my code:

procedure TfrmTestEmail.btnSendClick(Sender: TObject);
var
  NmSpace: NameSpace;
begin
  OutlookApplication1.Connect;
  NmSpace := OutlookApplication1.GetNamespace('MAPI');
  NmSpace.Logon('', '', False, False);

  try
    MailItem1.ConnectTo(OutlookApplication1.CreateItem(olMailItem) as MailItem);
    //no matter which value is used, the image is not being embedded
    MailItem1.Attachments.Add(edtLogoFile.text,
                          olByValue, // olAttachment, //olByValue, //olHTML, //olEmbeddeditem,
                          1,
                          'logo.jpg' );
    MailItem1.Subject := 'Test olByValue';

    MailItem1.Recipients.Add('test@test.com');
    MailItem1.HTMLBody :=
      '<DIV align=center><IMG height=65 src="cid:logo" width=339 align=left>  Test HTML </DIV> ';

    MailItem1.Save;
    MailItem1.Send;

    NmSpace.Logoff;
  //  OutlookApplication1.Quit;  //Don't really want to force the user to quit Outlook
    OutlookApplication1.Disconnect;

    showmessage('Done');
  except
    on E: Exception do
      showmessage(E.Message);
  end;
end;
j_vannAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
2266180Connect With a Mentor Commented:
hm... indeed, neither msdn lists any fields property for an attachment.
try to
somevariant:=MailItem1.Attachments.Item(1);
somevariant.fields.add(...);
and see if it chokes on it or not. if it does, then move those additions to MailItem1.Fields . maybe there is an error in the vbscript.
let me know the result
0
 
2266180Commented:
I found this: http://www.outlookcode.com/d/code/htmlimg.htm
although it's vb, it can be applied in the same manner to delphi, just paying attention to the syntax.
I can adapt it to delphi if you wish or can't handle it, but it will take a little more time. let me know if you need this done by me.
0
 
Wim ten BrinkSelf-employed developerCommented:
Change: src="cid:logo"
into: src="cid:logo.jpg"
And see if that helps.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
j_vannAuthor Commented:
Thanks for the replies!  

Dear Ciuly- I came across the vb example, but could not figure out how to get it to work in Delphi.  If you can adapt it to Delphi that would be very helpful!

Dear Workshop-Alex- Thanks for the info but I did try this and what happens is that the embedded image will show up, but so does the attachment.  What I need is for the attachment to not show.  If you go to Outlook and create an email and insert an image then mail, the receiver will see the embedded image without an attachment showing up.  This is what I need.

0
 
2266180Commented:
I wrote it directly here as I don't have the office controls installed and don't have time to write this from scratch using ole as I have to leave the office.

procedure TfrmTestEmail.btnSendClick(Sender: TObject);
var
  NmSpace: NameSpace;
oSession,oMsg:variant;
strEntryID:string;// or variant. not sure
begin
  OutlookApplication1.Connect;
  NmSpace := OutlookApplication1.GetNamespace('MAPI');
  NmSpace.Logon('', '', False, False);

  try
    MailItem1.ConnectTo(OutlookApplication1.CreateItem(olMailItem) as MailItem);
    //no matter which value is used, the image is not being embedded
    MailItem1.Attachments.Add('c:\path\to\logo.jpg' );
    MailItem1.Subject := 'Test whatever';

    MailItem1.Recipients.Add('test@test.com');
strEntryID := MailItem1.EntryID;// if this property does not exist under this name, look for a function or property similar to this

  oSession := CreateOleObject('MAPI.Session');
  oSession.Logon('', '', False, False);
 
  // get the message created earlier
  oMsg := oSession.GetMessage(strEntryID);
  //set properties of the attached graphic that make
  //it embedded and give it an ID for use in an <IMG> tag
// all the above might not even be necessary and you could work directly on MailItem1. you can test for this if I am right or not
  with MailItem1.Attachments.Item(1).Fields do// this could be 0, I am not sur eif it's 0-indexed or not
  begin
    Add(CdoPR_ATTACH_MIME_TAG, "image/jpeg");
    Add($3712001E, "myident");
  end;
  MailItem1.Fields.Add ('{0820060000000000C000000000000046}0x8514', 11, True);
  MailItem1.Update;
 
    MailItem1.HTMLBody :=
      '<DIV align=center><IMG height=65 src="cid:logo" width=339 align=left>  Test HTML </DIV> ';

    MailItem1.Save;
    MailItem1.Send;

    NmSpace.Logoff;
  //  OutlookApplication1.Quit;  //Don't really want to force the user to quit Outlook
    OutlookApplication1.Disconnect;

    showmessage('Done');
  except
    on E: Exception do
      showmessage(E.Message);
  end;
end;

fast translation and adaptation. hope there aren't issues. if there are, I will see to it tomorrow (if no one takes this ahead of me :) )
0
 
j_vannAuthor Commented:
Thanks for that.  Haven't been able to get past this line, though:

  with MailItem1.Attachments.Item(1).Fields do

The Fields property does not seem to exist and I couldn't see anything that might be close.  I think this is where I ran into a problem when I tried to translate before.  If you (or anybody) can help me get it working I'd sure be grateful!
0
 
rhentzenCommented:
Hi,
I tried the aforementioned code, however, i was unable to get it to compile.  The main issues were
-   oSession := CreateOleObject('MAPI.Session'); - which could not find the class
-  the Field property was not found.  
I have been looking at code accross the web for the past 2 days, and also the CDO however, i cannot seem to find a solution.

Any help would be greatly appreciated
Thanks
Rudy
0
 
rhentzenCommented:
Ok i got the CreateOleObject('MAPI.Session'); working ok by reinstalling Outlook (office) and installing the CDO objects however referencing the Item(1) gives an Invalid Argument error

Thanks
Rudy
0
 
2266180Commented:
according to EE rules, you should open your own question. nowbody will help you in this one since we cannot gain any points ;)
0
 
rhentzenCommented:
Cool, thanks, i will do...hopefully i have points to give :)

Rudy
0
 
rhentzenCommented:
Hi Ciuly,

I posted the question here, not sure if you managed to take a look yet?
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_23938025.html

Thanks
Rudy
0
 
2266180Commented:
sorry, but I no onlger work in a company and I don't have any outlook related software installe don my PC, so I cannot test anything.

I suggest working with ziolko. as you can see in the top 25 on the right, he is currently #1 delphi expert this year so he is good ;) I know that he is very busy for a few weeks; just try to ping him in a few days and ask him when he could take a look at it.

you can also use the request attention link from the quesiton body to ask the moderators for help in advertising the question to other experts. It would also be a good idea to add the question to some more zones, like outlook. the moderators will do this as well, when you ask via the request attention link.

sorry for not being able to help with this one.
0
 
rhentzenCommented:
Cool, no worries mate, thanks for getting back to me.  I feel so close, but alas.

Cheers mate enjoy the christmas festivities when they arrive
Rudy
0
 
2266180Commented:
thanks, you too. good luck.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.