Solved

Send message to another Exchange mailbox

Posted on 2001-06-14
7
233 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:Phoenix_s
Comment Utility
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
Comment Utility
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
Comment Utility
MaxSCZ's proc works too except you gotta have shellAPI in the uses clause...
0
 
LVL 1

Author Comment

by:JDN
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now