Send an email with attachments by shellexecute to outlook express

Spread the love

Shellexecute(Handle,’open’,’mailto:[email protected]?subject&body=body
text&CC=aaaa&bcc=dddd&attach=FileName’,nil,nil,SW_SHOW)
only works with outlook, not for outlook express,
but the method below can send attachment to outlook express

uses
  ComObj;

procedure SendMail(Subject, Body, RecvAddress : string; Attachs : array of string);
var
  MM, MS : Variant;
  i : integer;
begin
  MS := CreateOleObject('MSMAPI.MAPISession');
  try
    MM := CreateOleObject('MSMAPI.MAPIMessages');
    try
      MS.DownLoadMail := False;
      MS.NewSession := False;
      MS.LogonUI := True;
      MS.SignOn;
      MM.SessionID := MS.SessionID;

      MM.Compose;

      MM.RecipIndex := 0;
      MM.RecipAddress := RecvAddress;
      MM.MsgSubject := Subject;
      MM.MsgNoteText := Body;

      for i := Low(Attachs) to High(Attachs) do
      begin
        MM.AttachmentIndex := i;
        MM.AttachmentPathName := Attachs[i];
      end;
      MM.Send(True);
      MS.SignOff;
    finally
      VarClear(MS);
    end;
  finally
    VarClear(MM);
  end;
end;

procedure TForm1.FormCreate(Sender : TObject);
begin
  SendMail('Subject', 'Body'#13#10'Second', '[email protected]',
    ['C:\Winnt\explorer.exe', 'C:\winnt\win.ini']);
end;