Create outlook message.

How do I create an outlook message (providing address, subject and body) without using Shell commands?  (They are restricted from running all but a few programs...)
loucksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RBertoraCommented:
You can do it using OLE/COM
here is most of the code from one of my apps:

unit MainEmail01;

{ This is a very simple form that interfaces into the microsoft outlook email
  ole server, it obtains all the email addresses and aliases stored in the post box.

  The user can attach documents and write a short email, and the send the email to
  any address stored in the Memorecipients list

  This screen is can only be called by pressing the email button from the events
  screen, this button is only enabled if the associated event has an event category
  code of type Email.

}


interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons,ComObj,MyStringHandler01,DBTables,RobUtils, Menus;

type
  TFormEmail = class(TForm)
    BitBtn5: TBitBtn;
    BitBtn2: TBitBtn;
    GroupBox1: TGroupBox;
    ListBoxEmails: TListBox;
    GroupBox2: TGroupBox;
    MemoRecipients: TMemo;
    GroupBox3: TGroupBox;
    ListBoxFileNames: TListBox;
    GroupBox4: TGroupBox;
    MemoText: TMemo;
    GroupBox5: TGroupBox;
    ListBoxEventDocuments: TListBox;
    Label1: TLabel;
    EditSubject: TEdit;
    BitBtn1: TBitBtn;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Mailbombsomeone1: TMenuItem;
    procedure BitBtn5Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure ListBoxEmailsDblClick(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure ListBoxEventDocumentsDblClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);

    procedure Mailbombsomeone1Click(Sender: TObject);
  private
    SecretDocumentNames : TStringList;
    AdditionalEmails : Integer;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormEmail: TFormEmail;
  OutLookObject : Variant;
  NameSpaceObject : Variant;
  AddressList : Variant;
  MailObject : Variant;

implementation

{$R *.DFM}

// this loads outlook email adress list into a listbox.
procedure TFormEmail.FormActivate(Sender: TObject);
var
  LoopVariable : Integer;
  TempString : String;
  TempQuery : TQuery;
begin
  ListBoxFileNames.Clear;
  ListBoxEmails.Clear;
  ListBoxEventDocuments.Clear;
  MemoRecipients.Clear;
  SecretDocumentNames := TStringList.Create;
  EditSubject.Text := '';

  CreateTempQuery(FormClients.Database1.DatabaseName,TempQuery,Form1);
  OpenTempQuery(TempQuery,'Select EmailAddress from contacts where clientnumber = ' +
                FormEvents.GlobalClientNumber );
  AdditionalEmails := 0;
  for LoopVAriable := 0 to TempQuery.RecordCount-1 do
  begin
    ListBoxEmails.Items.Add(TempQuery.Fields[0].AsString);
    TempQuery.Next;
    Inc(AdditionalEmails);
  end;

  OutLookObject := CreateOleObject('Outlook.Application');
  NameSpaceObject := OutLookObject.GetNameSpace('MAPI');
  AddressList := NameSpaceObject.AddressLists('Global Address List');

  for LoopVariable := 1 to AddressList.AddressEntries.Count do
  begin
    ListBoxEmails.Items.Add(AddressList.AddressEntries[LoopVariable].Name);
  end;


  FormEvents.TableAttachments.First;
  for LoopVariable := 0 to FormEvents.TableAttachments.RecordCount-1 do
  begin
    TempString := EmptyStr;
    TempString := 'VER:' + FormEvents.TableAttachments.FieldByName('DocumentVersionNumber').AsString + '  ';
    TempString := Padd(' ',TempString,10);
    TempString := TempString + FormEvents.TableAttachments.FieldByName('Document').AsString;
    ListBoxEventDocuments.Items.Add(TempString);
    SecretDocumentNames.Add(FormEvents.TableAttachments.FieldByName('FileReferenceNumber').AsString);
    FormEvents.TableAttachments.Next;
  end;
  TempQuery.Free;
end;



procedure TFormEmail.ListBoxEmailsDblClick(Sender: TObject);
begin
  if ListBoxEmails.ItemIndex < AdditionalEmails then
    MemoRecipients.Lines.Add(ListBoxEmails.Items[ListBoxEmails.ItemIndex])
  else
   MemoRecipients.Lines.Add(AddressList.AddressEntries[ListBoxEmails.ItemIndex+1-AdditionalEmails].Address);
