unit: MAPIMsgUtils.pas
file path: ..\Library\
version: 2014.хх
uses Classes, Windows, SHDocVw, ExtendedMAPI, IMIEMTypes,
MAPIBestBody
Available functions:
function GetMsgSenderName - Returns the message sender's display name
function GetMsgSenderAddress - Returns the message sender's E-mail address
function GetMsgSenderAddressType - Returns the message sender's e-mail type
function GetMsgToNames - Returns a list of the display names of the primary (To) message recipients, separated by semicolons (;).
function GetMsgCcNames - Returns an ASCII list of the display names of any carbon copy (CC) message recipients, separated by semicolons (;).
function GetMsgRecipients - Returns an array of the message recipients header record
function GetMsgSubject - Returns the full subject of a message
function GetMsgSentTime - Returns the date and time when the message sender submitted a message
function GetMsgSize - Returns the sum, in bytes, of the sizes of all properties on a message object
function GetMsgBodyPlain - Returns the message text
function GetMsgBodyRTF - Returns the Rich Text Format (RTF) version of the message text
function GetMsgBodyHTML - Returns the message body text in HTML format
function GetMsgInetCodePage - Returns the code page used for message body
function GetMsgLocaleID - Returns the Windows LCID of the end user who created this message
function GetMsgCodePage - Returns the code page that is used for the message
function GetMsgImportance - Returns a value that indicates the message sender's opinion of the importance of a message
function GetMsgReadReceiptRequest - Returns TRUE if a message sender wants the messaging system to generate a read report when the recipient has read a message
function GetMsgDeliveryReportRequest - Returns TRUE if a message sender requests a delivery report for a particular recipient from the messaging system before the message is placed in the message store.
function GetMsgHasAttachment - Returns TRUE if a message contains at least one attachment
function GetMsgAttTable - Returns the message's attachment table.
function GetMsgAttCount - Returns the total number of attachments.
function GetMsgAttList - Returns an array of the message attachments header record
function GetMsgAttachment - Opens an attachment
function GetMsgAttDataAsStream - Returns a stream with attachment content
function GetAttDataAsStream - Returns a stream with attachment content
function SetMsgRecipients - Set the message recipients
function AddMsgAttachment - Create a new attachment
function SetMsgImportance - Set the message Importance
function SetMsgSubject - Set message Subject
function SetMsgReadReceiptRequest - Set message Read Receipt Request
function SetMsgDeliveryReportRequest - Set message Delivery Report Request
function SetMsgLocaleID - Set Windows LCID of the end user who created this message
function SetMsgInetCodePage - Set the code page (PR_INTERNET_CPID) used for message body. Indicates the code page used for PR_BODY or PR_BODY_HTML properties.
function SetMsgCodePage - Specifies the code page used to encode the non-Unicode string properties on this Message
procedure ClearMsgBodyPart - Delete all body parts as Plain, RTF, HTML
function SetMsgBodyPlain - Set the message plain text
function SetMsgBodyRtf - Set the message RTF text
function SetMsgBodyHtml - Set the message HTML text
function SaveMsg - Makes permanent any changes that were made to a message
procedure SubmitMsg - Saves all of the message's properties and marks the message as ready to be sent
procedure DeleteMsgAttachment - Delete an Attachment
procedure DeleteMsg - Delete a Message
procedure GetMsgAfterSendAction - Processing a Sent Message
procedure SetMsgAfterSendAction - Processing a Sent Message
procedure ShowHTMLBodyRendered - Shows HTML body by TWebBrowser
function ShowCustomSize - For internal use
procedure ShowModalMessage - Displays a message form
function GetMsgSenderName(const MAPIMessage: IMessage): String;
description
Returns the message sender's display name (PR_SENDER_NAME)
parameters
MAPIMessage - IMessage.
usage
// Get From Name
labelFrom.Caption := GetMsgSenderName(FMAPIMessage);
function GetMsgSenderAddress(const MAPIMessage: IMessage): String;
description
Returns the message sender's E-mail address (PR_SENDER_EMAIL_ADDRESS)
parameters
MAPIMessage - IMessage.
usage
// Get From Name
labelFrom.Caption := GetMsgSenderName(FMAPIMessage);
// Get From Address
labelFrom.Caption := labelFrom.Caption + ' [' +
GetMsgSenderAddress(FMAPIMessage) + ']';
function GetMsgSenderAddressType(const MAPIMessage: IMessage): String;
description
Returns the message sender's E-mail type (PR_SENDER_ADDRTYPE)
function GetMsgToNames(const MAPIMessage: IMessage): String;
description
Returns a list of the display names of the primary (To) message recipients,
separated by semicolons (;). (PR_DISPLAY_TO)
parameters
MAPIMessage -
IMessage.
usage
// Get From Name
labelFrom.Caption := GetMsgSenderName(FMAPIMessage);
// Get From Address
labelFrom.Caption := labelFrom.Caption + ' [' +
GetMsgSenderAddress(FMAPIMessage) + ']';
// To
lbTo.Caption := GetMsgToNames(FMAPIMessage);
function GetMsgCcNames(const MAPIMessage: IMessage): String;
description
Returns an ASCII list of the display names of any carbon copy (CC) message
recipients, separated by semicolons (;). (PR_DISPLAY_CC)
parameters
MAPIMessage - IMessage.
usage
// Get From Name
labelFrom.Caption := GetMsgSenderName(FMAPIMessage);
// Get From Address
labelFrom.Caption := labelFrom.Caption + ' [' +
GetMsgSenderAddress(FMAPIMessage) + ']';
// To
lbTo.Caption := GetMsgToNames(FMAPIMessage);
// CC
lbCc.Caption := GetMsgCcNames(FMAPIMessage);
function GetMsgRecipients(const MAPIMessage: IMessage; var Recipients: TRecipientsHeadList): Boolean;
description
On Success returns an array of the message recipients header record
parameters
MAPIMessage - IMessage.
Recipients - TRecipientsHeadList
Array of Recipient header records
usage
// Get saved (existing) recipients
GetMsgRecipients(FMAPIMessage, RecipientHeadList);
FillEmailFields;
procedure FillEmailFields;
var
iCount: Integer;
begin
ebTo.Text := ''; // eb => TEdit
ebCC.Text := '';
ebBcc.Text := '';
for iCount := 0 to Length(RecipientHeadList)
- 1 do
begin
case
RecipientHeadList[iCount].RecipientType of
rtTo:
ebTo.Text := ebTo.Text + RecipientHeadList[iCount].DisplayName + '; ';
rtCc:
ebCC.Text := ebCC.Text + RecipientHeadList[iCount].DisplayName + '; ';
rtBCc:
ebBcc.Text := ebBcc.Text + RecipientHeadList[iCount].DisplayName + '; ';
end;
end;
end;
function GetMsgSubject(const MAPIMessage: IMessage): String;
description
Returns the full subject of a message (PR_SUBJECT)
parameters
MAPIMessage - IMessage.
usage
// Get Subject
labelSubject.Caption := GetMsgSubject(FMAPIMessage);
function GetMsgSentTime(const MAPIMessage: IMessage): TDateTime;
description
Returns the date and time when the message sender submitted a message (PR_CLIENT_SUBMIT_TIME)
parameters
MAPIMessage - IMessage.
usage
// Get Sent time
labelSent.Caption := 'Sent: ' + DateTimetoStr(GetMsgSentTime(FMAPIMessage));
function GetMsgSize(const MAPIMessage: IMessage): ULONG;
description
Returns the sum, in bytes, of the sizes of all properties on a message object
(PR_MESSAGE_SIZE)
parameters
MAPIMessage - IMessage.
usage
// Get Message Size
labelSize.Caption := 'Message size: ' + IntToStr(GetMsgSize(FMAPIMessage)) + '
bytes';
function GetMsgHasAttachment(const MAPIMessage: IMessage): Boolean;
description
Returns TRUE if a message contains at least one attachment (PR_HASATTACH)
parameters
MAPIMessage - IMessage.
usage
// Has Attachment
labelHasAttachment.Caption := 'Has Attachment: ' + BoolTostr(GetMsgHasAttachment(FMAPIMessage),
True);
function GetMsgBodyPlain(const MAPIMessage: IMessage; out Stream: TStream; out Unicode: Boolean; out CodePage: Cardinal): Boolean;
description
Returns the message text (PR_BODY)
parameters
MAPIMessage - IMessage.
usage
// get plain body
if GetMsgBodyPlain(FMAPIMessage,
Stream, IsUnicode, CodePage) then
begin
if IsUnicode then
Memo.Lines.LoadFromStream(Stream
{$IFDEF DELPHI2009}, TEncoding.Unicode
{$ENDIF})
else
Memo.Lines.LoadFromStream(Stream
{$IFDEF DELPHI2009}, TEncoding.Ascii
{$ENDIF})
end;
function GetMsgBodyRTF(const MAPIMessage: IMessage; out Stream: TStream): Boolean;
description
Returns the Rich Text Format (RTF) version of the message text (PR_RTF_COMPRESSED).
RTF is always ASCII.
parameters
MAPIMessage - IMessage.
usage
// get RTF body
if GetMsgBodyRTF(FMAPIMessage, Stream) then
RichEdit.Lines.LoadFromStream(Stream {$IFDEF DELPHI2009}, TEncoding.Ascii
{$ENDIF});
function GetMsgBodyHTML(const MAPIMessage: IMessage; out Stream: TStream): Boolean;
description
Returns the message body text in HTML format (PR_HTML)
parameters
MAPIMessage - IMessage.
usage
// get HTML body
procedure GetHTMLBodyRendered;
var
Stream: TStream;
StrStream: TStream;
CodePage: ULONG;
begin
Stream := nil;
try
WebBrowser.Visible := True;
if
GetMsgBodyHTML(FMAPIMessage, Stream) then
begin
CodePage :=
GetMsgInetCodePage(FMAPIMessage);
{$IFDEF DELPHI2009}
StrStream :=
TStringStream.Create('', TEncoding.GetEncoding(CodePage));
{$ELSE}
StrStream :=
TIMIBStringStream.Create('', CodePage);
{$ENDIF}
try
TMemoryStream(Stream).SaveToStream(StrStream);
StrStream.Position := 0;
InitializeCIDMIMEHandler(FMAPIMessage);
{$IFDEF DELPHI2009}
LoadDocFromString(WebBrowser, TStringStream(StrStream).DataString, CodePage);
{$ELSE}
LoadDocFromString(WebBrowser, TIMIBStringStream(StrStream).DataStringW, CodePage);
{$ENDIF}
FreeCIDMIMEHandler;
finally
FreeAndNil(StrStream);
end;
end;
finally
if Assigned(Stream) then
FreeAndNil(Stream);
end;
end;
function GetMsgInetCodePage(const MAPIMessage: IMessage): ULONG;
description
Returns the code page used for message body (PR_INTERNET_CPID).
Indicates the code page used for
PR_BODY or
PR_BODY_HTML properties.
parameters
MAPIMessage - IMessage.
usage
// Get Internet Code Page
var
CodePage: ULONG;
lpCPInfoEx: TCPInfoEx;
begin
CodePage := GetMsgInetCodePage(FMAPIMessage);
if GetCPInfoEx(CodePage, 0, lpCPInfoEx) then
labelCodePage.Caption := lpCPInfoEx.CodePageName
else
labelCodePage.Caption := IntToStr(CodePage);
function GetMsgLocaleID(const MAPIMessage: IMessage): ULONG;
description
Returns the Windows LCID of the end user who created this message (PR_MESSAGE_LOCALE_ID)
parameters
MAPIMessage - IMessage.
function GetMsgCodePage(const MAPIMessage: IMessage): ULONG;
description
Returns the code page that is used for the message (PR_MESSAGE_CODEPAGE).
Specifies the code page used to encode the non-Unicode string properties on this Message object.
parameters
MAPIMessage - IMessage.
function GetMsgImportance(const MAPIMessage: IMessage): TMsgImportance;
description
Returns a value that indicates the message sender's opinion of the importance
of a message (PR_IMPORTANCE)
parameters
MAPIMessage - IMessage.
function GetMsgReadReceiptRequest(const MAPIMessage: IMessage): Boolean;
description
Returns TRUE if a message sender wants the messaging system to generate a
read report when the recipient has read a message (PR_READ_RECEIPT_REQUESTED)
parameters
MAPIMessage - IMessage.
function GetMsgDeliveryReportRequest(const MAPIMessage: IMessage): Boolean;
description
Returns TRUE if a message sender requests a delivery report for a particular
recipient from the messaging system before the message is placed in the message
store. (PR_ORIGINATOR_DELIVERY_REPORT_REQUESTED)
parameters
MAPIMessage - IMessage.
function GetMsgAttTable(const MAPIMessage: IMessage): IMAPITable;
description
Returns the message's attachment table.
parameters
MAPIMessage - IMessage.
function GetMsgAttCount(const MAPIMessage: IMessage): ULONG;
description
Returns the total number of attachments.
parameters
MAPIMessage - IMessage.
function GetMsgAttList(const MAPIMessage: IMessage): TAttHeadList;
description
Returns an array of the message attachments
header record
parameters
MAPIMessage - IMessage.
function GetMsgAttachment(const MAPIMessage: IMessage; const ID: Cardinal; const Modify: Boolean = False): IAttach;
description
Opens and returns an attachment - IAttach
parameters
MAPIMessage - IMessage.
ID - Attachment unique ID
Modify - requests read/write access
function GetMsgAttDataAsStream(const MAPIMessage: IMessage; const ID: Cardinal; Stream: TStream): Boolean;
description
Opens an attachment and returns a stream with attachment content
parameters
MAPIMessage -
IMessage.
ID - Attachment unique ID
Stream - attachment content as Stream
usage
procedure TAttachmentDlg.btSaveAsClick(Sender: TObject);
var
FileName: string;
ID: Cardinal;
index: Integer;
Stream: TMemoryStream;
begin
if not Assigned(AttachmentListView.Selected)
then
Exit;
Index := AttachmentListView.Selected.Index;
ID := FAttHeadList[Index].ID;
FileName := FAttHeadList[Index].FileName;
Stream := TMemoryStream.Create;
try
if not GetMsgAttDataAsStream(FMAPIMessage,
ID, Stream) then Exit;
if not
PromptForFileName(FileName, '', '', '', '', True) then Exit;
Stream.SaveToFile(FileName);
ShowMessage('The Attachment is saved
to "' + FileName + '"');
finally
if Assigned(Stream)
then
FreeAndNil(Stream);
end;
end;
function GetAttDataAsStream(const Attachment: IAttach; Stream: TStream): Boolean;
description
Returns a stream with attachment content
parameters
Attachment - IAttach.
Stream - attachment content as Stream
function SetMsgRecipients(const MAPIMessage: IMessage;
const AddressBook: IAddrBook;
var Recipients:
TRecipientsHeadList; UIParam:
ULONG_PTR = 0): Boolean;
description
Set the message recipients
parameters
MAPIMessage - IMessage;
AddressBook - IAddrBook
Recipients - TRecipientsHeadList
- array of Recipients Head Records
UIParam - Specifies the parent window handle
function AddMsgAttachment(const MAPIMessage: IMessage; Stream: TStream;
const DisplayName: string; out AttachmentNum: Cardinal):
IAttach; overload;
function AddMsgAttachment(const MAPIMessage:
IMessage; const FileName: string;
out AttachmentNum: Cardinal): IAttach; overload;
description
Create a new attachment
parameters
MAPIMessage -IMessage
Stream - TStream. Stream which contains attachment content
DisplayName - string. The display name of the attachment
FileName - string. The path and file name of the file containing the data for
the attachment
AttachmentNum - Cardinal. Attachment Unique ID
function SetMsgImportance(const MAPIMessage: IMessage; Importance: TMsgImportance): Boolean;
description
Set message importance (PR_IMPORTANCE)
parameters
MAPIMessage -IMessage
Importance - TMsgImportance
function SetMsgSubject(const MAPIMessage: IMessage; const Subject: string): Boolean;
description
Set message Subject (PR_SUBJECT). The subject properties are typically small
strings of fewer than 256 characters
parameters
MAPIMessage -IMessage
Subject - string
usage
// Set Subject
if Length(Trim(ebSubject.Text)) > 0 then
SetMsgSubject(FMAPIMessage, Trim(ebSubject.Text));
// set Importance
SetMsgImportance(FMAPIMessage, TMsgImportance(rgMsgImportance.ItemIndex));
// set Read Receipt Request
SetMsgReadReceiptRequest(FMAPIMessage, cbReadRequest.Checked);
// Delivery ReportRequest
SetMsgDeliveryReportRequest(FMAPIMessage, cbDelivery.Checked);
function SetMsgReadReceiptRequest(const MAPIMessage: IMessage; const Request: Boolean): Boolean;
description
Set message Read Receipt Request (PR_READ_RECEIPT_REQUESTED). Set Request to
TRUE if a message sender wants the messaging system to generate a read report
when the recipient has read a message.
parameters
MAPIMessage -IMessage
Request - Boolean
usage
// Set Subject
if Length(Trim(ebSubject.Text)) > 0 then
SetMsgSubject(FMAPIMessage, Trim(ebSubject.Text));
// set Importance
SetMsgImportance(FMAPIMessage, TMsgImportance(rgMsgImportance.ItemIndex));
// set Read Receipt Request
SetMsgReadReceiptRequest(FMAPIMessage, cbReadRequest.Checked);
// Delivery ReportRequest
SetMsgDeliveryReportRequest(FMAPIMessage, cbDelivery.Checked);
function SetMsgDeliveryReportRequest(const MAPIMessage: IMessage; const Request: Boolean): Boolean;
description
Set message Delivery Report Request (PR_ORIGINATOR_DELIVERY_REPORT_REQUESTED).
Set Request to TRUE if a message sender requests a delivery report for a
particular recipient from the messaging system before the message is placed in
the message store.
parameters
MAPIMessage -
IMessage
Request - Boolean
usage
// Set Subject
if Length(Trim(ebSubject.Text)) > 0 then
SetMsgSubject(FMAPIMessage, Trim(ebSubject.Text));
// set Importance
SetMsgImportance(FMAPIMessage, TMsgImportance(rgMsgImportance.ItemIndex));
// set Read Receipt Request
SetMsgReadReceiptRequest(FMAPIMessage, cbReadRequest.Checked);
// Delivery ReportRequest
SetMsgDeliveryReportRequest(FMAPIMessage, cbDelivery.Checked);
function SetMsgLocaleID(const MAPIMessage: IMessage; const LocaleID: Cardinal): Boolean;
description
Set Windows LCID of the end user who created this message (PR_MESSAGE_LOCALE_ID)
parameters
MAPIMessage - IMessage
LocaleID - Cardinal
function SetMsgInetCodePage(const MAPIMessage: IMessage; const InetCodePage:ULONG): Boolean;
description
Set the code page (PR_INTERNET_CPID)
used for message body.
Indicates the code page used for
PR_BODY or
PR_BODY_HTML properties.
parameters
MAPIMessage - IMessage
InetCodePage - Cardinal
function SetMsgCodePage(const MAPIMessage: IMessage; const LocaleID: Cardinal): Boolean;
description
Specifies the code page used to encode the non-Unicode string properties on this
Message object. ( (PR_MESSAGE_CODEPAGE))
parameters
MAPIMessage - IMessage
LocaleID - Cardinal
procedure ClearMsgBodyPart(const MAPIMessage: IMessage);
description
Deletes all body parts as Plain, RTF, HTML. Do not call it when Message Store
reside inside Exchange Server.
parameters
MAPIMessage - IMessage
function SetMsgBodyPlain(const MAPIMessage: IMessage; Stream: TStream): Boolean;
description
Set the message plain text body (PR_BODY). The value for Stream must be
expressed in the code page of the operating system that MAPI is running on.
parameters
MAPIMessage - IMessage
Stream - TStream. Text body.
usage
Stream := TMemoryStream.Create;
try
Memo.Lines.SaveToStream(Stream {$IFDEF
DELPHI2009}, TEncoding.Unicode {$ENDIF});
if Stream.Size > 0 then
SetMsgBodyPlain(FMAPIMessage,
Stream);
finally
FreeAndNil(Stream);
end;
function SetMsgBodyRtf(const MAPIMessage: IMessage; Stream: TStream): Boolean;
description
Set the message plain RTF body (PR_RTF_COMPRESSED). RTF is always ASCII.
parameters
MAPIMessage - IMessage
Stream - TStream. RTF uncompressed stream.
usage
Stream := TMemoryStream.Create;
try
// RTF is always ASCII
RichEdit.Lines.SaveToStream(Stream
{$IFDEF DELPHI2009}, TEncoding.Ascii {$ENDIF});
if Stream.Size > 0 then
SetMsgBodyRTF(FMAPIMessage, Stream);
finally
FreeAndNil(Stream);
end;
function SetMsgBodyHtml(const MAPIMessage: IMessage; Stream: TStream): Boolean;
description
Set the message HTML text (PR_HTML).
parameters
MAPIMessage - IMessage
Stream - TStream. HTML stream.
usage
Stream := TMemoryStream.Create;
try
SaveDocToStream(WebBrowser, Stream); //
MAPIBestBody.pas; WebBrowser - TWebBrowser
NormalizeHTMLStream(Stream); // if
Stream is <> from ASCII, convert it to UTF-8
if Stream.Size > 0 then
SetMsgBodyHtml(FMAPIMessage,
Stream);
finally
FreeAndNil(Stream);
end;
function SaveMsg(const MAPIMessage: IMessage): Boolean;
description
Makes permanent any changes that were made to a message.
parameters
MAPIMessage - IMessage
procedure SubmitMsg(const MAPIMessage: IMessage);
description
Saves all of the message's properties and marks the message as ready to be sent
parameters
MAPIMessage - IMessage
procedure DeleteMsgAttachment(const MAPIMessage: IMessage; const AttachmentID: Cardinal; const Permanent: Boolean = False);
description
Deletes an Attachment
parameters
MAPIMessage - IMessage
AttachmentID - Unique attachment ID
Permanent - A deleted attachment is not permanently deleted if Permanent
is False. In case of Permanent = False
you need to call function
SaveMsg after that.
procedure DeleteMsg(const MAPIMessage: IMessage; const OwnerFolder: IMAPIFolder);
description
Deletes message from the current folder
parameters
MAPIMessage -IMessage
OwnerFolder -
IMAPIFolder
procedure GetMsgAfterSendAction(const MAPIMessage: IMessage; out MoveToSentItems: Boolean; out Delete: Boolean);
description
Get information how to be processed a Sent Message
parameters
MAPIMessage -
IMessage
MoveToSentItems - Boolean. If TRUE, moves the message to the Sent Items folder.
Delete - Boolean. If TRUE, deletes the message
The following table describes how these values affect what you do with sent messages.
If neither property is set: | Leave the message in the folder from which it was sent (typically the Outbox). |
MoveToSentItems is TRUE | Move the message to the Sent Items folder |
Delete is TRUE | Delete the message |
procedure SetMsgAfterSendAction(const DefaultMsgStore: IMsgStore; const MAPIMessage: IMessage; const MoveToSentItems: Boolean = True);
description
Set information how to be processed a Sent Message
parameters
DefaultMsgStore - Default for current MAPI session
IMsgStore
MAPIMessage - IMessage
MoveToSentItems - Boolean. If TRUE, moves the message to the Sent Items folder.
Delete - Boolean. If TRUE, deletes the message
The following table describes how these values affect what you do with sent messages.
If neither property is set: | Leave the message in the folder from which it was sent (typically the Outbox). |
MoveToSentItems is TRUE | Move the message to the Sent Items folder |
Delete is TRUE | Delete the message |
procedure ShowHTMLBodyRendered(const MAPIMessage: IMessage; const WebBrowser: TWebBrowser);
description
Show HTML Body in TWebBrowser
parameters
MAPIMessage - IMessage
WebBrowser - TWebBrowser
usage
ShowHTMLBodyRendered(FMAPIMessage, WebBrowser);
function ShowCustomSize(Value: Cardinal): string;
description
For internal use
procedure ShowModalMessage(const MAPISession: IMAPISession; const MsgStore: IMsgStore; const MAPIFolder: IMAPIFolder; const MAPIMessage: IMessage; const UIParam: ULONG_PTR = 0; const MsgFlag: ULONG = MAPI_POST_MESSAGE);
description
Displays a message form
parameters
MAPISession: IMAPISession
MsgStore: IMsgStore
MAPIFolder: IMAPIFolder
MAPIMessage: IMessage
UIParam: ULONG_PTR = 0
MsgFlag: ULONG = MAPI_POST_MESSAGE
Copyright © 2021 IMIBO
Privacy Statement |