Solved

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

Posted on 2006-07-23
14
2,098 Views
Last Modified: 2012-05-05
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;
0
Comment
Question by:j_vann
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
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
 
LVL 17

Expert Comment

by:Wim ten Brink
Comment Utility
Change: src="cid:logo"
into: src="cid:logo.jpg"
And see if that helps.
0
 

Author Comment

by:j_vann
Comment Utility
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
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
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
 

Author Comment

by:j_vann
Comment Utility
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
 
LVL 28

Accepted Solution

by:
ciuly earned 500 total points
Comment Utility
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
 

Expert Comment

by:rhentzen
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Expert Comment

by:rhentzen
Comment Utility
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
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
according to EE rules, you should open your own question. nowbody will help you in this one since we cannot gain any points ;)
0
 

Expert Comment

by:rhentzen
Comment Utility
Cool, thanks, i will do...hopefully i have points to give :)

Rudy
0
 

Expert Comment

by:rhentzen
Comment Utility
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
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
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
 

Expert Comment

by:rhentzen
Comment Utility
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
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
thanks, you too. good luck.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now