Link to home
Start Free TrialLog in
Avatar of JDN
JDN

asked on

Send message to another Exchange mailbox

Hello,

Can anyone tell me if there's a easy way in Delphi to send a message to an Exchange Server mailbox of another user.
I just want to specify the parameters: mailbox (or e-mail address), subject an message text.

Thanks,
JDN
Avatar of MaxSCZ
MaxSCZ

Hi,
this can help?



procedure TForm1.mnuEmailClick(Sender: TObject);
var
  cCommand: String ;
  cBody: String ;
begin
  cBody := 'Dir sir(s)%0d%0aI would like to inform you that%0d%0athe
following' ;
  cCommand := 'mailto:PersonToSendTo.com'   +
              '?Subject=Subject goes here'  +
              '&cc=AddressToCC.com'         +
              '&bcc=BlindCC.com'            +
              '&body=' + cBody ;

  if ShellExecute(0, nil, PChar(cCommand), nil, nil, SW_SHOWDEFAULT) <= 32
then
  begin
    MessageDlg('Your error message on failure',mtError,[mbOk],0) ;
  end else begin
    { The following attempts to send the email by invoking send via the menu
}
    Sleep(1000);                                  {give mail prog time to
open}
    keybd_Event(VK_MENU, 0, 0, 0);                {get email prog menu}
    keybd_Event(ord('S'), 0, 0, 0);               {s for send}
    keybd_Event(ord('S'), 0, KEYEVENTF_KEYUP, 0); {click}
    keybd_Event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);  {exit menu}
    ShowMessage('Finished!') ;
  end ;
end;


bye Max
ASKER CERTIFIED SOLUTION
Avatar of Phoenix_s
Phoenix_s

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 JDN

ASKER

Phoenix,

When I call procedure email, with the three parameters, I get an error:

Access violation at address 77E1513A in module 'user32.dll'.

I'm using Outlook 2000. The error comes up with Outlook running and with Outlook not running.

Any idea?
JDN: really... cuz that worked with win98 SE, win95, and win2k  with outlook 97,98,and 2k in various permutations

try this-  I just ran it and it works


unit source:
------------------------------------------------------------------------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls,comobj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

procedure email(sendto,subject:string;body:tstrings);

var
  Form1: TForm1;

implementation


procedure email(sendto, subject: string; body: tstrings);
const
 // for email capability
 olFolderDeletedItems = $00000003;
 olFolderOutbox = $00000004;
 olFolderSentMail = $00000005;
 olFolderInbox = $00000006;
 olFolderCalendar = $00000009;
 olFolderContacts = $0000000A;
 olFolderJournal = $0000000B;
 olFolderNotes = $0000000C;
 olFolderTasks = $0000000D;
 olMailItem = 0;

var str1, str2, str3 : string;
   pos1 : integer;
var
 outlook, nmspace, mailitem: olevariant;

begin
 
 try
   outlook := getactiveoleobject('outlook.application');
 except
   outlook := createoleobject('outlook.application');
 end ;
 
 nmspace := outlook.getnamespace('MAPI');
 nmspace.logon(emptyparam, emptyparam, false, true);
 mailitem := outlook.createitem(olmailitem);
 pos1 := pos(';',sendto);
 if pos1 = 0 then
   mailitem.recipients.add(sendto)
 else
   begin
     str3 := sendto;
     repeat
       str1 := copy(str3,1,pos1-1);
       str2 := copy(str3,pos1+1,(length(str3)-length(str1)));
       mailitem.recipients.add(str1);
       str3 := str2;
       pos1 := pos(';',str3);
     until pos1 = 0;
     mailitem.recipients.add(str3);
   end;
 mailitem.subject := subject;
 mailitem.body := body.text;
 mailitem.send;
 nmspace.logoff;
 nmspace := unassigned;
 mailitem := unassigned;
 outlook := unassigned;
end;



{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var emailbody : tstrings;
begin
  emailbody := tstringlist.create;
  emailbody.add('This is a dumb test');
  emailbody.add('this is the second line of the dumb test');


  email('jhack@okspring.com','test email',emailbody);
  emailbody.free;
end;

end.
----------------------------------------------------------

form:
-----------------------------------------------------------------------------------
object Form1: TForm1
  Left = 192
  Top = 123
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 48
    Top = 60
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end

------------------------------------------------------------------------

Project:

------------------------------------------------------------------------
program Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
----------------------------------------------------------------------------



OH... and, this is done in delphi 5, but it has worked with delphi4 as well with no changes to the code.

dunno about delphi 3 though, haven't run it in a couple years... ehhehe
MaxSCZ's proc works too except you gotta have shellAPI in the uses clause...
Avatar of JDN

ASKER

Phoenix,

Now your procedure works.
Perhaps I did something wrong.

MaxSCZ's solution works, but I prefer the OLE call to Outlook. So, I give Phoenix the points.

Thank you both.