Mega Search
23.2 Million


Sign Up

Make a donation  
Send a mail with TIdSMTP with a signature  
News Group: embarcadero.public.delphi.internet.winsock

I want to send an email via TIdSMTP with an image as the logo to simulate the signature in Outlook.
I try this code but in Outlook I see only a square with a little x inside.
Here the code:
    IdMessage1.From.Address := FromAddress;
    IdMessage1.Subject := aSubject;
    //IdMessage1.ContentType := 'multipart/mixed';
    IdMessage1.Body.Add('');
    IdMessage1.Body.Add('');
    IdMessage1.Body.Add('');
    IdMessage1.Body.Add(myText);
    IdMessage1.Body.Add('');
    IdMessage1.Body.Add('');
    IdMessage1.Body.Add('
Vote for best question.
Score: 0  # Vote:  0
Date Posted: 12-Dec-2014, at 1:49 AM EST
From: Roberto Colpani
 
Re: Send a mail with TIdSMTP with a signature  
News Group: embarcadero.public.delphi.internet.winsock
Roberto wrote:

> I try this code but in Outlook I see only a square with a little x
> inside.

You did not set up the TIdMessage correctly.  It needs to be more like this 
instead:

{code}
IdMessage1.ContentType := 'multipart/related; type="text/html"';

IdText1 := TIdText.Create(IdMessage1.MessageParts, nil);
IdText1.ContentType := 'text/html';
IdText1.Body.Add('');
IdText1.Body.Add('');
IdText1.Body.Add('');
IdText1.Body.Add(myText);
IdText1.Body.Add('');
IdText1.Body.Add('');
IdText1.Body.Add('');

IdAttach := TIdAttachmentFile.Create(IdMessage1.MessageParts, 'mypath\myjpeg.jpg');
IdAttach.ContentType := 'image/jpeg';
IdAttach.ContentDisposition := 'inline';
IdAttach.FileIsTempFile := False;
IdAttach.ContentID := 'myjpeg';
IdAttach.DisplayName := 'Logo';
{code}

The important things are:

1. You are using the wrong TIdMessage.ContentType value.  The ContentType 
needs to be "multipart/related", with a "type" attribute indicating which 
child part is the main part of the relationship (in this case, the HTML). 
 That tells Outlook (or any other MIME reader) that multiple MIME parts are 
related to each other.

2. You are using the wrong TIdAttachment.ContentID value.  The ContentID 
needs to match the value of the "cid:" url used in the HTML  tag.  That 
allows the content of one MIME part to refer to the content of another MIME 
part within the relationship.

I suggest you read the following article I wrote on the Indy website:

HTML Messages
http://www.indyproject.org/sockets/blogs/RLebeau/2005_08_17_A.aspx

Also this one:

New HTML Message Builder class
http://www.indyproject.org/sockets/blogs/RLebeau/20080116.aspx

Your code could be re-written using TIdMessageBuilderHtml as follows:

{code}
with TIdMessageBuilderHtml.Create do
try
  Html.Add('');
  Html.Add('');
  Html.Add('');
  Html.Add(myText);
  Html.Add('');
  Html.Add('');
  Html.Add('');

  with HtmlFiles.Add('mypath\myjpeg.jpg', 'myjpeg') do
  begin
    ContentType := 'image/jpeg';
    Name := 'Logo';
  end;

  FillMessage(IdMessage1);
finally
  Free;
end;
{code}

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 12-Dec-2014, at 12:20 PM EST
From: Remy Lebeau (TeamB)