Delphi .NET (2) Database (71) Delphi IDE (89) Network (39) Printing (3) Strings (12) VCL (83) Windows with Delphi (280)
Exchange Links About this site Links to us 
|
Sending an email from Delphi using MAPI
22 comments. Current rating: (6 votes). Leave comments and/ or rate it.
If you do not want to rely on Outlook to send an email but you know that MAPI is installed, then you can also send mails with the following handy routine SendMailMAPI(). You need to add unit MAPI to your uses clause.
Note that MAPI is not always installed with Windows.
Thanks to Sven Sonderegger for improving the code with the 'SMTP:' prefix.  | |  | | program MAPIMail;
uses
MAPI;
function SendMailMAPI(const Subject, Body, FileName, SenderName, SenderEMail,
RecepientName, RecepientEMail: String) : Integer;
var
message: TMapiMessage;
lpSender,
lpRecepient: TMapiRecipDesc;
FileAttach: TMapiFileDesc;
SM: TFNMapiSendMail;
MAPIModule: HModule;
begin
FillChar(message, SizeOf(message), 0);
with message do
begin
if (Subject<>'') then
begin
lpszSubject := PChar(Subject)
end;
if (Body<>'') then
begin
lpszNoteText := PChar(Body)
end;
if (SenderEMail<>'') then
begin
lpSender.ulRecipClass := MAPI_ORIG;
if (SenderName='') then
begin
lpSender.lpszName := PChar(SenderEMail)
end
else
begin
lpSender.lpszName := PChar(SenderName)
end;
lpSender.lpszAddress := PChar('SMTP:'+SenderEMail);
lpSender.ulReserved := 0;
lpSender.ulEIDSize := 0;
lpSender.lpEntryID := nil;
lpOriginator := @lpSender;
end;
if (RecepientEMail<>'') then
begin
lpRecepient.ulRecipClass := MAPI_TO;
if (RecepientName='') then
begin
lpRecepient.lpszName := PChar(RecepientEMail)
end
else
begin
lpRecepient.lpszName := PChar(RecepientName)
end;
lpRecepient.lpszAddress := PChar('SMTP:'+RecepientEMail);
lpRecepient.ulReserved := 0;
lpRecepient.ulEIDSize := 0;
lpRecepient.lpEntryID := nil;
nRecipCount := 1;
lpRecips := @lpRecepient;
end
else
begin
lpRecips := nil
end;
if (FileName='') then
begin
nFileCount := 0;
lpFiles := nil;
end
else
begin
FillChar(FileAttach, SizeOf(FileAttach), 0);
FileAttach.nPosition := Cardinal($FFFFFFFF);
FileAttach.lpszPathName := PChar(FileName);
nFileCount := 1;
lpFiles := @FileAttach;
end;
end;
MAPIModule := LoadLibrary(PChar(MAPIDLL));
if MAPIModule=0 then
begin
Result := -1
end
else
begin
try
@SM := GetProcAddress(MAPIModule, 'MAPISendMail');
if @SM<>nil then
begin
Result := SM(0, Application.Handle, message, MAPI_DIALOG or
MAPI_LOGON_UI, 0);
end
else
begin
Result := 1
end;
finally
FreeLibrary(MAPIModule);
end;
end
if Result<>0 then
begin
MessageDlg('Error sending mail ('+IntToStr(Result)+').', mtError, [mbOk],
0)
end;
end;
begin
end. | |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
| You are on page 1 of 2, other pages: [1] 2 | |
anonymous from Russian Federation
|
 |
|
|
|
|
lugbestrio@yahoo.com from Brazil
|
 |
|
|
Hello how to write a event OnSaveAs (when user clicks in menu File|Save as) using this excelent function?
I need capture the full path of file because open it last and execute others procedures.
Thanks, Luiz Gustavo
|
|
anonymous from Germany
|
|
|
|
hi, this is nearly what i was searching for. one question: when i run the function the 'new email'-window from outlook express opens. is it possible to send the email automatically without the necessity to answer this window by pressing 'send'? thx a lot for tips.
|
|
anonymous from United States
|
|
|
|
when I run this function and do not specify a recipients email name, it will put the email address in the recipient name. When I send it this way in outlook, I get there error
The following recipient(s) could not be reached:
'xxx@yyy.com' on 8/30/2006 2:20 PM
None of your e-mail accounts could send to this recipient.
Note: I substituted xxx@yyy.com for a valid email.
It looks like it is trying to find the name and ignores the email address
|
|
dcalliari79@tiscali.it from Italy
|
|
|
|
I've got this error:
-if I write:
...
lpSender.lpszAddress := PChar('SMTP:'+SenderEMail);
...
lpRecepient.lpszAddress := PChar('SMTP:'+RecepientEMail);
...
it sends the email, but my PDF attachment is in the format: 'winmail.dat' and doesn't work.
-if I write:
...
lpSender.lpszAddress := PChar(SenderEMail);
...
lpRecepient.lpszAddress := PChar(RecepientEMail);
...
I get the same errore explained above-mentioned:
The following recipient(s) could not be reached:
'xxx@yyy.com' on 8/30/2006 2:20 PM
None of your e-mail accounts could send to this recipient.
Thanks for your help,
Dimitri
|
|
Z.,Richard from Hungary
|
|
|
|
Hi! I send e-mails from vbscript and i could send a lot. But now I reinstall the Windows and now i can't send emails- Please help!!!
|
|
anonymous
|
 |
|
|
THANKS ALOT for this code!
|
|
Wewnon from Taiwan
|
|
|
|
Thank for the code!
|
|
anonymous from India
|
|
|
|
Hi,
when i run the function the 'new email'-window opens. Is it possible to send the email automatically without the necessity to answer this window by pressing 'send'?
Thanks a lot in advance
|
|
anonymous from India
|
|
|
|
Hi,
when i run the function the 'new email'-client mail window opens. Is it possible to send the email automatically without the necessity to answer this window by pressing 'send'?
Thanks a lot in advance
|
|
PerryJ from United States
|
 |
|
|
Has anyone solved the issue with the PDF being in the winmail.dat format?
|
|
julot@infomaniak.ch from Switzerland
|
|
|
|
When I send the mail (using Outlook 2003) , I get next error code : Error sending mail (11). Could someone send me an example which works properly ? Thanks a lot - all the best.
|
|
julot@infomaniak.ch from Switzerland
|
|
|
|
I got it ! Excellent - thanks a lot !
|
|
anonymous from India
|
|
|
|
but the new mail windows open in outlook express......is there any way to send mail without opening the send mail window......??
|
|
anonymous from Norway
|
|
|
|
From documentation, remove MAPI_DIALOG and the or MAPI_LOGON_UI
from this:
Result := SM(0, Application.Handle, message, MAPI_DIALOG or MAPI_LOGON_UI, 0);
Most possibly this would work:
Result := SM(0, Application.Handle, message, 0, 0);
|
| You are on page 1 of 2, other pages: [1] 2 |
|