2007. május 23., szerda
Read any MAPI complient emails
Problem/Question/Abstract:
How can read emails from OutlookExpress, Outlook or Eudora from a Delphi program?
Answer:
We can read emails from any MAPI compliant email client using MAPI. The bad news is that we cannot read formatted email using simple MAPI, but only the plain text and attachments.
That�s because MAPI was created before HTML formatted emails were created. But MAPI can bevery useful anyway.
To run this program create a blank form (on IDE point to "File/Close all", then "File/New Application"), put a button on it, a Label (both on standard tab)and a RichEdit (on Win32 tab; I used a RichEdit because Memo can only handle 32k of text).
unit Unit1;
{*****************************************************************
* Simple MAPI sample *
* Source written by Rafael Cotta (rcotta.geo@yahoo.com) *
* July 26th, 2001 *
*****************************************************************}
// To run this sample, place on a blank form a TRichEdit, a TLabel
// and a TButton
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, MAPI;
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Button1: TButton;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hndMAPISession: Cardinal;
v_CurrentMsgID: array[0..512] of char; // I�ll use long IDs here
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
// Initializing components and variables
v_CurrentMsgID[0] := #0;
hndMAPISession := 0;
Button1.Caption := 'Read Next';
RichEdit1.Lines.Clear;
Label1.Caption := 'Subject : ';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
dwRet: Cardinal;
v_Out: array[0..512] of char;
oMsg: PMapiMessage;
begin
Label1.Caption := 'Retrieving... wait';
if hndMAPISession = 0 then
begin
dwRet := MapiLogOn(
Handle, // This window�s handle
'', // If you want to use another profile that�s
'', // not the default, set these two parameters
MAPI_LOGON_UI, // Will prompt for logon if necessary
0, // Reserve, must be 0
@hndMAPISession);
// An error occurred
if (dwRet <> SUCCESS_SUCCESS) then
begin
MessageBox(Handle, PChar('Error ' + IntToHex(dwRet, 8) +
' while trying to logon'), PChar('Error'),
MB_OK + MB_ICONERROR);
Application.Terminate;
Exit;
end;
end;
dwRet := MapiFindNext(
hndMAPISession,
Handle,
nil, // All messages
@v_CurrentMsgID,
MAPI_GUARANTEE_FIFO {sort by date; you can use "or MAPI_UNREAD_ONLY"},
0,
@v_Out // The messageId of message next to v_Current will be here
);
// Reached the end, so, going to first message
if dwRet = MAPI_E_NO_MESSAGES then
begin
v_CurrentMsgID[0] := #0;
MessageBox(Handle,
PChar('Last message reached. Going to first one in InBox'),
PChar('MAPI sample'),
MB_OK + MB_ICONEXCLAMATION);
dwRet := MapiFindNext(
hndMAPISession,
Handle,
nil, // All messages
@v_CurrentMsgID,
MAPI_GUARANTEE_FIFO {sort by date; you can use "or MAPI_UNREAD_ONLY"},
0,
@v_Out // The messageId of message next to v_Current will be here
);
end;
StrCopy(@v_CurrentMsgID, @v_Out);
dwRet := MapiReadMail(
hndMAPISession,
Handle,
@v_CurrentMsgID,
MAPI_PEEK, // Won�t mark message as read
0,
oMsg
);
if dwRet = SUCCESS_SUCCESS then
begin
RichEdit1.SetTextBuf(oMsg.lpszNoteText);
Label1.Caption := 'Subject : ' + StrPas(oMsg.lpszSubject);
end;
if Assigned(oMsg) then
MapiFreeBuffer(oMsg);
end;
end.
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése