Board index » delphi » sending mail using MAPI , CDO, SMTP

sending mail using MAPI , CDO, SMTP

Hi all,
I have a peculiar problem. Earlier, we were using wsendmail [a third party
tool] for sending emails. This tool does not work when we use Exchange
Server 2000.
So i tried our SMTP component to send message. This works fine for Exchange
Server 5.5 but when used with exchange server 2000, it gives a error
message, "555 5.2.5 Invalid address".

so i tried using MAPI... My piece of code was this

MapiLogOn(0, nil,nil,MAPI_EXTENDED or MAPI_NEW_SESSION or
MAPI_USE_DEFAULT, 0, FMapiSession);

MapiSendMail(0, 0, MapiMsg, 0, 0);

This works fine only when outlook is opened in the background else it gives
MAPI_E_LOGIN_FAILURE error. We cant expect the client to open his outlook
always.
When i tried changing the flag to 0, it works with my local machine, but
when using in IIS server, it gives MAPI_E_FAILURE error.

Can anyone help me out?

Thanks
Subha Narayanan

 

Re:sending mail using MAPI , CDO, SMTP


You cannot use Simple MAPI under anything running as a service, including IIS.
If all you want is sending SMTP messages, why not use Indy?

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

Quote
"Subha Narayanan" <subha...@hotmail.com> wrote in message

news:3c674f50_2@dnews...
Quote
> Hi all,
> I have a peculiar problem. Earlier, we were using wsendmail [a third party
> tool] for sending emails. This tool does not work when we use Exchange
> Server 2000.
> So i tried our SMTP component to send message. This works fine for Exchange
> Server 5.5 but when used with exchange server 2000, it gives a error
> message, "555 5.2.5 Invalid address".

> so i tried using MAPI... My piece of code was this

> MapiLogOn(0, nil,nil,MAPI_EXTENDED or MAPI_NEW_SESSION or
> MAPI_USE_DEFAULT, 0, FMapiSession);

> MapiSendMail(0, 0, MapiMsg, 0, 0);

> This works fine only when outlook is opened in the background else it gives
> MAPI_E_LOGIN_FAILURE error. We cant expect the client to open his outlook
> always.
> When i tried changing the flag to 0, it works with my local machine, but
> when using in IIS server, it gives MAPI_E_FAILURE error.

> Can anyone help me out?

> Thanks
> Subha Narayanan

Re:sending mail using MAPI , CDO, SMTP


Thanks, is there any sample for using Indy?
I had earlier downloaded one sample and it doesnot work local server.

Quote
"Dmitry Streblechenko" <dmi...@dimastr.com> wrote in message

news:3c67f979_2@dnews...
Quote
> You cannot use Simple MAPI under anything running as a service, including
IIS.
> If all you want is sending SMTP messages, why not use Indy?

> Dmitry Streblechenko (MVP)
> http://www.dimastr.com/
> OutlookSpy  - Outlook, CDO
> and MAPI Developer Tool

> "Subha Narayanan" <subha...@hotmail.com> wrote in message
> news:3c674f50_2@dnews...
> > Hi all,
> > I have a peculiar problem. Earlier, we were using wsendmail [a third
party
> > tool] for sending emails. This tool does not work when we use Exchange
> > Server 2000.
> > So i tried our SMTP component to send message. This works fine for
Exchange
> > Server 5.5 but when used with exchange server 2000, it gives a error
> > message, "555 5.2.5 Invalid address".

> > so i tried using MAPI... My piece of code was this

> > MapiLogOn(0, nil,nil,MAPI_EXTENDED or MAPI_NEW_SESSION or
> > MAPI_USE_DEFAULT, 0, FMapiSession);

> > MapiSendMail(0, 0, MapiMsg, 0, 0);

> > This works fine only when outlook is opened in the background else it
gives
> > MAPI_E_LOGIN_FAILURE error. We cant expect the client to open his
outlook
> > always.
> > When i tried changing the flag to 0, it works with my local machine, but
> > when using in IIS server, it gives MAPI_E_FAILURE error.

