TOutlookAplication元件的應用範例
資料來源: Borlan CodeCentral
http://codecentral.borland.com/codecentral/ccweb.exe/listing?id=16437
Title: | Ole with Outlook 2000 example |
Terms: | No Special Terms |
Name: | Roland Wardenaar email: Anonymous |
URL: | None |
Summary: | Shows how to retrieve email messages from Microsoft Outlook 2000. |
Description: | Reading Email messages via Ole.
Made for Outlook 2000.
The program is just an example how to achieve this.
Freeware . |
Product: | C++Builder (1-5) Source Code |
Contest: | None |
Uploaded: | 12-Aug-01 10:25:50 AM last updated 12-Aug-01 10:02:42 AM |
Tools CD: | No |
Copyright: | No significant restrictions |
Size: | 7.0K List Files (1435 downloads) |
Comments: | 1 |
站內下載:http://delphi.ktop.com.tw/loadfile.php?TOPICID=10339342&CC=231238
取得Outlook中的InBox的信件內容 原程式碼:
unit3.h
//--------------------------------------------------------------------------- #ifndef Unit3H
#define Unit3H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
#include "Outlook_2K_SRVR.h"
#include
#include
#include
//---------------------------------------------------------------------------
class TMainForm : public TForm
{
__published: // IDE-managed Components
TOutlookApplication *OA1;
TMemo *M1;
TButton *Next;
TButton *Previous;
TActionList *ActionList;
TAction *aShowMessage;
TButton *First;
TButton *Last;
TEdit *eID;
TEdit *eTo;
TEdit *eSubject;
TEdit *eSender;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TButton *SaveMessage;
TComboBox *Format;
TLabel *Label4;
TEdit *eCC;
TRadioGroup *rSortOrder;
TEdit *eDateTime;
TEdit *eDate;
TLabel *Label5;
TEdit *eTime;
TLabel *Label7;
TLabel *Label6;
TLabel *Label8;
TLabel *Label9;
TButton *Button1;
TButton *bCloseEmail;
void __fastcall NextClick(TObject *Sender);
void __fastcall PreviousClick(TObject *Sender);
void __fastcall aShowMessageExecute(TObject *Sender);
void __fastcall FirstClick(TObject *Sender);
void __fastcall LastClick(TObject *Sender);
void __fastcall SaveMessageClick(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall rSortOrderClick(TObject *Sender);
void __fastcall FormatChange(TObject *Sender);
void __fastcall Button1Click(TObject *Sender);
void __fastcall bCloseEmailClick(TObject *Sender);
private: // User declarations
// Outlook pointers :
NameSpacePtr pNameSpace ;
FoldersPtr pFolders ;
MAPIFolderPtr pFolder ;
ItemsPtr pItems ;
MailItemPtr pMailItem;
RecipientPtr pRecipient ;
// Some Outlook vars :
AnsiString EmailUser ;
AnsiString FolderName ;
AnsiString FirstID ;
AnsiString LastID ;
//
long ItemNr ;
long NumberOfMessages ;
long NumberOfFolders ;
long NumberOfFolderItems ;
// Saving message vars :
AnsiString * Extention ;
int ExtentionIndex ;
//
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif unit3.cpp
//--------------------------------------------------------------------------- #include
#include
#include
#pragma hdrstop
#define NO_PROMPT_ON_ASSERTE_FAILURE
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Outlook_2K_SRVR"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
// --------------------------------------------------------------
// Much information i used to write this program
// can be found in the MSOffice visual basic help file :
// C:\Program Files\microsoft office\Office\1043\vbaoutl9.chm
//
// If you look at the line after #pragma hdrstop, you wil see the
// line with "#define NO_PROMPT_ON_ASSERTE_FAILURE" .
// This keeps the program from showing an ugly error-message
// when you try to get a not existing emailmessage with
// GetPervious or GetNext.
// I rather catch the errors myself with the catch blocks..
//
// But if anybody knows how to check for existing messages when
// using GetPrevious/GetNext, like they write in VBA-helpfile vbaoutl9.chm
// "the method wil returns 'Nothing' if no object exists ",
// please let me know. I couldn't figure out how to implement this.
// --------------------------------------------------------------
//
// Get icon from outlook.exe ,if program is found :
HICON Icon1 = ExtractIcon (MainForm->Handle ,"C:\\Program Files\\microsoft office\\Office\\Outlook.exe" , 0);
if(Icon1 != NULL ) MainForm->Icon->Handle = Icon1 ;
//
// Outlook pointers:
//
// It is a long road to the messages :
// Namespace->Folders->Folder->Items->MaiItem
//
OA1->Connect();
//
pNameSpace = OA1->GetNamespace (WideString("MAPI"));
pFolders = pNameSpace->Folders;
pFolder = pNameSpace->GetDefaultFolder(olFolderInbox);
pItems = pFolder->Items ;
//
// Sort the items on date recieved :
pItems->Sort(WideString("[ReceivedTime]"), true ) ;
//
// Some values :
pRecipient = pNameSpace->CurrentUser ;
EmailUser = pRecipient->Name;
FolderName = pFolder->Name ;
//
NumberOfMessages = pFolder->Items->Count ;
NumberOfFolders = pFolders->Count;
NumberOfFolderItems = pFolder->Items->Count ;
//
// Get the ID's from the first and last email message :
pMailItem = pFolder->Items->GetLast() ;
LastID = pMailItem->EntryID;
pMailItem = pFolder->Items->GetFirst() ;
FirstID = pMailItem->EntryID;
//
// make array with all possible export formats :
Extention = new AnsiString[8] ;
Extention[0]="txt";
Extention[1]="rtf";
Extention[2]="msg";
Extention[3]="msg";
Extention[4]="doc";
Extention[5]="html";
Extention[6]="txt";
Extention[7]="txt";
//
ExtentionIndex = 0 ; // needed if message is saved
FirstClick(this) ; // get first message to start with.
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::aShowMessageExecute(TObject *Sender)
{
//
// fill editboxes with values :
eID->Text = pMailItem->EntryID;
eTo->Text = pMailItem->To ;
eCC->Text = pMailItem->CC ;
eSubject->Text = pMailItem->Subject ;
eSender->Text = pMailItem->SenderName ;
eDateTime->Text = pMailItem->ReceivedTime ;
//
// long ReceivedTime is a number of type long.
// left of the comma is the Date value,
// right of the comma is the Time value.
//
TDateTime x ;
ShortDateFormat = "dd/mm/yyyy" ;
x = (TDate)pMailItem->ReceivedTime ;
eDate->Text = x.DateString();
//
LongTimeFormat = "hh:mm:ss" ;
x = (TTime)pMailItem->ReceivedTime ;
eTime->Text = x.TimeString();
//
// fill memo with message :
M1->Text = pMailItem->Body;
//
// Enable / Disable last or previous
// when first or last item is reached :
// This is NOT the right solution,
// in some cases still access-violations !
if ( FirstID.AnsiCompare( pMailItem->EntryID) == 0 )
{
//Previous->Enabled = false;
Application->MessageBox( "FirstMessage" , "Test", MB_OK);
}
//else Previous->Enabled = true;
if ( LastID.AnsiCompare( pMailItem->EntryID) == 0)
{
//Next->Enabled = false;
Application->MessageBox( "LastMessage" , "Test", MB_OK);
}
//else Next->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FirstClick(TObject *Sender)
{
// Show first message :
pMailItem = pFolder->Items->GetFirst() ;
aShowMessageExecute(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::NextClick(TObject *Sender)
{
// Next and Previous can give problems, because
// in certain situations a jump out of the array can occur.
try
{
pMailItem = pItems->GetNext();
aShowMessageExecute(Sender);
}
catch(EOleError &Deze)
{
// Application->MessageBox( "OleErr" , "NextTest", MB_OK);
}
catch(EAccessViolation &Deze)
{
// Application->MessageBox( "AccessV" , "NextTest", MB_OK);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::PreviousClick(TObject *Sender)
{
// Next and Previous can give problems, because
// in certain situations a jump out of the array can occur.
try
{
pMailItem = pItems->GetPrevious();
aShowMessageExecute(Sender);
}
catch(EOleError &Deze)
{
// Application->MessageBox( "OleErr" , "PreviousTest", MB_OK);
}
catch(EAccessViolation &Deze)
{
// Application->MessageBox( "AccessV" , "PreviousTest", MB_OK);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::LastClick(TObject *Sender)
{
// Show lastmessage :
pMailItem = pFolder->Items->GetLast();
aShowMessageExecute(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::SaveMessageClick(TObject *Sender)
{
// It seems that not all formats are supported for MailItems.
// Working formats are : TXT , MSG and HTML .
// The other formats give no results.
//
AnsiString FileNameStr = "C:\\temp\\Export." + Extention[ExtentionIndex];
pMailItem->SaveAs((WideString)FileNameStr , ExtentionIndex );
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
delete[] Extention;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::rSortOrderClick(TObject *Sender)
{ if( rSortOrder->ItemIndex == 0 ) pItems->Sort(WideString("[ReceivedTime]"), true ) ;
else pItems->Sort(WideString("[ReceivedTime]"), false ) ;
}
//--------------------------------------------------------------------------- void __fastcall TMainForm::FormatChange(TObject *Sender)
{
ExtentionIndex = Format->ItemIndex ;
}
//--------------------------------------------------------------------------- void __fastcall TMainForm::Button1Click(TObject *Sender)
{
pMailItem->Display("[Modal]");
bCloseEmail->Enabled = true ; }
//--------------------------------------------------------------------------- void __fastcall TMainForm::bCloseEmailClick(TObject *Sender)
{
pMailItem->Close(olDiscard);
bCloseEmail->Enabled = false ;
}
//---------------------------------------------------------------------------
HAVE A NICE DAY FOR YOU 發表人 - axsoft 於 2003/06/26 11:32:31