Request # 22
Command line .exe that sends e-mails
Usage (example):
CmdMail.exe -T="imibo@imibo.com, info@imibo.com, lolo@imibo.com" -P=Outlook -U="SMTP:az00008@mapisoft.com" -S="Test Message" -B="C:\Temp\Questions.htm" -A="C:\TEMP\SmtpClient.zip, C:\TEMP\TestFile.zip" T -> Recipients TO List – each recipient should be delimited by comma. Value is quoted (“) C -> Recipients CC List – each recipient should be delimited by comma. Value is quoted (“) A -> Attachments List (path to existing file) – each attachment should be delimited by comma. Value is quoted (“) P –> MAPI/Outlook profile name – Name of MAPI profile for connection to Exchange Server. Should be configured as on-line connection (do not use “cache/offline”!) with Exchange Server, administrative rights. U -> User SMTP e-mail address – from this e-mail address (mailbox), will be sent e-mail.
program CmdMail;
{ Please add "..\Library" to project search path }
(* Please read Readme.txt from the directory !!! *)
{$APPTYPE CONSOLE}
{$R *.res}
{$I IMI.INC}
uses
Classes, SysUtils, Windows, ActiveX, EDK, MAPIException, MAPIUtils, MAPIPropUtils,
ExtendedMAPI;
const
cRecepientsTo = 'T';
cRecepientsBcc = 'C';
cSubject = 'S';
cBodyPath = 'B';
cAttachementsPath = 'A';
//
cProfile = 'P';
cUser = 'U';
Const
PropForCorrect:
Record cValues: ULONG;
aulPropTag:
Array [0 .. 2] Of ULONG;
End
= (cValues: 3; aulPropTag: (PR_EMAIL_ADDRESS_A, PR_ADDRTYPE_A,
PR_DISPLAY_NAME_A));
var
hr: HRESULT;
IsInitOK: Boolean = False;
MAPIProfile: AnsiString = '';
MAPISession: IMAPISession = nil;
AddressBook: IAddrBook = nil;
GAL: IABContainer = nil;
ExchPrivateStore: IMsgStore = nil;
ExServer: AnsiString = '';
UserPrivateStore: IMsgStore = nil;
UserEMailAddress: AnsiString = '';
ServerDN: AnsiString = '';
MailboxDN: AnsiString = '';
Outbox: IMAPIFolder = nil;
// ------------------------------ //
function InitMAPI: Boolean;
var
MAPIINIT: TMAPIINIT;
begin
ZeroMemory(@MAPIINIT, SizeOf(TMAPIINIT));
MAPIINIT.ulVersion := MAPI_INIT_VERSION;
MAPIINIT.ulFlags := MAPI_NT_SERVICE;
hr := MAPIInitialize(@MAPIINIT);
if failed(hr) then
Raise Exception.Create(WrapText(GetMAPIInitializeError(hr), 80));
IsInitOK := True;
Result := IsInitOK;
end;
procedure MAPIClose;
var
Flag: ULONG;
begin
Flag := LOGOFF_NO_WAIT;
try
Outbox := nil;
if Assigned(UserPrivateStore) then
hr := UserPrivateStore.StoreLogOff(Flag);
UserPrivateStore := nil;
if Assigned(ExchPrivateStore) then
hr := ExchPrivateStore.StoreLogOff(Flag);
ExchPrivateStore := nil;
GAL := nil;
AddressBook := nil;
if Assigned(MAPISession) then
hr := MAPISession.Logoff(0, 0, 0);
MAPISession := nil;
MAPIUninitialize;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end;
function LogonToMapi: Boolean;
var
ProfileName: PAnsiChar;
MAPILogonFlags: ULONG;
begin
if MAPIProfile <> '' then
begin
ProfileName := PAnsiChar(@MAPIProfile[1]);
MAPILogonFlags := MAPI_NO_MAIL or MAPI_NEW_SESSION or MAPI_EXTENDED or
MAPI_LOGON_UI;
end
else
begin
ProfileName := nil;
MAPILogonFlags := MAPI_NO_MAIL or MAPI_NEW_SESSION or MAPI_EXTENDED or
MAPI_PASSWORD_UI;
end;
hr := MAPILogonEx(0, Pointer(ProfileName), nil, MAPILogonFlags, MAPISession);
if failed(hr) then
raise Exception.Create(GetMAPILogonExError(hr));
Result := True;
end;
function LoadAddressBook: Boolean;
var
GALEntry: TSBinary;
GALObjectType: ULONG;
begin
Result := False;
// Get interface to Address Book
hr := MAPISession.OpenAddressBook(0, nil, 0, AddressBook);
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPISession, hr);
ZeroMemory(@GALEntry, SizeOf(TSBinary));
try
hr := HrFindExchangeGlobalAddressList(AddressBook, GALEntry.cb,
PENTRYID(GALEntry.lpb));
if failed(hr) then
raise EMAPIError.CreateMAPI(AddressBook, hr);
hr := AddressBook.OpenEntry(GALEntry.cb, PENTRYID(GALEntry.lpb),
@IID_IMAPIContainer, MAPI_BEST_ACCESS, GALObjectType, IUnknown(GAL));
if failed(hr) then
raise EMAPIError.CreateMAPI(AddressBook, hr)
else
if (GALObjectType <> MAPI_ABCONT) or (GAL = nil) then
raise EMAPIError.Create('Cannot open the Exchange Global Address List');
Result := True;
finally
if Assigned(GALEntry.lpb) then
MAPIFreeBuffer(GALEntry.lpb);
end;
end;
Function LoadPrivateMDB: Boolean;
var
RootFolder: IMAPIFolder;
ObjType: ULONG;
ServerNamePV: PSPropValue;
begin
Result := False;
ServerNamePV := nil;
hr := HrOpenExchangePrivateStore(MAPISession, ExchPrivateStore, True);
// False
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPISession, hr);
hr := ExchPrivateStore.OpenEntry(0, nil, nil, MAPI_BEST_ACCESS, ObjType,
IUnknown(RootFolder));
if failed(hr) then
raise EMAPIError.CreateMAPI(ExchPrivateStore, hr);
try
hr := HrGetOneProp(RootFolder, PR_REPLICA_SERVER_A, ServerNamePV);
if failed(hr) then
raise EMAPIError.CreateMAPI(RootFolder, hr);
ExServer := AnsiString(ServerNamePV.Value.lpszA);
Result := True;
finally
if Assigned(ServerNamePV) then
MAPIFreeBuffer(ServerNamePV);
ServerNamePV := nil;
RootFolder := nil;
end;
end;
function ResolveEmlAddr: Boolean;
var
IsMAPIRec: Bool;
Entry: TSBinary;
ObjType: ULONG;
Unkn: IMAilUser;
HOME_MDB, SiteDN: AnsiString;
begin
Result := False;
ZeroMemory(@Entry, SizeOf(TSBinary));
try
hr := HrGWResolveAddress(IABContainer(GAL), PAnsiChar(UserEMailAddress),
IsMAPIRec, Entry.cb, PENTRYID(Entry.lpb));
if failed(hr) then
raise EMAPIError.CreateMAPI(GAL, hr);
hr := GAL.OpenEntry(Entry.cb, PENTRYID(Entry.lpb), nil, MAPI_BEST_ACCESS or
MAPI_NO_CACHE, ObjType, IUnknown(Unkn));
if failed(hr) then
hr := GAL.OpenEntry(Entry.cb, PENTRYID(Entry.lpb), nil, MAPI_BEST_ACCESS,
ObjType, IUnknown(Unkn));
if failed(hr) then
raise EMAPIError.CreateMAPI(GAL, hr);
MailboxDN := AnsiString(MAPIPropUtils.GetPropString(Unkn, PR_EMAIL_ADDRESS_A));
HOME_MDB := AnsiString(MAPIPropUtils.GetPropString(Unkn, PR_EMS_AB_HOME_MDB_A));
SiteDN := AnsiString(IGetSiteDN(PChar(String(MailboxDN))));
ServerDN := AnsiString
((Format('%s/cn=Configuration/cn=Servers/cn=%s/cn=Microsoft Private MDB',
[SiteDN, ExServer])));
Result := True;
finally
if Assigned(Entry.lpb) then
MAPIFreeBuffer(Entry.lpb);
end;
end;
function OpenOtherUsersMailbox: Boolean;
var
XManageStore: IExchangeManageStore;
EntryID: TSBinary;
begin
Result := False;
hr := ExchPrivateStore.QueryInterface(IID_IExchangeManageStore, XManageStore);
if failed(hr) then
raise EMAPIError.CreateMAPI(ExchPrivateStore, hr);
ZeroMemory(@EntryID, SizeOf(TSBinary));
try
hr := XManageStore.CreateStoreEntryID(PAnsiChar(ServerDN),
PAnsiChar(MailboxDN), OPENSTORE_USE_ADMIN_PRIVILEGE or
OPENSTORE_TAKE_OWNERSHIP, EntryID.cb, PENTRYID(EntryID.lpb));
if failed(hr) then
raise EMAPIError.CreateMAPI(nil, hr);
hr := MAPISession.OpenMsgStore(0, EntryID.cb, PENTRYID(EntryID.lpb),
@IID_IMsgStore, MAPI_BEST_ACCESS or MDB_ONLINE or MDB_TEMPORARY,
IMsgStore(UserPrivateStore));
if hr = MAPI_E_UNKNOWN_FLAGS then
hr := MAPISession.OpenMsgStore(0, EntryID.cb, PENTRYID(EntryID.lpb),
@IID_IMsgStore, MAPI_BEST_ACCESS or MDB_TEMPORARY,
IMsgStore(UserPrivateStore));
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPISession, hr);
Result := True;
finally
if Assigned(EntryID.lpb) then
MAPIFreeBuffer(EntryID.lpb);
XManageStore := nil;
end;
end;
function GetOutbox: Boolean;
var
EntryID: TSBinary;
ObjType: ULONG;
begin
Result := False;
// Get Entry ID of Outbox folder
ZeroMemory(@EntryID, SizeOf(TSBinary));
hr := HrMAPIFindOutbox(UserPrivateStore, EntryID.cb, PENTRYID(EntryID.lpb));
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPISession, hr);
try
// Get Interface to Outbox folder
hr := UserPrivateStore.OpenEntry(EntryID.cb, PENTRYID(EntryID.lpb),
@IID_IMAPIFolder, MAPI_MODIFY or MAPI_NO_CACHE, ObjType,
IUnknown(Outbox));
if (MAPI_E_UNKNOWN_FLAGS = hr) then // Outlook Version < 2003
hr := UserPrivateStore.OpenEntry(EntryID.cb, PENTRYID(EntryID.lpb),
@IID_IMAPIFolder, MAPI_MODIFY, ObjType, IUnknown(Outbox));
if failed(hr) then
raise EMAPIError.CreateMAPI(UserPrivateStore, hr);
Result := True;
finally
if Assigned(EntryID.lpb) then
MAPIFreeBuffer(EntryID.lpb);
end;
end;
Procedure CreateAttachement(Const MAPIMessage: IMessage; Stream: TMemoryStream;
AttName: AnsiString);
Var
ComStream: IStream;
CountWritten: Integer;
TagProp: Cardinal;
AttachmentNumber: ULONG;
Attachment: IAttach;
DisplayName, ShortFileName, FileNameExt: AnsiString;
PropArray: Array [0 .. 4] Of TSPropValue;
Begin
CountWritten := 0;
Stream.Seek(0, soFromBeginning);
TagProp := PR_ATTACH_DATA_BIN;
Attachment := nil;
DisplayName := AnsiString(ExtractFileName(String(AttName)));
FileNameExt := AnsiString(ExtractFileExt(String(DisplayName)));
ShortFileName := AnsiString
(ExtractFileName(ExtractShortPathName(String(AttName))));
FillChar(PropArray, SizeOf(PropArray), 0);
PropArray[0].ulPropTag := PR_ATTACH_METHOD;
PropArray[0].Value.ul := ATTACH_BY_VALUE;
PropArray[1].ulPropTag := PR_ATTACH_LONG_FILENAME_A;
PropArray[1].Value.lpszA := PAnsiChar(DisplayName);
PropArray[2].ulPropTag := PR_ATTACH_FILENAME_A;
PropArray[2].Value.lpszA := PAnsiChar(ShortFileName);
PropArray[3].ulPropTag := PR_DISPLAY_NAME_A;
PropArray[3].Value.lpszA := PAnsiChar(DisplayName);
PropArray[4].ulPropTag := PR_ATTACH_EXTENSION_A;
PropArray[4].Value.lpszA := PAnsiChar(FileNameExt);
Try
hr := MAPIMessage.CreateAttach(nil, 0, AttachmentNumber, Attachment);
If failed(hr) Then
Raise EMAPIError.CreateMAPI(MAPIMessage, hr);
hr := Attachment.SetProps(5, @PropArray, PSPropProblemArray(nil^));
If failed(hr) Then
Raise EMAPIError.CreateMAPI(Attachment, hr);
hr := Attachment.OpenProperty(TagProp, @IID_IStream, 0, MAPI_CREATE Or
MAPI_MODIFY, IUnknown(ComStream));
If failed(hr) Then
Raise EMAPIError.CreateMAPI(Attachment, hr);
hr := ComStream.Write(Stream.Memory, Stream.Size, @CountWritten);
If failed(hr) Then
Raise EMAPIError.CreateSys(hr);
hr := ComStream.Commit(STGC_DEFAULT);
If failed(hr) Then
Raise EMAPIError.CreateSys(hr);
hr := Attachment.SaveChanges(0);
If failed(hr) Then
Raise EMAPIError.CreateSys(hr);
Finally
ComStream := nil;
Attachment := nil;
End;
End;
Procedure AddMsgAttachements(Const MAPIMessage: IMessage;
AttachmentsList: TStrings);
Var
iCount: Integer;
SourceFileName: String;
Stream: TMemoryStream;
Begin
Stream := nil;
If Not Assigned(MAPIMessage) Or Not Assigned(AttachmentsList) Then
Raise EMAPIError.CreateMAPI(nil, MAPI_E_INVALID_PARAMETER);
Stream := TMemoryStream.Create;
Try
For iCount := 0 To AttachmentsList.Count - 1 Do
Begin
SourceFileName := AttachmentsList[iCount];
If Not FileExists(SourceFileName) Then
Raise EMAPIError.Create('Cannot locate file: ' + SourceFileName);
/// =================================///
Try
Stream.LoadFromFile(SourceFileName);
Stream.Seek(0, soFromBeginning);
CreateAttachement(MAPIMessage, Stream, AnsiString(SourceFileName));
Except
// do nothing
End;
// ==================================///
End;
Finally
If Assigned(Stream) Then
FreeAndNil(Stream);
End;
End;
procedure SetEmlParams(RecListTo, RecListBcc, Attachements: TStringList;
SubjectLine, BodyPath: AnsiString);
var
SentMailEID: PSPropValue;
MAPIMessage: IMessage;
TempString: AnsiString;
StrBody: TStringStream;
BodyStream: IStream;
StreamAdapter: TStreamAdapter;
StreamSize: Int64;
iCount: Integer;
sADDRTYPE, sEMAILADDRESS: AnsiString;
HowManyProp: ULONG;
PropValues: Array [0 .. 3] Of TSPropValue;
AddressEntry: PADRLIST;
begin
SentMailEID := nil;
MAPIMessage := nil;
StrBody := nil;
StreamAdapter := nil;
AddressEntry := nil;
// Get Sent Items Folder - we will move message to this folder after submit
hr := HrGetOneProp(UserPrivateStore, PR_IPM_SENTMAIL_ENTRYID, SentMailEID);
if failed(hr) then
raise EMAPIError.CreateMAPI(UserPrivateStore, hr);
SentMailEID.ulPropTag := PR_SENTMAIL_ENTRYID;
try
// Create a new empty message
hr := Outbox.CreateMessage(nil, 0, MAPIMessage);
if failed(hr) then
raise EMAPIError.CreateMAPI(Outbox, hr);
// Set Message Class
hr := HrMAPISetPropString(MAPIMessage, PR_MESSAGE_CLASS_A,
Pointer(PAnsiChar('IPM.Note')));
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPIMessage, hr);
// Set Sent Items Entry ID
hr := HrSetOneProp(MAPIMessage, SentMailEID);
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPIMessage, hr);
// Set Message Subject
TempString := AnsiString(Trim(String(SubjectLine)));
if TempString <> '' then
begin
hr := HrMAPISetPropString(MAPIMessage, PR_SUBJECT_A,
Pointer(PAnsiChar(TempString)));
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPIMessage, hr);
hr := HrMAPISetPropString(MAPIMessage, PR_CONVERSATION_TOPIC_A,
PAnsiChar(TempString));
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPIMessage, hr);
end;
// Set Message Body
TempString := AnsiString(Trim(String(BodyPath)));
if FileExists(String(TempString)) then
begin
StrBody := TStringStream.Create('');
{$IFDEF DELPHI10}
StrBody.LoadFromFile(TempString);
{$ELSE}
StrBody.CopyFrom(TFileStream.Create(String(TempString),
fmOpenRead or fmShareDenyWrite), MAXINT);
{$ENDIF}
StreamSize := StrBody.Size;
StrBody.Position := 0;
hr := MAPIMessage.OpenProperty(PR_HTML, @IID_IStream,
STGM_WRITE Or STGM_DIRECT, MAPI_CREATE or MAPI_MODIFY,
IUnknown(BodyStream));
if failed(hr) then
raise EMAPIError.CreateMAPI(MAPIMessage, hr);
StreamAdapter := TStreamAdapter.Create(StrBody);
hr := StreamAdapter.CopyTo(BodyStream, StreamSize, Int64(nil^),
Int64(nil^));
If failed(hr) Then
Raise EMAPIError.CreateSys(hr);
hr := StreamAdapter.Commit(STGC_DEFAULT);
If failed(hr) Then
Raise EMAPIError.CreateSys(hr);
hr := HrMAPISetPropLong(MAPIMessage, PR_MSG_EDITOR_FORMAT,
EDITOR_FORMAT_HTML);
If failed(hr) Then
Raise EMAPIError.CreateSys(hr);
end;
AddMsgAttachements(MAPIMessage, Attachements);
// Recepients !!!
If Assigned(RecListTo) And (RecListTo.Count > 0) Then
Begin
For iCount := 0 To RecListTo.Count - 1 Do
Begin
sADDRTYPE := '';
sEMAILADDRESS := AnsiString(Trim(RecListTo[iCount]));
sADDRTYPE := AnsiString(Copy(String(sEMAILADDRESS), 1,
Pos(':', String(sEMAILADDRESS)) - 1));
If Length(sADDRTYPE) > 1 Then
Delete(sEMAILADDRESS, 1, Length(sADDRTYPE) + 1)
Else
sADDRTYPE := 'SMTP'; // Default Value
sADDRTYPE := AnsiString(UpperCase(String(sADDRTYPE)));
If sADDRTYPE = 'SMTP' Then
HowManyProp := 4
Else
HowManyProp := 4;
FillChar(PropValues, SizeOf(PropValues), 0);
PropValues[0].ulPropTag := PR_EMAIL_ADDRESS_A;
PropValues[0].Value.lpszA := PAnsiChar(sEMAILADDRESS);
PropValues[1].ulPropTag := PR_RECIPIENT_TYPE;
PropValues[1].Value.l := MAPI_TO;
PropValues[2].ulPropTag := PR_ADDRTYPE_A;
PropValues[2].Value.lpszA := PAnsiChar(sADDRTYPE);
PropValues[3].ulPropTag := PR_DISPLAY_NAME_A;
PropValues[3].Value.lpszA := PAnsiChar(sEMAILADDRESS);
If Not Assigned(AddressEntry) Then
hr := HrMAPICreateAddressList(HowManyProp, @PropValues, AddressEntry)
Else
hr := HrMAPIAppendAddressList(HowManyProp, @PropValues, AddressEntry);
End;
End;
If Assigned(RecListBcc) And (RecListBcc.Count > 0) Then
Begin
For iCount := 0 To RecListBcc.Count - 1 Do
Begin
sADDRTYPE := '';
sEMAILADDRESS := AnsiString(Trim(RecListBcc[iCount]));
sADDRTYPE := AnsiString(Copy(String(sEMAILADDRESS), 1,
Pos(':', String(sEMAILADDRESS)) - 1));
If Length(sADDRTYPE) > 1 Then
Delete(sEMAILADDRESS, 1, Length(sADDRTYPE) + 1)
Else
sADDRTYPE := 'SMTP'; // Default Value
sADDRTYPE := AnsiString(UpperCase(String(sADDRTYPE)));
If sADDRTYPE = 'SMTP' Then
HowManyProp := 4
Else
HowManyProp := 4;
FillChar(PropValues, SizeOf(PropValues), 0);
PropValues[0].ulPropTag := PR_EMAIL_ADDRESS_A;
PropValues[0].Value.lpszA := PAnsiChar(sEMAILADDRESS);
PropValues[1].ulPropTag := PR_RECIPIENT_TYPE;
PropValues[1].Value.l := MAPI_CC;
PropValues[2].ulPropTag := PR_ADDRTYPE_A;
PropValues[2].Value.lpszA := PAnsiChar(sADDRTYPE);
PropValues[3].ulPropTag := PR_DISPLAY_NAME_A;
PropValues[3].Value.lpszA := PAnsiChar(sEMAILADDRESS);
If Not Assigned(AddressEntry) Then
hr := HrMAPICreateAddressList(HowManyProp, @PropValues, AddressEntry)
Else
hr := HrMAPIAppendAddressList(HowManyProp, @PropValues, AddressEntry);
End;
End;
If Assigned(AddressEntry) Then
Begin
hr := AddressBook.PrepareRecips(0, @PropForCorrect, AddressEntry);
If failed(hr) Then
Raise EMAPIError.CreateMAPI(AddressBook, hr);
hr := AddressBook.ResolveName(0, 0 { MAPI_DIALOG or AB_UNICODEUI } , nil,
AddressEntry);
// if (failed(hr)) then
// raise EMAPIError.CreateMAPI(AddressBook, hr);
End;
hr := MAPIMessage.ModifyRecipients(0, AddressEntry);
If failed(hr) Then
Raise EMAPIError.CreateMAPI(MAPIMessage, hr);
hr := MAPIMessage.SaveChanges(KEEP_OPEN_READWRITE);
If failed(hr) Then
Raise EMAPIError.CreateMAPI(MAPIMessage, hr);
hr := MAPIMessage.SubmitMessage(FORCE_SUBMIT);
If failed(hr) Then
Raise EMAPIError.CreateMAPI(MAPIMessage, hr);
finally
If Assigned(AddressEntry) Then
FreePadrlist(AddressEntry);
if Assigned(StrBody) then
FreeAndNil(StrBody);
if Assigned(StreamAdapter) then
FreeAndNil(StreamAdapter);
BodyStream := nil;
// Clear Sent Item Entry ID
if Assigned(SentMailEID) then
MAPIFreeBuffer(SentMailEID);
end;
end;
procedure ProcessParams;
var
RecListTo, RecListBcc, Attachements: TStringList;
I: Integer;
S: AnsiString;
SR: AnsiString;
SubjectLine: AnsiString;
BodyPath: AnsiString;
begin
RecListTo := TStringList.Create;
RecListBcc := TStringList.Create;
Attachements := TStringList.Create;
try
for I := 1 to ParamCount do
begin
S := AnsiString(ParamStr(I));
if (Copy(S, 1, 1) = '-') or (Copy(S, 1, 1) = '/') then
begin
if CompareText(Copy(String(S), 2, 1), cRecepientsTo) = 0 then
begin
SR := Copy(S, 4, Length(S));
SR := AnsiString(StringReplace(String(SR), '"', '', [rfReplaceAll]));
RecListTo.Clear;
RecListTo.Delimiter := ',';
RecListTo.DelimitedText := String(SR);
end
else if CompareText(Copy(String(S), 2, 1), cRecepientsBcc) = 0 then
begin
SR := Copy(S, 4, Length(S));
SR := AnsiString(StringReplace(String(SR), '"', '', [rfReplaceAll]));
RecListBcc.Clear;
RecListBcc.Delimiter := ',';
RecListBcc.DelimitedText := String(SR);
end
else if CompareText(Copy(String(S), 2, 1), cSubject) = 0 then
begin
SR := Copy(S, 4, Length(S));
SubjectLine := AnsiString(StringReplace(String(SR), '"', '',
[rfReplaceAll]));
end
else if CompareText(Copy(String(S), 2, 1), cBodyPath) = 0 then
begin
SR := Copy(S, 4, Length(S));
BodyPath := AnsiString(StringReplace(String(SR), '"', '',
[rfReplaceAll]));
end
else if CompareText(Copy(String(S), 2, 1), cAttachementsPath) = 0 then
begin
SR := Copy(S, 4, Length(S));
SR := AnsiString(StringReplace(String(SR), '"', '', [rfReplaceAll]));
Attachements.Clear;
Attachements.Delimiter := ',';
Attachements.DelimitedText := String(SR);
end;
end;
end;
SetEmlParams(RecListTo, RecListBcc, Attachements, SubjectLine, BodyPath);
finally
FreeAndNil(Attachements);
FreeAndNil(RecListBcc);
FreeAndNil(RecListTo);
end;
end;
procedure GetInputMAPIParams;
var
I: Integer;
S: AnsiString;
begin
for I := 1 to ParamCount do
begin
S := AnsiString(ParamStr(I));
if (Copy(S, 1, 1) = '-') or (Copy(S, 1, 1) = '/') then
begin
if CompareText(Copy(String(S), 2, 1), cProfile) = 0 then
begin
MAPIProfile := Copy(S, 4, Length(S));
MAPIProfile := AnsiString(StringReplace(String(MAPIProfile), '"', '',
[rfReplaceAll]));
end
else if CompareText(Copy(String(S), 2, 1), cUser) = 0 then
begin
UserEMailAddress := Copy(S, 4, Length(S));
UserEMailAddress := AnsiString(StringReplace(String(UserEMailAddress),
'"', '', [rfReplaceAll]));
end
end;
end;
end;
begin
try
try
// Init MAPI Subsystem
if InitMAPI then
begin
Writeln('GetInputMAPIParams -> Done!');
GetInputMAPIParams;
// Establish Session
if LogonToMapi then
begin
Writeln('LogonToMapi -> Done!');
// Load your OWN Mailbox (Administrative mailbox)
LoadPrivateMDB;
Writeln('LoadPrivateMDB -> Done!');
// Open Address Book;
LoadAddressBook;
Writeln('LoadAddressBook -> Done!');
// Resolve Other user
ResolveEmlAddr;
Writeln('Resolve Other user -> Done!');
// Open Other User MAilbox
OpenOtherUsersMailbox;
Writeln('Open Other Users Mailbox -> Done!');
// Get Outbox
GetOutbox;
Writeln('Get Outbox -> Done!');
// Process Params
ProcessParams;
Writeln('');
Writeln('Send Message -> Done! Press any key to exit!');
Readln;
end;
end;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
Writeln('Press any key to exit!');
Readln;
end;
end;
finally
if IsInitOK then
MAPIClose;
end;
end.
Download Request #22 as Compiled Application
Download Project (DELPHI 10.4) ZIP file
Source Code: In package