> > Can anyone help me out?

> > Thanks
> > Subha Narayanan

Re:sending mail using MAPI , CDO, SMTP


I am only aware of the samples at http://www.nevrona.com/indy/

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy  - Outlook, CDO
and MAPI Developer Tool

Quote
"Subha Narayanan" <subha...@hotmail.com> wrote in message

news:3c69faf9_1@dnews...
Quote
> Thanks, is there any sample for using Indy?
> I had earlier downloaded one sample and it doesnot work local server.

> "Dmitry Streblechenko" <dmi...@dimastr.com> wrote in message
> news:3c67f979_2@dnews...
> > You cannot use Simple MAPI under anything running as a service, including
> IIS.
> > If all you want is sending SMTP messages, why not use Indy?

> > Dmitry Streblechenko (MVP)
> > http://www.dimastr.com/
> > OutlookSpy  - Outlook, CDO
> > and MAPI Developer Tool

> > "Subha Narayanan" <subha...@hotmail.com> wrote in message
> > news:3c674f50_2@dnews...
> > > Hi all,
> > > I have a peculiar problem. Earlier, we were using wsendmail [a third
> party
> > > tool] for sending emails. This tool does not work when we use Exchange
> > > Server 2000.
> > > So i tried our SMTP component to send message. This works fine for
> Exchange
> > > Server 5.5 but when used with exchange server 2000, it gives a error
> > > message, "555 5.2.5 Invalid address".

> > > so i tried using MAPI... My piece of code was this

> > > MapiLogOn(0, nil,nil,MAPI_EXTENDED or MAPI_NEW_SESSION or
> > > MAPI_USE_DEFAULT, 0, FMapiSession);

> > > MapiSendMail(0, 0, MapiMsg, 0, 0);

> > > This works fine only when outlook is opened in the background else it
> gives
> > > MAPI_E_LOGIN_FAILURE error. We cant expect the client to open his
> outlook
> > > always.
> > > When i tried changing the flag to 0, it works with my local machine, but
> > > when using in IIS server, it gives MAPI_E_FAILURE error.

> > > Can anyone help me out?

> > > Thanks
> > > Subha Narayanan

Re:sending mail using MAPI , CDO, SMTP


Hi,

