To my knowledge you can't. I wrote the following procedure in delphi 5 to
solve this problem (and others).
You can set all fields of an email with this procedure.
Greetings,
Kees Willem van der Meer
uses MAPI, ....
procedure MailTo (Recipients, CC, BCC, AttachedFiles: TStringList; Subject,
Body: string);
var Recips: packed array of MAPIREcipDesc;
procedure AddRecipients (RecipClass: cardinal; S: TStringList);
var i: integer;
begin
if S<>nil then
begin
Setlength (REcips, length(Recips)+S.Count);
for i:=0 to S.Count-1 do
with Recips[length(Recips)-S.Count+i] do
begin
ulReserved := 0;
ulRecipClass := REcipClass;
lpszName := StrNew(PChar(S[i]));
lpszAddress := StrNew(PChar('SMTP:'+S[i]));
ulEIDSize := 0;
lpEntryID := nil;
end
end
end;
var MapiMsg: MapiMessage;
i: integer;
Orig: MAPIRecipdesc;
AttFiles: packed array of MapiFileDesc;
begin
FillChar (MapiMsg, sizeof(MapiMSG), 0);
MapiMsg.lpszSubject := PChar (Subject);
MapiMSg.lpszNoteText := PChar (Body);
AddRecipients (MAPI_TO, Recipients);
AddRecipients (MAPI_CC, CC);
AddRecipients (MAPI_BCC, BCC);
MapiMsg.nRecipCount := length(Recips);
MapiMsg.lpRecips := pointer(Recips);
Fillchar (Orig, sizeof(Orig), 0);
MapiMsg.lpOriginator := @Orig;
if AttachedFiles<>nil then
begin
MapiMsg.nFileCount := AttachedFiles.Count;
SetLength (AttFiles, AttachedFiles.Count);
for i:=0 to AttachedFiles.Count-1 do
with AttFiles[i] do
begin
ulReserved := 0;
flFlags := 0;
nPosition := $FFFFFFFF;
lpszPathName := PChar(AttachedFiles[i]);
lpszFileName := nil;
lpFileType := 0;
end;
MapiMsg.lpFiles := pointer(AttFiles);
end;
case MAPISendMail (0, Application.Handle, MapiMsg, MAPI_DIALOG or
MAPI_LOGON_UI or MAPI_NEW_SESSION, 0)of
SUCCESS_SUCCESS: ;
MAPI_E_AMBIGUOUS_RECIPIENT: raise Exception.Create ('A recipient matched
more than one of the recipient descriptor structures and MAPI_DIALOG was not
set. No message was sent.');
MAPI_E_ATTACHMENT_NOT_FOUND: raise Exception.Create ('The specified
attachment was not found. No message was sent.');
MAPI_E_ATTACHMENT_OPEN_FAILURE: raise Exception.Create ('The specified
attachment could not be open; no message was sent.');
MAPI_E_BAD_RECIPTYPE: raise Exception.Create ('The type of a recipient
was not MAPI_TO, MAPI_CC, or MAPI_BCC. No message was sent.');
MAPI_E_FAILURE: raise Exception.Create ('One or more unspecified errors
occurred; no message was sent.');
MAPI_E_INSUFFICIENT_MEMORY: raise Exception.Create ('There was
insufficient memory to proceed. No message was sent.');
MAPI_E_LOGIN_FAILURE: raise Exception.Create ('There was no default
logon, and the user failed to log on successfully when the logon dialog box
was displayed. No message was sent.');
MAPI_E_TEXT_TOO_LARGE: raise Exception.Create ('The text in the message
was too large to sent; the message was not sent.');
MAPI_E_TOO_MANY_FILES: raise Exception.Create ('There were too many file
attachments; no message was sent.');
MAPI_E_TOO_MANY_RECIPIENTS: raise Exception.Create ('There were too many
recipients; no message was sent.');
MAPI_E_UNKNOWN_RECIPIENT: raise Exception.Create ('A recipient did not
appear in the address list; no message was sent.');
MAPI_E_USER_ABORT: raise Exception.Create ('The user canceled one of the
dialog boxes; no message was sent.');
else
RaiseLastWin32Error;
end;
for i:=0 to length(Recips)-1 do
begin
StrDispose (Recips[i].lpszName);
StrDispose(Recips[i].lpszAddress);
end
end;
Look, there's a bug in my code. It should include a try-finally block.
I just remembered that it first didn't run on my computer (running Windows
98) because of the MAPI-unit expecting some registry keys. To solve this i
commented out some code in the MAPI-unit. See below.
Kees Willem van der Meer
The corrected code for MailTo
try
finally
for i:=0 to length(Recips)-1 do
begin
StrDispose (Recips[i].lpszName);
StrDispose(Recips[i].lpszAddress);
end
end
end;
In the MAPI-unit:
procedure InitMapi;
var
OSVersionInfo: TOSVersionInfo;
hkWMS: HKEY;
MAPIValueSize: Longint;
MAPIValueBuf: array[0..8] of char;
rType: Longint;
begin
if not MAPIChecked then
begin
MAPIChecked := True;
MAPIModule := 0;
(* Do not check any registry keys
OSVersionInfo.dwOSVersionInfoSize := SizeOf(OSVersionInfo);
GetVersionEx(OSVersionInfo);
if (OSVersionInfo.dwMajorVersion > 3) or
((OSVersionInfo.dwMajorVersion = 3) and
(OSVersionInfo.dwMinorVersion > 51)) then
begin
MAPIValueSize := sizeof(MAPIValueBuf);
if RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'SOFTWARE\Microsoft\Windows
Messaging Subsystem',
0, KEY_READ, hkWMS) <> ERROR_SUCCESS then Exit;
if RegQueryValueEx(hkWMS, 'MAPI', nil, @rType, @MAPIValueBuf,
@MAPIValueSize) <> ERROR_SUCCESS then Exit;
RegCloseKey(hkWMS);
if not ((MAPIValueBuf[0] = '1') and (MAPIValueBuf[1] = #0)) then Exit;
end
else
*)
if GetProfileInt('Mail', 'MAPI', 0) = 0 then Exit;
MAPIModule := LoadLibrary(PChar(MAPIDLL));
end;
end;