Link to home
Start Free TrialLog in
Avatar of Schmoekel
Schmoekel

asked on

ShellExecute... to start my email-client...

Hello !
With my code here i can start my email-client with the parameters 'subject' and 'mailto'.:
//------------------------------
procedure TForm5.Button2Click(Sender: TObject);
var
s : String;
e : String;

begin
e := 'Ingo.Schmoekel@t-online.de';
s := 'mailto:'+e+'?subject=Vielen Dank...!';
ShellExecute(hinstance,'open',PChar(s),nil,nil,SW_SHOWNORMAL);
end;
//--------------------------

Is it possible to send more parameters? ...I need 'Body or so' for the whole email-text and something for an attached file?!
Thank's a lot for a code-sample or even any tip!

Sincerly,
Ingo Schmökel



ASKER CERTIFIED SOLUTION
Avatar of rwilson032697
rwilson032697

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
Avatar of TheNeil
TheNeil

I wouldn't use ShellExecute if I was you (too limited). The best way is to use the MAPI stuff or the SMTP component. Try the following (using MAPI)

uses mapi;

function SendEMail(Handle :THandle;Mail :TStrings):Cardinal;
type
  TAttachAccessArray = ARRAY [0..0] OF TMapiFileDesc;
  PAttachAccessArray = ^TAttachAccessArray;
var
  MapiMessage : TMapiMessage;
  Receip      : TMapiRecipDesc;
  Attachments : PAttachAccessArray;
  AttachCount : Integer;
  i1          : Integer;
  FileName    : String;
begin
  fillChar(MapiMessage,SizeOf(MapiMessage),#0);
  Attachments:=nil;
  fillChar(Receip,SizeOf(Receip),#0);
  if Mail.Values['to'] <> ''
  then
  begin
    Receip.ulReserved := 0;
    Receip.ulRecipClass := MAPI_TO;
    Receip.lpszName := StrNew(PChar(Mail.Values['to']));
    Receip.lpszAddress:=StrNew(PChar('SMTP:' + Mail.Values['to']));
    Receip.ulEIDSize:=0;
    MapiMessage.nRecipCount := 1;
    MapiMessage.lpRecips    := @Receip;
  end;
  AttachCount := 0;
  for i1 := 0 to MaxInt
  do
  begin
    if Mail.Values['attachment'+inttostr(i1)]=''
    then
      break;
    inc(AttachCount);
  end;
  if AttachCount > 0
  then
  begin
    GetMem(Attachments,SizeOf(TMapiFileDesc)*AttachCount);
    for i1 := 0 to AttachCount-1
    do
    begin
      FileName := Mail.Values['attachment'+inttostr(i1)];
      Attachments[i1].ulReserved := 0;
      Attachments[i1].flFlags    := 0;
      Attachments[i1].nPosition  := ULONG($FFFFFFFF);
      Attachments[i1].lpszPathName := StrNew(PChar(FileName));
      Attachments[i1].lpszFileName := StrNew(PChar(ExtractFileName(FileName
 )));
      Attachments[i1].lpFileType := nil;
    end;

    MapiMessage.nFileCount := AttachCount;
    MapiMessage.lpFiles := @Attachments^;
  end;

  if Mail.Values['subject'] <> ''
  then
    MapiMessage.lpszSubject := StrNew(PChar(Mail.Values['subject']));
  if Mail.Values['body'] <> ''    
  then
    MapiMessage.lpszNoteText
 := StrNew(PChar(Mail.Values['body']));

  Result := MapiSendMail(0, Handle, MapiMessage,MAPI_DIALOG*Ord(Handle<>0)
 or MAPI_LOGON_UI or MAPI_NEW_SESSION, 0);

  for i1 := 0 to AttachCount-1
  do
  begin
    strDispose(Attachments[i1].lpszPathName);
    strDispose(Attachments[i1].lpszFileName);
  end;

  if assigned(MapiMessage.lpszSubject)  
  then
    strDispose(MapiMessage.lpszSubject);
  if assigned(MapiMessage.lpszNoteText)
  then
    strDispose(MapiMessage.lpszNoteText);
  if assigned(Receip.lpszAddress)      
  then
    strDispose(Receip.lpszAddress);
  if assigned(Receip.lpszName)          
  then
    strDispose(Receip.lpszName);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  mail : TStringList
begin
  mail := TStringList.Create;
  mail.values['to']:='xyz@zz.com';
  mail.values['subject'] := 'no subject';
  mail.values['body'] := 'no subject';
  mail.values['body'] := 'no subject';
  mail.values['attachment0'] := 'c:\winnt\winnt256.bmp';
  mail.values['attachment1'] := 'c:\winnt\winnt.bmp';
  sendEMail(application.handle, mail);
  mail.free;
end;

The Neil