Recently I wrote a very simple NT service which periodically reads mail from Exchange Server and sends response (See code below).
There are several different CDO objects. With Exchange you have to use CDO 1.21 which is located in MS Outlook distributive and not installed by default ("c:\Program Files\Common Files\System\Mapi\1033\NT\CDO.DLL").
After its installation you have to import "Microsoft CDO 1.21 Library" (It is required to rename TSession to TCDOSession or something else). It will produce file MAPI_TLB.pas. After installation of the service go to "Log On" tab of service Properties dialog and put domain\username and password. Use the same username in Session.Logon procedure. I think this security settings depend on your network setting, but they worked in my case.
For further development you can refer to MSDN or/and www.cdolive.com (http://www.cdolive.com/cdo5.htm page I found the most useful at the beginning)

Regards,

Vladimir

/////
NT Service code which uses CDO to work with Exchange:
/////

unit UTMService;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;

type
  TTMExecThread = class(TThread)
  private
    procedure SendMail;
  protected
    procedure Execute; override;
  end;

  TTMService = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
  private
    FExecThread: TTMExecThread;
  public
    function GetServiceController: TServiceController; override;
  end;

var
  TMService: TTMService;

implementation

uses
  ActiveX, MAPI_TLB;

{$R *.DFM}

procedure LogError(const Msg: String);
var
  S: TStream;
begin
  S := TFileStream.Create('c:\SimpeErrorLog.txt', fmCreate);
  try
    S.Write(Msg[1], Length(Msg));
  finally
    S.Free;
  end;
end;

{ TTMExecThread }

procedure TTMExecThread.Execute;
begin
  CoInitialize(nil);
  try
    while not Terminated do
    begin
      Synchronize(SendMail);
      Sleep(3000);
    end;
  finally
    CoUnInitialize;
  end;
end;

procedure TTMExecThread.SendMail;
var
  CDOSession: MAPI_TLB.Session;
  CDOMessage, CDONewMessage: MAPI_TLB.Message;
  CDORecipient: MAPI_TLB.Recipient;
  CDOMessages: MAPI_TLB.Messages;
begin
  try
    CDOSession := CoSession.Create;
    //!!! change Servername to computer name of your exchange server
    // Username and password are empty because I am using security settings for NT service
    // Using Null instead of UnAssigned produce error in NT service
    CDOSession.Logon(UnAssigned, UnAssigned, False, True, 0, False, 'Servername'#10'username');
    try
      CDOMessages := IDispatch(CDOSession.Inbox.Messages) as MAPI_TLB.Messages;
      CDOMessage := IDispatch(CDOMessages.GetFirst(UnAssigned)) as MAPI_TLB.Message;
      while Assigned(CDOMessage) do
      begin
        if CDOMessage.Unread then
        begin
          CDOMessage.Unread := False;
          CDOMessage.Update(True, True);
          CDONewMessage := IDispatch(CDOSession.Outbox.Messages.Add) as MAPI_TLB.Message;
          CDONewMessage.Subject := 'This is a message created with CDO';
          CDONewMessage.Text := 'Welcome to the world of CDO. Enjoy your life!';
          CDORecipient := IDispatch(CDONewMessage.Recipients.Add) as MAPI_TLB.Recipient;
          CDORecipient.Name := CDOMessage.Sender;
          CDORecipient.Resolve(False);
          CDONewMessage.Update(True, True);
          CDONewMessage.Send(False, False, 0);
        end;
        CDOMessage := IDispatch(CDOMessages.GetNext) as MAPI_TLB.Message;
      end;
    finally
      CDOSession.Logoff;
    end;
  except
    on E: Exception do
      LogError(E.Message);
  end;
end;

{ TTMService }

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  TMService.Controller(CtrlCode);
end;

function TTMService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TTMService.ServiceStart(Sender: TService; var Started: Boolean);
begin
  FExecThread := TTMExecThread.Create(False);
  FExecThread.Priority := tpLower;
  FExecThread.FreeOnTerminate := True;
  Started := True;
end;

procedure TTMService.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
  FExecThread.Terminate;
  FExecThread := nil;
  Stopped := True;
end;

end.

Re:sending mail using MAPI , CDO, SMTP


Quote
"Vladimir Morozov" <vmo...@hotmail.com> wrote in message <news:3c6d47d3$1_1@dnews>...
> Hi,

> Recently I wrote a very simple NT service which periodically reads mail from Exchange Server and sends response (See code below).

Vladimir,

I'm not a Delphi expert, but I've written a few small applications and
worked on a few others.  There are a couple of things about your
sample that I don't understand.

When I copied the code into a new unit, Delphi didn't seem to want to
compile without a .DFM file.  So I added a blank form and copied the
code into that.  My question is how do you create a .DFM file without
creating a form?  I am using Delphi 5 professional.

My other question is: How do you call this from your application?
Nothing I have tried seems to work.

Thanks,

Donna Manus

Re:sending mail using MAPI , CDO, SMTP


I can see the header of your post on my ISP's copy of this
newsgroup, but it hasn't been sent to Borland's news
server.
That means that most of the people who read Borland's
newsgroups cannot see your post.

To post messages that other people will see, you need to
connect to Borland's server, newsgroups.borland.com,
directly. Here's a link with instructions if you don't know
how:
http://www.borland.com/newsgroups/newsinfo.html

If you cannot connect directly to newsgroups because
of a firewall, you can post to Borland's newsgroups
via the web pages at http://newsgroups.borland.com/

--
Deborah Pate (TeamB) http://delphi-jedi.org

  TeamB don't see posts sent via Google or ISPs
  Use the real Borland server: newsgroups.borland.com
  http://www.borland.com/newsgroups/genl_faqs.html

Other Threads