News
Photos
Articles
Components
Applications
Kleinkunst

Delphi - URLs and emails

In Delphi it is quite easy to open an URL (Universal Resource Location) in your webbrowser by using the WinAPI function ShellExecute (unit ShellAPI). This function is mostly used to start applications but it can also be used for calling some internet protocols like MailTo, HyperText Tranfer Protocol (http), File Tranfer Protocol (ftp), News, Telnet, …

The syntax in the Win32 SDK help :

HINSTANCE ShellExecute(
  HWND hwnd,            // handle to parent window
  LPCTSTR lpOperation,  // pointer to string that specifies operation to perform
  LPCTSTR lpFile,       // pointer to filename or folder name string
  LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters 
  LPCTSTR lpDirectory,  // pointer to string that specifies default directory
  INT nShowCmd          // whether file is shown when opened
);

Opening an URL or sending an email can be done by changing the file-parameter into "mailto:emailaddress" or "http://webaddress".
For sending an email you can also add some extra values to the file-parameter to specify the subject and text. Be aware that this only works when Microsoft Outlook or Microsoft Outlook Express is your default email application. Text lines can be separated by %0a.

'?subject=' + SubjectString +
'&body='+ Line1String +
'%0a' + Line2String

Examples

ShellExecute(Application.Handle,'open','mailto:person@domain','',nil,SW_NORMAL);
ShellExecute(Application.Handle,'open',
  'mailto:person@domain ?subject=test &body=line1 %0a line2',
  '',nil,SW_NORMAL);
ShellExecute(Application.Handle,'open','http://www.scip.be','',nil,SW_NORMAL);