function sendMail(
const TargetName, TargetAddr,
SenderName, SenderAddr,
MsgSubject, MsgContent,
Attachment :
String;
PreviewMsg : Boolean = TRUE ) : Integer;
var
msg : TMapiMessage;
// Pointer to the message itself
mrdSender,
// Who's sending it?
mrdTarget : TMapiRecipDesc;
// Who's going to get it?
mfdAttach : TMapiFileDesc;
// The attached file.
liFlags : Longint;
// Flags for MAPI.
strError :
String;
// Holds MAPI error results;
begin
result := 0;
liFlags := 0;
fillChar( msg, sizeOf( msg ), 0 );
with msg
do
begin
// if TargetAddr = '' then
// raise tMailFileException.createFmt( ERROR,
// [ 'Target email address not specified' ] )
begin
if TargetName = '
'
then
mrdTarget.lpszName := pChar( TargetAddr )
else
mrdTarget.lpszName := pChar( TargetName );
mrdTarget.ulRecipClass := MAPI_TO;
mrdTarget.lpszAddress := pChar( TargetAddr );
mrdTarget.ulReserved := 0;
mrdTarget.ulEIDSize := 0;
mrdTarget.lpEntryID :=
NIL;
nRecipCount := 1;
lpRecips := @mrdTarget;
end;
// if SenderAddr = '' then
// raise tMailFileException.createFmt( ERROR,
// [ 'Sender email address not specified' ] )
begin
if SenderName = '
'
then
mrdSender.lpszName := pChar( SenderAddr )
else
mrdSender.lpszName := pChar( SenderName );
mrdSender.ulRecipClass := MAPI_ORIG;
mrdSender.lpszAddress := pChar( '
SMTP:' + SenderAddr );
mrdSender.ulReserved := 0;
mrdSender.ulEIDSize := 0;
mrdSender.lpEntryID :=
NIL;
lpOriginator := @mrdSender;
end;
if MsgSubject = '
'
then
lpszSubject := '
'
else
lpszSubject := pChar( MsgSubject );
// if ( MsgContent = '' ) AND ( Attachment = '' ) then
// raise tMailFileException.createFmt( ERROR,
// [ 'Tried to send an empty message (no content or attachment)' ] )
begin
if MsgContent = '
'
then
lpszNoteText := '
Please see the attached file.'
else
lpszNoteText := pChar( MsgContent );
if Attachment = '
'
then begin
nFileCount := 0;
lpFiles :=
NIL;
end else begin
fillChar( mfdAttach, sizeOf( mfdAttach ), 0 );
mfdAttach.nPosition := cardinal( $FFFFFFFF );
mfdAttach.lpszPathName := pChar( Attachment );
nFileCount := 1;
lpFiles := @mfdAttach;
end;
end;
// with
if previewMsg
then liFlags := MAPI_DIALOG;
result := mapiSendMail( 0, application.Handle, msg, liFlags, 0 );
{ if result <> 0 then
raise tMailFileException.createFmt( ERROR,
[ 'MAPI triggered an error ('
+ getMAPIError( result ) + ')' ] ); }
end;
end;