Automaticly open Email-message with HTML in the body

Dear Experts,

I have read and tried many articles on the Internet about Email in Delphi.

What I want is: When the user clicks on a button then the default E-mail client
starts with an New Message form, like what this procedure does:

uses
  ShellApi;

procedure TForm1.Button1Click(Sender: TObject);
var
  mail: string;
begin
  mail := 'mailto:you@you.com' + '?subject=hello' + '&cc=me@me.com' + '&body=Delphi is cool! ;)';
  ShellExecute(Self.Handle, 'open', PChar(mail), nil, nil, SW_SHOWNORMAL);
end;

But instead of a string '&body=Delphi is cool! ;)' in the body-section
I want HTML (TStrings) in the body-section, like this:

var
  html: TStrings;
begin
  html := TStringList.Create();
  html.Add('<html>');
  html.Add('<head>');
  html.Add('</head>');
  html.Add('<body><h1>Hello</h1>');
  html.Add('This is a picture of us!</body>');
  html.Add('</html>');

So,
1. The user clicks on a button
2. The default email-clients starts with a new message form
3. With just the body-section filled with HTML

* The email must not be send automatically, that's the job of the user *

Does any one know a solution?

Thank you in advanced.

Greetings,

Peter Kiers
LVL 1
peterkiersAsked:
Who is Participating?
 
MerijnBConnect With a Mentor Sr. Software EngineerCommented:
I just found out that you can use MAPI to prepare, but not send a mail, I didn't know that (something to learn everyday).

See this example (I found the SendMailMAPI function itself here http://www.delphifaq.com/faq/delphi/network/f236.shtml):


function SendMailMAPI(const Subject, Body, FileName, SenderName, SenderEMail, RecepientName, RecepientEMail: String) : Integer;
var
  message: TMapiMessage;
  lpSender,
  lpRecepient: TMapiRecipDesc;
  FileAttach: TMapiFileDesc;
  SM: TFNMapiSendMail;
  MAPIModule: HModule;
begin
  FillChar(message, SizeOf(message), 0);
  with message do
  begin
    if (Subject<>'') then
    begin
      lpszSubject := PChar(Subject)
    end;
    if (Body<>'') then
    begin
      lpszNoteText := PChar(Body)
    end;
    if (SenderEMail<>'') then
    begin
      lpSender.ulRecipClass := MAPI_ORIG;
      if (SenderName='') then
      begin
        lpSender.lpszName := PChar(SenderEMail)
      end
      else
      begin
        lpSender.lpszName := PChar(SenderName)
      end;
      lpSender.lpszAddress := PChar('SMTP:'+SenderEMail);
      lpSender.ulReserved := 0;
      lpSender.ulEIDSize := 0;
      lpSender.lpEntryID := nil;
      lpOriginator := @lpSender;
    end;
    if (RecepientEMail<>'') then
    begin
      lpRecepient.ulRecipClass := MAPI_TO;
      if (RecepientName='') then
      begin
        lpRecepient.lpszName := PChar(RecepientEMail)
      end
      else
      begin
        lpRecepient.lpszName := PChar(RecepientName)
      end;
      lpRecepient.lpszAddress := PChar('SMTP:'+RecepientEMail);
      lpRecepient.ulReserved := 0;
      lpRecepient.ulEIDSize := 0;
      lpRecepient.lpEntryID := nil;
      nRecipCount := 1;
      lpRecips := @lpRecepient;
    end
    else
    begin
      lpRecips := nil
    end;
    if (FileName='') then
    begin
      nFileCount := 0;
      lpFiles := nil;
    end
    else
    begin
      FillChar(FileAttach, SizeOf(FileAttach), 0);
      FileAttach.nPosition := Cardinal($FFFFFFFF);
      FileAttach.lpszPathName := PChar(FileName);
      nFileCount := 1;
      lpFiles := @FileAttach;
    end;
  end;
  MAPIModule := LoadLibrary(PChar(MAPIDLL));
  if MAPIModule=0 then
  begin
    Result := -1
  end
  else
  begin
    try
      @SM := GetProcAddress(MAPIModule, 'MAPISendMail');
      if @SM<>nil then
      begin
        Result := SM(0, Application.Handle, message, MAPI_DIALOG or
                     MAPI_LOGON_UI, 0);
      end
      else
      begin
        Result := 1
      end;

    finally
      FreeLibrary(MAPIModule);
    end;
  end;
  if Result<>0 then
  begin
    MessageDlg('Error sending mail ('+IntToStr(Result)+').', mtError, [mbOk],
               0)
  end;
end;

procedure TForm14.Button1Click(Sender: TObject);
var
  html: TStrings;
begin
  html := TStringList.Create();
  html.Add('<html>');
  html.Add('<head>');
  html.Add('</head>');
  html.Add('<body><h1>Hello</h1>');
  html.Add('This is a picture of us!</body>');
  html.Add('</html>');
  SendMailMAPI('test subject', HTML.text, '', 'me', 'me@here.com', 'test', 'you@there.com');
  html.Free();
end;
0
 
MerijnBSr. Software EngineerCommented:
the only good way of achieving this is to use automation of each different mail client (if this is available).
In other words, this is very very hard to achieve properly.
0
 
peterkiersAuthor Commented:
I know it possible, because I know another programm that has that feature too.
I have tried many examples, but the ones with HTML in the body-section actually
send the email, and I don't want that.

Peter

 
0
Get your problem seen by more experts

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

 
MerijnBSr. Software EngineerCommented:
and this other program, which email clients does it support?
0
 
peterkiersAuthor Commented:
all
0
 
MerijnBSr. Software EngineerCommented:
the only thing which will 'work' is what you described in your first post, the the problem is that the size of the command line is limited and that you don't know to what, so unless you will always write very short mails to very few people, it will be very likely that your mail contents will be truncated.
0
 
peterkiersAuthor Commented:
With this procedure you can  add HTML in the body-section
but is send the mail, and I don't want that.

http://www.howtodothings.com/computers/a791-sending-html-emails-with-indy.html

And this procedure below does not send the mail but HTML
in the body-section is not possible:

http://www.techtricks.com/delphi/sendmail.php

P.
0
 
peterkiersAuthor Commented:
Hi,

I have tested your example. But the text in the
body-section is text and not HTML.

Peter
0
 
MerijnBSr. Software EngineerCommented:
It does work with my mail client, thunderbird.

It seems that it just isn't possible: http://support.microsoft.com/kb/268440

Did you try:

var
  html: TStrings;
  mail: string;
begin
  html := TStringList.Create();
  html.Add('<html>');
  html.Add('<head>');
  html.Add('</head>');
  html.Add('<body><h1>Hello</h1>');
  html.Add('This is a picture of us!</body>');
  html.Add('</html>');
  mail := 'mailto:you@you.com' + '?subject=hello' + '&cc=me@me.com' + '&body=' + html.Text;
  ShellExecute(Self.Handle, 'open', PChar(mail), nil, nil, SW_SHOWNORMAL);
0
 
peterkiersAuthor Commented:
Oke, I quess it just isn't possible.
Thank you for trying.

500 comming your way.

Peter Kiers
0
 
rob_emacsCommented:
There is a work-around which I've been using. If you save the html you want to send out to a file and then use the function above to send a blank body with a .htm file as an attachment then Outlook picks this up as a HTML email message.

SendMailMAPI('test subject', '', 'c:\htmlbody.htm' , 'me', 'me@here.com', 'test', 'you@there.com');
0
 
peterkiersAuthor Commented:
Thanks

Peter Kiers
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.