|
Как общаться с MAPI ?
#32178749
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
Ссылка на профиль пользователя:
|
Участник
Откуда: Вологда
Сообщения: 1 587
|
|
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. 152. 153. 154. 155. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. 171. 172. 173. 174. 175. 176. 177. 178. 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191. 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. 212. 213. 214. 215. 216. 217. 218. 219. 220. 221. 222. 223. 224. 225. 226. 227. 228. 229. 230. 231. 232. 233. 234. 235. 236. 237. 238. 239. 240. 241. 242. 243. 244. 245. 246. 247. 248. 249. 250. 251. 252. 253. 254. 255. 256. 257. 258. 259. 260. 261. 262. 263. 264. 265. 266. 267. 268. 269. 270. 271. 272. 273. 274. 275. 276. 277. 278. 279. 280. 281. 282. 283. 284. 285. 286. 287. 288. 289. 290. 291.
unit SendMail;
interface
uses
Classes,dialogs;
type
TMAPIMail = class(TComponent)
private
{ Private declarations }
FLastError: Integer;
FSubject: string;
FBody: string;
FSenderName: string;
FSenderAddress: string;
FRecipients: TStrings;
FAttachments: TStrings;
FEditDialog: Boolean;
procedure SetRecipients(Value: TStrings);
procedure SetAttachments(Value: TStrings);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function Send: Boolean;
function MAPIErrorDescription(intErrorCode: Integer): string;
property LastError: Integer read FLastError;
published
{ Published declarations }
property Subject: string read FSubject write FSubject;
property Body: string read FBody write FBody;
property Recipients: TStrings read FRecipients write SetRecipients;
property Attachments: TStrings read FAttachments write SetAttachments;
property EditDialog: Boolean read FEditDialog write FEditDialog;
property SenderName: string read FSenderName write FSenderName;
property SenderAddress: string read FSenderAddress write FSenderAddress;
end;
function SendEMailByMAPI(SenderName, SenderAddress, Subject, Body: string; Recipients, Attachments: TStrings; WithOpenMessage: Boolean): Integer;
implementation
uses SysUtils, MAPI, Forms, Windows;
function SendEMailByMAPI(SenderName, SenderAddress, Subject, Body: string; Recipients, Attachments: TStrings; WithOpenMessage: Boolean): Integer;
const
RECIP_MAX = MaxInt div SizeOf(TMapiRecipDesc);
ATTACH_MAX = MaxInt div SizeOf(TMapiFileDesc);
type
TRecipAccessArray = array [0..(RECIP_MAX - 1)] of TMapiRecipDesc;
TlpRecipArray = ^TRecipAccessArray;
TAttachAccessArray = array [0..(ATTACH_MAX - 1)] of TMapiFileDesc;
TlpAttachArray = ^TAttachAccessArray;
TszRecipName = array[0..256] of Char;
TlpszRecipName = ^TszRecipName;
TszPathName = array[0..256] of Char;
TlpszPathname = ^TszPathname;
TszFileName = array[0..256] of Char;
TlpszFileName = ^TszFileName;
var
i: Integer;
Message: TMapiMessage;
lpRecipArray: TlpRecipArray;
lpAttachArray: TlpAttachArray;
function CheckRecipient(strRecipient: string): Integer;
var
lpRecip: PMapiRecipDesc;
begin
try
Result := MapiResolveName(0, 0, PChar(strRecipient), 0, 0, lpRecip);
if (Result in [MAPI_E_AMBIGUOUS_RECIPIENT,
MAPI_E_UNKNOWN_RECIPIENT]) then
Result := MapiResolveName(0, 0, PChar(strRecipient), MAPI_DIALOG, 0, lpRecip);
if Result = SUCCESS_SUCCESS then
begin
strRecipient := StrPas(lpRecip^.lpszName);
with lpRecipArray^ do
begin
lpszName := StrCopy(new(TlpszRecipName)^, lpRecip^.lpszName);
if lpRecip^.lpszAddress = nil then
lpszAddress := StrCopy(new(TlpszRecipName)^, lpRecip^.lpszName)
else
lpszAddress := StrCopy(new(TlpszRecipName)^, lpRecip^.lpszAddress);
ulEIDSize := lpRecip^.ulEIDSize;
lpEntryID := lpRecip^.lpEntryID;
MapiFreeBuffer(lpRecip);
end
end;
finally
end;
end;
function SendMess: Integer;
const
arrMAPIFlag: array[Boolean] of Word = (0, MAPI_DIALOG);
begin
try
Result := MAPISendMail(0, Application.Handle{0}, Message,
arrMAPIFlag[WithOpenMessage] or
MAPI_LOGON_UI {or MAPI_NEW_SESSION}, 0)
finally
end;
end;
var
lpSender: TMapiRecipDesc;
s: string;
begin
FillChar(Message, SizeOf(Message), 0);
with Message do
begin
if (SenderAddress <> '') then
begin
lpSender.ulRecipClass := MAPI_ORIG;
if (SenderName <> '') then
lpSender.lpszName := PChar(SenderAddress)
else
lpSender.lpszName := PChar(SenderName);
lpSender.lpszAddress := PChar(SenderAddress);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := @lpSender;
end;
lpszSubject := PChar(Subject);
lpszNoteText := PChar(Body);
if Assigned(Attachments) then
begin
nFileCount := Attachments.Count;
lpAttachArray := TlpAttachArray(StrAlloc(nFileCount*SizeOf(TMapiFileDesc)));
FillChar(lpAttachArray^, StrBufSize(PChar(lpAttachArray)), 0);
for i := 0 to nFileCount-1 do
begin
lpAttachArray^.nPosition := ULONG($FFFFFFFF); //Cardinal(-1); //Cardinal($FFFFFFFF); //ULONG(-1);
lpAttachArray^.lpszPathName := StrPCopy(new(TlpszPathname)^, Attachments);
lpAttachArray^.lpszFileName := StrPCopy(new(TlpszFileName)^, ExtractFileName(Attachments));
end;
lpFiles := @lpAttachArray^
end
else
nFileCount := 0;
end;
if Assigned(Recipients) then
begin
lpRecipArray := TlpRecipArray(StrAlloc(Recipients.Count*SizeOf(TMapiRecipDesc)));
FillChar(lpRecipArray^, StrBufSize(PChar(lpRecipArray)), 0);
for i := 0 to Recipients.Count-1 do
begin
s := Recipients;
if (UpperCase(Copy(s, 1, 3)) = 'CC:') then
begin
lpRecipArray^.ulRecipClass := MAPI_CC;
Delete(s, 1, 3);
end
else
if (UpperCase(Copy(s, 1, 4)) = 'BCC:') then
begin
lpRecipArray^.ulRecipClass := MAPI_BCC;
Delete(s, 1, 4);
end
else
lpRecipArray^.ulRecipClass := MAPI_TO;
CheckRecipient(s);
end;
Message.nRecipCount := Recipients.Count;
Message.lpRecips := @lpRecipArray^;
end
else
Message.nRecipCount := 0;
Result := SendMess;
if Assigned(Attachments) then
begin
for i := 0 to Message.nFileCount-1 do
begin
Dispose(lpAttachArray^.lpszPathname);
Dispose(lpAttachArray^.lpszFileName);
end;
StrDispose(PChar(lpAttachArray));
end;
if Assigned(Recipients) then
begin
for i := 0 to Message.nRecipCount-1 do
begin
if Assigned(lpRecipArray^.lpszName) then
Dispose(lpRecipArray^.lpszName);
if Assigned(lpRecipArray^.lpszAddress) then
Dispose(lpRecipArray^.lpszAddress);
end;
StrDispose(PChar(lpRecipArray));
end;
end;
{ TMAPIMail }
constructor TMAPIMail.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
EditDialog := True;
FRecipients := TStringList.Create;
FAttachments := TStringList.Create;
end;
destructor TMAPIMail.Destroy;
begin
FRecipients.Free;
Attachments.Free;
inherited Destroy;
end;
procedure TMAPIMail.SetRecipients(Value: TStrings);
begin
FRecipients.Assign(Value)
end;
procedure TMAPIMail.SetAttachments(Value: TStrings);
begin
Attachments.Assign(Value)
end;
function TMAPIMail.Send: Boolean;
begin
FLastError := SendEMailByMAPI(SenderName, SenderAddress, Subject, Body, Recipients, Attachments, EditDialog);
Result := (LastError = SUCCESS_SUCCESS);
end;
function TMAPIMail.MAPIErrorDescription(intErrorCode: Integer): string;
begin
case intErrorCode of
MAPI_E_USER_ABORT: Result := 'User cancelled request';
MAPI_E_FAILURE: Result := 'General MAPI failure';
MAPI_E_LOGON_FAILURE: Result := 'Logon failure';
MAPI_E_DISK_FULL: Result := 'Disk full';
MAPI_E_INSUFFICIENT_MEMORY: Result := 'Insufficient memory';
MAPI_E_ACCESS_DENIED: Result := 'Access denied';
MAPI_E_TOO_MANY_SESSIONS: Result := 'Too many sessions';
MAPI_E_TOO_MANY_FILES: Result := 'Too many files open';
MAPI_E_TOO_MANY_RECIPIENTS: Result := 'Too many recipients';
MAPI_E_ATTACHMENT_NOT_FOUND: Result := 'Attachment not found';
MAPI_E_ATTACHMENT_OPEN_FAILURE: Result := 'Failed to open attachment';
MAPI_E_ATTACHMENT_WRITE_FAILURE: Result := 'Failed to write attachment';
MAPI_E_UNKNOWN_RECIPIENT: Result := 'Unknown recipient';
MAPI_E_BAD_RECIPTYPE: Result := 'Invalid recipient type';
MAPI_E_NO_MESSAGES: Result := 'No messages';
MAPI_E_INVALID_MESSAGE: Result := 'Invalid message';
MAPI_E_TEXT_TOO_LARGE: Result := 'Text too large.';
MAPI_E_INVALID_SESSION: Result := 'Invalid session';
MAPI_E_TYPE_NOT_SUPPORTED: Result := 'Type not supported';
MAPI_E_AMBIGUOUS_RECIPIENT: Result := 'Ambiguous recipient';
MAPI_E_MESSAGE_IN_USE: Result := 'Message in use';
MAPI_E_NETWORK_FAILURE: Result := 'Network failure';
MAPI_E_INVALID_EDITFIELDS: Result := 'Invalid edit fields';
MAPI_E_INVALID_RECIPS: Result := 'Invalid recipients';
MAPI_E_NOT_SUPPORTED: Result := 'Not supported';
else
Result := 'Unknown Error Code: ' + IntToStr(intErrorCode);
end;
end;
end.
|
|
|