?
Solved

Automaticly open Email-message with HTML in the body

Posted on 2007-10-03
12
Medium Priority
?
591 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:peterkiers
  • 6
  • 5
12 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20004943
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
 
LVL 1

Author Comment

by:peterkiers
ID: 20004970
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
 
LVL 19

Expert Comment

by:MerijnB
ID: 20004976
and this other program, which email clients does it support?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:peterkiers
ID: 20004984
all
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20005000
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
 
LVL 1

Author Comment

by:peterkiers
ID: 20005049
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
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 20005059
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
 
LVL 1

Author Comment

by:peterkiers
ID: 20006712
Hi,

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

Peter
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20007075
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
 
LVL 1

Author Comment

by:peterkiers
ID: 20007196
Oke, I quess it just isn't possible.
Thank you for trying.

500 comming your way.

Peter Kiers
0
 

Expert Comment

by:rob_emacs
ID: 20412265
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
 
LVL 1

Author Comment

by:peterkiers
ID: 20412717
Thanks

Peter Kiers
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

840 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