Simple Sending Emails

Please discuss general Delphi programming topics here.

Simple Sending Emails

Postby DelphiAlex » February 24th, 2008, 11:52 am

Hi,

I'm looking for the simplest possible way to send an email.
I have the email address in a database which I can easily extract, but I'm having difficulty finding a working way of sending the email.

I need to be able to manipulate the body text by adding a date and other values from the database.

I first tried:
pCh := 'mailto: peter@preview.org?subject=my subject&body=Body text';
ShellExecute(0,'open',pCh, NIL, NIL, SW_SHOWNORMAL);

This works fine opening the default mail client, but I couldn't insert any fields into the body, apparently due to a PAnsiChar incompatibility. There doesn't seem to be a function to convert String to PAnsiChar.

I also looked into NMSMTP - but I'm using Turbo Delphi 2006 and don't seem to have the DCU for this.



Therefore, does anyone have any suggestions or good Delphi-email pages which may help.

Thanks
Alex
DelphiAlex
Active Member
Active Member
 
Posts: 7
Joined: November 25th, 2007, 10:49 am

Postby Kambiz » February 24th, 2008, 1:38 pm

If you are going the default mail claient, use the following function to build the url.

Code: Select all
function SafeURL(const S: String): String;
const
  SafeChars = ['A'..'Z', 'a'..'z', '0'..'9'];
var
  P: PChar;
begin
  Result := '';
  P := PChar(S);
  while P^ <> #0 do
  begin
    if not (P^ in SafeChars) then
      Result := Result + '%' + IntToHex(Ord(P^), 2)
    else
      Result := Result + P^;
    Inc(P);
  end;
end;


For example:

Code: Select all
URL := Format('mailto:%s?subject=%s&body=%s', [email, SafeURL(subject), SafeURL(body)]);
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2421
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron