Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2000-02-16
2
Medium Priority
?
450 Views
Last Modified: 2010-04-04
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



0
Comment
Question by:Schmoekel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 800 total points
ID: 2526374
You can add body text like this in a shell execute command:

s := 'mailto:'+e+'?subject=Vielen Dank...!?Body=Some text';

Unfortunately you can't use shellexecute to attach files to a mail message. Below are a couple examples using MAPI and the SakEmail component to compose and send mail using attachments.

The TNMSMTP component shipped with Delphi can also do this (though I do not have a code sample to hand).

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;

Or you can use something like the SakEmail component and send a mail like this:

procedure TSVOExceptForm.Button1Click(Sender: TObject);

begin
  SakSMTP1.host := 'smtp.trimble.co.nz';
   SakMsg1.CharSet := 'ISO-8859-1';    
   SakMsg1.TextEncoding := te8Bit;
       
SakMsg1.UserName := 'Fred';
   SakMsg1.From := '<Fred@fred.com'+'>';
   SakMsg1.SendTo := 'mary@fred.com';
   SakMsg1.Subject := 'Subject line';
   SakMsg1.Text.Assign(Memo2.Lines);
   SakMsg1.Text.append(' ');
   SakMsg1.Text.AddStrings(Memo1.lines);
   SakSMTP1.connect;
   if not SakSMTP1.SMTPError then
   begin
      SakSMTP1.SendTheMessage( SakMsg1);
      SakSMTP1.quit;
      ShowMessage('Message sent to '+ SakMsg1.SendTo);
   end;
end;

Cheers,

Raymond.
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2526384
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

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question