end;


// ok this is the MAIN procedure for sending emails:
procedure TFormEmail.BitBtn2Click(Sender: TObject);
var
  Loopvariable,LoopVariableB : Integer;
  Body : String;
begin
  MailObject := OutLookObject.CreateItem(0);

  for Loopvariable := 0 to MemoRecipients.Lines.Count -1 do
    MailObject.Recipients.Add(MemoRecipients.Lines[LoopVariable]);

  MailObject.Subject := EditSubject.Text;

  Body := EmptyStr;
  for LoopVariableB := 0 to MemoText.Lines.Count-1 do
    Body := Body + MemoText.Lines[LoopVariableB] + #10;

  MailObject.Body :=Body;

  for LoopVariable := 0 to ListBoxFileNames.Items.Count -1 do
  begin
    MailObject.Attachments.add(ListBoxFileNames.Items[LoopVariable]);
  end;

  MailObject.Send;
//    Form1.SystemTracker.SaveEvent(FormEvents.Query1.Fieldbyname('ClientName').AsString,'None','Sent email subject : ' + EditSubject.Text  + ' to ' + MemoRecipients.Lines[0]);
   Form1.SaveEvent(FormEvents.Query1.Fieldbyname('ClientName').AsString,'None','Sent email: ' + EditSubject.Text  + ' to ' + MemoRecipients.Lines[0]);
  showmessage('Mail sent!');


end;


procedure TFormEmail.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  OutLookObject.Quit;
  SecretDocumentNames.Free;
end;




procedure TFormEmail.ListBoxEventDocumentsDblClick(Sender: TObject);
var
  FullSecretFileName : String;
  NewFileName : String;
begin
  if messagedlg('A copy of this document is about to be exported for email attachment, proceed?',mtconfirmation,[mbyes,mbno],0) = mrno then exit;
  FullSecretFileName := FormEvents.GlobalSavedirectory + SecretDocumentNames.Strings[ListBoxEventDocuments.ItemIndex];
  NewFileName := Copy(ListBoxEventDocuments.Items[ListBoxEventDocuments.ItemIndex],11,Length(ListBoxEventDocuments.Items[ListBoxEventDocuments.ItemIndex]));


  if CopyFile(Pchar(FullSecretFileName),pchar(NewFileName),false) = false then
  begin
    messagedlg('There was an error moving the file to ' + NewFileName,mterror,[mbok],0);
    exit;
  end;
  ListBoxFileNames.Items.Add(NewFileName);
end;

procedure TFormEmail.BitBtn1Click(Sender: TObject);
begin
  Close;
end;


end.
0
loucksAuthor Commented:
This is what I did...  I need to be able to have the person edit the message b4 they send it.  (I know I could make a memo box and stuff, but what I would like to do is have them see the message...)  If you want more points I could be persuaded...

procedure TForm1.Button1Click(Sender: TObject);
var
  OutLookObject : Variant;
  MailObject : Variant;
begin
  OutLookObject := CreateOleObject('Outlook.Application');
  MailObject := OutLookObject.CreateItem(0);
  MailObject.Recipients.Add('loucks@aim.andrews.edu');
  MailObject.Body := 'This is a test...';
  MailObject.Subject := 'Hi there...';
  MailObject.Send;
end;
0
RBertoraCommented:
Hmm I am a bit confused as to what you want.. if you want the user to type the message then drop a memo for the message and a editbox for the subject..

then you can modify your existing code to look at the message fields on teh form before mailing:

MailObject.Body := Memo1.Lines.Text;
MailObject.Subject := Edit1.Text;

Rob ;-)
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

loucksAuthor Commented:
nope nope... what I wanted was
instead of MailObject.Send, MailObject.Display...  I finally found the $#(%)%$#*@(%$# OLB file...  This cleared everything up...

Now, it seems like the
  OutLookObject := CreateOleObject('Outlook.Application');
  code creates a new app, is there a way to use the one that is already started?  If not, just submit that as an answer and you will get your points.  Otherwise, you could tell me how to do that too...
0
RBertoraCommented:
use GetactiveOleObject instead.
Rob ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
loucksAuthor Commented:
found it...  do this:

 try
  OutLookObject := GetActiveOLEObject('Outlook.Application');
 except
   OutLookObject := CreateOLEObject('Outlook.Application');
 end;

answer this question and collect your points...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.