• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

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
0
JDN
Asked:
JDN
  • 4
  • 2
1 Solution
 
MaxSCZCommented:
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
0
 
Phoenix_sCommented:
this works too, although it does require outlook to be installed on the PC.




wherever you put this has to have   comobj   in the USES clause!
----------------------------------------------------------------
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;

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

to use, just call email(sendto,subject,body)

* sender can be multiple recipients seperated by ';'  the procedure will parse the individual recipients out appropriately and add them to the TO: field in the message.

0
 
JDNAuthor Commented:
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Phoenix_sCommented:
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.
----------------------------------------------------------------------------



0
 
Phoenix_sCommented:
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
0
 
Phoenix_sCommented:
MaxSCZ's proc works too except you gotta have shellAPI in the uses clause...
0
 
JDNAuthor Commented:
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now