Solved

Send message to another Exchange mailbox

Posted on 2001-06-14
7
244 Views
Last Modified: 2010-05-18
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
Comment
Question by:JDN
  • 4
  • 2
7 Comments
 
LVL 1

Expert Comment

by:MaxSCZ
ID: 6190886
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
 
LVL 1

Accepted Solution

by:
Phoenix_s earned 50 total points
ID: 6193259
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
 
LVL 1

Author Comment

by:JDN
ID: 6196548
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 1

Expert Comment

by:Phoenix_s
ID: 6196922
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
 
LVL 1

Expert Comment

by:Phoenix_s
ID: 6196927
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
 
LVL 1

Expert Comment

by:Phoenix_s
ID: 6196944
MaxSCZ's proc works too except you gotta have shellAPI in the uses clause...
0
 
LVL 1

Author Comment

by:JDN
ID: 6201159
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

792 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