How to launch email program

I want to launch an email program (whether Outlook or other) from my app. I need to pass a recipient email address to the email program, (and a subject line if possible). I guess WinExec is best, but I don't know what parameters to use.
Thanks
HorridManAsked:
Who is Participating?
 
geobulConnect With a Mentor Commented:
Hi,

Try this:

uses ShellApi;

procedure LaunchEMail(const address, subject: string);
begin
  ShellExecute(0, 'open', PChar('mailto:'+address+'?subject='+subject), nil, nil, SW_SHOWNORMAL);
end;

// usage
procedure TForm1.Button1Click(Sender: TObject);
begin
  LaunchEMail('aaa@abv.bg', 'Hello');
end;

Regards, Geo
0
 
robert_marquardtCommented:
This is what Simple MAPI is for.
Try the Jedi Code library http://jcl.sf.net
It has a class to access Simple Mapi.

Alternatively use the JVCL (includes JCL) http://jvcl.sf.net
It has a component on top of the JCL class.
0
 
JustinWillisCommented:
Yes I think that is the best way, just wondering what ShellExecute does different that WinExec, i have always used WinExec before, what are the advantages of using ShellExecute instead?

If this is not a simple question/answer then don't worry about it! ;)

Regards,
Justin Willis.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
geobulCommented:
Hi Justin,

1. You can't simply call:
WinExec(PChar('mailto:'+address+'?subject='+subject), SW_SHOWNORMAL);

You'll have to find out the assosiated program first and then add the parameters after the program name. ShellExecute, on the other hand, does that for you and launches the default mail client, i.e. saves you some work (one FindExecutable API call). The same applies to all other nonexecutable filenames that have 'open' or 'print' assosiations to executables.

2. You may control the directory your process will start from.

3. You still may use an executable name directly passing parameters to it.

4. You may control whether the new process will have a parent window.

5. You may print a file directly just replacing 'open' with 'print':
ShellExecute(0, 'print', PChar('c:\test.doc'), nil, nil, SW_SHOWNORMAL);

Perhaps there is more I've missed above.

Regards, Geo
0
 
JustinWillisCommented:
wow, thanks, i didn't realize there was that much difference.

thanks very much for highlighting those.

Justin.
0
 
DTHLIEMCommented:
Hello HorridMan
--------------------------------------------

 uses
  ShellApi;


procedure TForm1.Button1Click(Sender: TObject);
var
  strEmail, strSubject, strBody, Param: string;
begin
  strEmail := 'user@host.com';
  strSubject := 'Your Subject';
  strBody := 'Your Message Text';

  Param := 'mailto:' + strEmail + '?subject=' + strSubject +
           '&Body=' + strBody;

  ShellExecute(Form1.Handle, 'open', PChar(Param), nil, nil, SW_SHOWNORMAL);
end;
 
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.