Link to home
Start Free TrialLog in
Avatar of Allan_Fernandes
Allan_Fernandes

asked on

Smoothest way to send Email via Delphi

Hi,

I need to send email from my application (with small attachment) with either of two options.
1) Via default Email Handler (Even if it is not running) (alternatively can I know default handler is not running)
or
2) Directly
How can I do this without windows popping up ugly messages.

I use Delphi 2007


Regards
Allan


ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you want to send with outlook, use outlook Redemption for the same
Another way is to call the default email handler to create a new mail, filling some fields and allowing the user to view/edit the email before he sends it.
That can be done in one single line (ShellExecute) if you only want one destination and a subject, but this complete function will handle additional parameters and URL encoding problems (especially the body)

Note that some parameters like subject & content do not work well on all email clients (Eudora). No problem so far with Microsoft emails applications (Outlook)

CreateMail( edtSubject.Text, mmoSendTo.Lines, mmoBody.Lines);
function CreateMail( sSubject : string; rgTo:TStrings; rgContent:TStrings=nil; rgCc:TStrings=nil; rgBcc:TStrings=nil ) : Boolean;

	function Encode(s: String): String;
	var i: Integer;
	Const
	 cset :set of Char=  [' ', '#', '<', '>', '"', '%', '?', '+'];
	begin
         Result:='';
	 for i := 1 to length(s) do
	  begin
	   if s[i] in cset 
	    Then Result:=Result+'%'+IntToHex(ord(s[i]),2)
	    Else Result:=Result+s[i];
	  end;
	end;
	
	function LinesToStr( rgs : TStrings; const sDelim : string ) : string;
	var i : Integer;
	begin
	 Result := '';
	 if Assigned(rgs) Then for i := 0 to rgs.Count - 1 do
	  begin
	   Result := Result + Encode(rgs[ i]);
	   if ( i < rgs.Count - 1 ) then Result := Result + sDelim;
	  end;
	end;

var
  sTo, sCc, sBcc, sContent, sURL : string;

begin
  Result := False;
  if not Assigned( rgTo ) then Exit;
  sTo := LinesToStr( rgTo, '; ' );
  sBcc := LinesToStr( rgBcc, '; ' );
  sCc := LinesToStr( rgCc, '; ' );
  sContent := LinesToStr( rgContent, '%0D%0A' );
  sURL := 'mailto:' + sTo;
  sSubject := Encode(sSubject);
  if ( sCc <> '' ) then sURL := sURL + '?cc=' + sCc;
  if ( sBcc <> '' ) then sURL := sURL + '&bcc=' + sBcc;
  if ( sSubject <> '' ) then sURL := sURL + '&subject=' + sSubject;
  if ( sContent <> '' ) then sURL := sURL + '&body=' + sContent;
  Result := ShellExecute( 0, 'Open', PChar( sURL ), nil, nil, SW_SHOWNORMAL ) > 32;
end; 

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial