[Delphi] Gebruik maken van Exchange

Pagina: 1
Acties:
  • 191 views sinds 30-01-2008
  • Reageer

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ik heb een klein programmaatje geschreven wat een tekst via e-mail moet verzenden binnen ons netwerk.
We werken hier met Exchange.

Hoe krijg ik het in Delphi voor elkaar om iets te versturen?

Acties:
  • 0 Henk 'm!

  • whoami
  • Registratie: December 2000
  • Laatst online: 13:23
Op uw delphi component palette heb je daar een mooi tabje 'FastNet'. Kijk daar eens naar.

https://fgheysels.github.io/


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
In delhpi 4 ook?, Heb even rondgekeken en in help gezocht maar zie niks.....

Acties:
  • 0 Henk 'm!

  • whoami
  • Registratie: December 2000
  • Laatst online: 13:23
Waar het in Delphi 4 zit weet ik niet. (Misschien op de Internet tab?)
Maar ik denk wel dat je minstens de Professional versie moet hebben.

https://fgheysels.github.io/


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Heb client/server suite van delphi 4 hier geinstaleerd.
Ik zal anders vanmidag thuis (Delphi 6) eens kijken, misschien dan meer resultaat.

Acties:
  • 0 Henk 'm!

  • SchizoDuckie
  • Registratie: April 2001
  • Laatst online: 18-02 23:12

SchizoDuckie

Kwaak

* PaPa Eend is ook druk bezig met exchange (server) + delphi

The keyword is MAPI.

Er zijn iig voor het verzenden van exchange e-mail binnen delphi mooie componentjes te vinden op http://www.torry.net .

Voor de wat professionelere grappen zoals interfacen met de exchange server heb ik wat uitgebreidere examples (laten) kopen. Werkt allemaal best perfect :7

Ik ga (hopelijk deze week) eerst maar eens een test-server opzetten om mee te rommelen.

Stop uploading passwords to Github!


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Geloof dat ik het idd heb!
Simpele MAPI applicatie zonder component en het werkt tot nu toe!

Bedankt!

Acties:
  • 0 Henk 'm!

  • SchizoDuckie
  • Registratie: April 2001
  • Laatst online: 18-02 23:12

SchizoDuckie

Kwaak

Op donderdag 06 juni 2002 12:52 schreef EM het volgende:
Bedankt!
Glad to help :)

Stop uploading passwords to Github!


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ik heb nu dat programma aardig draaien en werkt allemaal ook wel alleen ik moet Outlook aan hebben staan anders verstuurd hij niks en geeft het programma een foutmelding.
Hoe kan ik ervoor zorgen dat ik geen outlook meer nodig heb?

Hier de code van het mailprog:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  RECIP_MAX  = MaxInt div SizeOf(TMapiRecipDesc);
  ATTACH_MAX    = MaxInt div SizeOf(TMapiFileDesc);

type
  { array of structures for TO, CC, and BCC }
  TRecipAccessArray     = array [0 .. (RECIP_MAX - 1)] of TMapiRecipDesc;
  TlpRecipArray    = ^TRecipAccessArray;
  { array of structures for attachments }
  TAttachAccessArray    = array [0 .. (ATTACH_MAX - 1)] of TMapiFileDesc;
  TlpAttachArray      = ^TAttachAccessArray;

var
  frmMain: TfrmMain;
  lppMapiMessage: MapiMessage; // main message info pointer
  hSession: ULONG;
  lppMapiFileDesc: MapiFileDesc; // attachment info pointer
  flFlags: ULONG;
  lparrayRecips: TlpRecipArray;
  lppMapiRecipDesc: TMapiRecipDesc; // recipient info pointer
  lparrayAttachments: TlpAttachArray;


implementation

uses About;

{$R *.DFM}

procedure TfrmMain.btnOKClick(Sender: TObject);
var s : string;
    x :  integer;
    err: ULONG;
    AnyStr: PChar;
    nAttachments,
    nRecipients: Cardinal;
Label Fout, einde, fout2;
begin

txtSubject.text := ParketEdit.text + ' ' + DateEdit.text;
TelMemo.lines.savetofile('c:\' + ParketEdit.text + ' ' + DateEdit.text + '.txt');
txtAttachment.text := ('c:\' + ParketEdit.text + ' ' + DateEdit.text + '.txt');
     { This routine is going to simply make sure
     that all information are properly entered
     subject, to, and message body }
     if (txtTo.Text <> '') and (txtSubject.Text <> '') and
      (moBody.Lines.Count > 0) then
     begin
        nAttachments := 1;
        nRecipients := 0;
        if txtTo.Text <> '' then
         Inc(nRecipients);
        if txtCC.Text <> '' then
         Inc(nRecipients);
        if txtBCC.Text <> '' then
         Inc(nRecipients);

        { assign 0 to all structure options }
        FillChar(lppMapiRecipDesc, SizeOf(TMapiRecipDesc), 0);
        lparrayRecips  := TlpRecipArray(StrAlloc(nRecipients*SizeOf(TMapiRecipDesc)));
        FillChar(lparrayRecips^, StrBufSize(PChar(lparrayRecips)), 0);
        lparrayAttachments := TlpAttachArray(StrAlloc(nAttachments*SizeOf(TMapiFileDesc)));
        FillChar(lparrayAttachments^, StrBufSize(PChar(lparrayAttachments)), 0);

        { check to see if there is file to attach }

        if txtAttachment.Text <> '' then
        begin
        { info about Attachment}
           lparrayAttachments^[0].ulReserved := 0;
           lparrayAttachments^[0].flFlags := 0;
           lparrayAttachments^[0].nPosition := ULONG($FFFFFFFF);
           AnyStr := StrAlloc(length(txtAttachment.Text)+1);
           StrPCopy(AnyStr,txtAttachment.Text);
           lparrayAttachments^[0].lpszPathName := AnyStr;
           AnyStr := nil;
           StrDispose(AnyStr);
           AnyStr := StrAlloc(length(ExtractFileName(txtAttachment.Text))+1);
           StrPCopy(AnyStr,ExtractFileName(txtAttachment.Text));
           lparrayAttachments^[0].lpszFileName := AnyStr;
           AnyStr := nil;
           StrDispose(AnyStr);
           lparrayAttachments^[0].lpFileType := nil;
        end;
        
        { info about recipient }
        lparrayRecips^[0].ulReserved := 0;
        lparrayRecips^[0].ulRecipClass := MAPI_TO;
        AnyStr := StrAlloc(length(txtTo.Text)+1);
        StrPCopy(AnyStr,txtTo.Text);
        lparrayRecips^[0].lpszName := AnyStr;
        lparrayRecips^[0].lpszAddress := nil;
        lparrayRecips^[0].ulEIDSize := 0;
        lparrayRecips^[0].lpEntryID := nil;
        AnyStr := nil;
        StrDispose(AnyStr);

        { info about CC }
        if txtCC.Text <> '' then
        begin
           AnyStr := StrAlloc(length(txtCC.Text)+1);
           StrPCopy(AnyStr,txtCC.Text);
           lparrayRecips^[1].ulReserved := 0;
           lparrayRecips^[1].ulRecipClass := MAPI_CC;
           lparrayRecips^[1].lpszName := AnyStr;
           lparrayRecips^[1].lpszAddress := nil;
           lparrayRecips^[1].ulEIDSize := 0;
           lparrayRecips^[1].lpEntryID := nil;
           AnyStr := nil;
           StrDispose(AnyStr);
        end;

        { info about BCC }
        if txtBCC.Text <> '' then
        begin
           AnyStr := StrAlloc(length(txtBCC.Text)+1);
           StrPCopy(AnyStr,txtBCC.Text);
           lparrayRecips^[2].ulReserved := 0;
           lparrayRecips^[2].ulRecipClass := MAPI_BCC;
           lparrayRecips^[2].lpszName := AnyStr;
           lparrayRecips^[2].lpszAddress := nil;
           lparrayRecips^[2].ulEIDSize := 0;
           lparrayRecips^[2].lpEntryID := nil;
           AnyStr := nil;
           StrDispose(AnyStr);
        end;

        { main structure that is used to send the message }
        lppMapiMessage.ulReserved := ULONG(0);
        lppMapiMessage.lpszSubject := PChar(txtSubject.Text);
        lppMapiMessage.lpszNoteText := PChar(moBody.Lines.Text);
        lppMapiMessage.lpszMessageType := nil;
        lppMapiMessage.lpszDateReceived := nil;
        lppMapiMessage.lpszConversationID := nil;
        lppMapiMessage.flFlags := ULONG(0);
        lppMapiMessage.lpOriginator := nil;
        lppMapiMessage.nRecipCount := nRecipients;
        lppMapiMessage.lpRecips := @lparrayRecips^;
        lppMapiMessage.nFileCount := nAttachments;
        lppMapiMessage.lpFiles := @lparrayAttachments^;
        { drops the mail in the outbox
        Make sure you check the option to send mail
        immediately in your Exchange or Outlook }
        err := MAPISendMail(0,0,lppMapiMessage,0,0);
        if err <> SUCCESS_SUCCESS then
        ShowMessage('Error');

     end;
DeleteFile ('c:\' + ParketEdit.text + ' ' + DateEdit.text + '.txt');
end;

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
klein schopje, doe dit anders nooit, maar wil (moet eigenlijk) dit probleem voor het weekend hebben opgelost...... plz iemand een idee?

Acties:
  • 0 Henk 'm!

  • yodax
  • Registratie: Januari 2000
  • Laatst online: 11-08 03:06
Ik heb dit wel eens gemaakt, maar heb een andere oplossing gekozen.

Ik heb gebruik gemaakt van het COM object van MAPI.

In het onderstaande stuk code wordt er een verbinding gemaakt met de exchange server.
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function TMarbelMAPI.Connect: boolean;
begin
  mConnected := false;

  try
    // Is de sessie al connected log dan af
    if mConnected then
    mMapiSession.Logoff;

    // Roep coinitialize aan om te zorgen dat het ole object geschikt wordt,
    // voor multi threading.
    CoInitializeEx (NIL, 2);

    // Maak een nieuwe MapiSessie instantie
    mMapiSession := CreateOleObject('Mapi.Session');

    // Login en geruik de variabelen die uit de text box gehaald zijn
    mMapiSession.Logon(mProfile, mPassword, True, True);
    mConnected := True; // Zet connected op true
    Connected;
  except
  on E:Exception do
  begin
    GenerateErrorEvent( 'Can not establish a connection (with: ' + mProfile + ', ' 
+ mPassword + ' as param): ' + E.Message , 43, 1);
    mConnected := false;
  end;
  end;

  Connect := mConnected;
end;

Voor het gemaak geef ik je ook even de variabelen van mijn componentje.

{ Connection variables }
mConnected : boolean;
mConnectionString : string;
mFolder : TStringList;
mProfile : string; // Stores the profile name
mPassword : string;
mIsMail : boolean;
mMessage : TMailMessage;

{ Ole en folder stores }
mMapiFolder : variant;
mMapiSession : variant;
mMapiSubFolder : variant;
mSubFolder : string;

{ ReScanFolder flags }
mFolderChanged : boolean;
mUseBackupSubFolder : boolean;
mSetSubFolder : boolean;

{ Custom Events }
fConnected : TNotifyEvent;
fOnError : TErrorMsg;
Alle functies voor het COM object staan in de MSDN.

MAPI reference op de MSDN
Zoeken op MAPI op de MSDN

In dit voorbeeld maak ik gebruik van late binding, het is beter om gebruik te maken van early binding, dit kun je bereiken door de Type Library te importen in je project (dit zit onder het menu project) Delphi maakt dan een .pas file van het com object zodat je ook tijdens het ontwerpen type information hebt. (m.a.w. code completion werkt)

Tot slot nog even de code om een bericht te verzenden
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function TMarbelMAPI.SendMail(mailMessage: TMailMessage): boolean;
var
  MapiTempMessage : variant;
  MapiTempRecipiant : variant;
begin
  if not mConnected then
  begin
    if not Connect then
    begin
    GenerateErrorEvent('Can not send mail, because i can not establish a connection.',32,1);
    Result := false;
    exit;
    end;
  end;

  try
    MapiTempMessage := mMapiSession.Outbox.Messages.Add;
  except
  on E:Exception do
  begin
    GenerateErrorEvent('Can not create a new message: ' + E.Message, 33,1);
    Result := false;
    exit;
  end;
  end;

  try
    MapiTempMessage.Subject := mailMessage.mSubject;
    MapiTempMessage.Text := mailMessage.mMessageText;
  except
  on E:Exception do
  begin
    GenerateErrorEvent('Can not fill the message: ' + E.Message, 34,2);
  end;
  end;

  try
    MapiTempRecipiant := MapiTempMessage.Recipients.Add;
    MapiTempRecipiant.Name := mailMessage.mRecipientAddress;
    MapiTempRecipiant.Resolve;
  except
  on E:Exception do
  begin
    GenerateErrorEvent('Can not apply the recipient: ' + E.Message, 35, 2);
  end;
  end;

  try
    MapiTempMessage.Update;
    MapiTempMessage.Send;
  except
  on E:Exception do
  begin
    GenerateErrorEvent('Can not send the message: ' + E.Message, 36,1);
    Result := false;
    Exit;
  end;
  end;

  Result := true;
end;

Op deze manier heb je dus geen openstaande outlook nodig. Je gaat namelijk rechtstreeks gebruik maken van het mapi object.

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Bedankt, ga hier meteen ff mee stoeien.
Pagina: 1