DelphiFAQ Home Search:

Sending an email from Delphi using MAPI

 

comments23 comments. Current rating: 5 stars (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
2006-03-06, 02:14:24
anonymous from Russian Federation  
rating
2006-03-31, 15:40:25
lugbestrio@yahoo.com from Brazil  
rating
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
2006-06-25, 16:00:46
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.
2006-08-30, 12:46:37
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
2006-09-04, 08:05:12
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
2007-07-10, 07:08:37
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!!!
2007-09-06, 10:58:15
anonymous  
rating
THANKS ALOT for this code!
2007-10-23, 21:44:59
Wewnon from Taiwan  
Thank for the code!
2007-12-26, 00:46:42
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
2007-12-26, 02:02:57
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
2008-03-10, 11:20:24
PerryJ from United States  
rating
Has anyone solved the issue with the PDF being in the winmail.dat format?
2008-03-12, 02:43:41
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.
2008-03-12, 02:56:35
julot@infomaniak.ch from Switzerland  
I got it ! Excellent - thanks a lot !
2008-06-17, 04:38:15
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......??
2008-07-02, 15:16:24
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

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: