Solved

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

Posted on 2006-07-23
14
2,226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17165623
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
ID: 17166913
Change: src="cid:logo"
into: src="cid:logo.jpg"
And see if that helps.
0
 

Author Comment

by:j_vann
ID: 17167068
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:2266180
ID: 17167708
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
ID: 17173373
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:
2266180 earned 500 total points
ID: 17173661
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
ID: 23033980
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
 

Expert Comment

by:rhentzen
ID: 23041081
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:2266180
ID: 23042942
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
ID: 23043758
Cool, thanks, i will do...hopefully i have points to give :)

Rudy
0
 

Expert Comment

by:rhentzen
ID: 23063903
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:2266180
ID: 23063921
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
ID: 23063942
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:2266180
ID: 23063944
thanks, you too. good luck.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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