Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8519
  • Last Modified:

MAPISendMail giving MAPI_E_FAILURE on Outlook

We have a general mail interface programmed in Delphi that calls MAPISendMail. This works fine on all machines except one which consistently returns MAPI_E_FAILURE. This particular machine is running Windows XP and has Outlook 2003 as the default mail server. Other machines with the same setup run fine. If the default mail server is changed to Outlook Express then this machine also works fine. I have written a simple test program that just calls MAPISendMail as follows:


This gives MAPI_E_FAILURE trying to load Outlook on the above machine but loads a blank email dialog on all other machines. I've also tried MAPILogon first and the logon works but MAPISendMail still fails with the same error. Any suggestions what might be causing this problem ?
2 Solutions
Could this be of any use?

Try the following, It works on Outlook as well as any other email client like Outlook express

function TfmMain.sendMail(const TargetAddr,SenderAddr,MsgSubject, MsgContent:String; Attachment, Attachname:Tstringlist; PreviewMsg : Boolean = TRUE ) : String;
TAttachAccessArray = array [0..0] of TMapiFileDesc;
PAttachAccessArray = ^TAttachAccessArray;
  msg       : TMapiMessage;
  mrdTarget : TMapiRecipDesc;
  liFlags   : Longint;
  Attachments: PAttachAccessArray;
  MAPIError: DWord;
 result := '';
 liFlags := 0;
 fillChar( msg, sizeOf( msg ), 0 );
 with msg do
      mrdTarget.lpszName := pChar( TargetAddr );
      mrdTarget.ulRecipClass := MAPI_TO;
      mrdTarget.lpszAddress := pChar('SMTP:'+TargetAddr );
      mrdTarget.ulReserved := 0;
      mrdTarget.ulEIDSize := 0;
      mrdTarget.lpEntryID := NIL;
      nRecipCount := 1;
      lpRecips := @mrdTarget;

      mrdSender.lpszName := pChar('');
      mrdSender.ulRecipClass := MAPI_ORIG;
      mrdSender.lpszAddress := pChar('');
      //mrdSender.lpszAddress := pChar( SenderAddr );
      mrdSender.ulReserved := 0;
     mrdSender.ulEIDSize := 0;
      mrdSender.lpEntryID := NIL;
      lpOriginator := @mrdSender;

   if MsgSubject = '' then lpszSubject := ''  else lpszSubject := pChar( MsgSubject );
   if MsgContent = '' then  lpszNoteText := 'Please see the attached file.' else lpszNoteText := pChar(MsgContent);
   if Attachment.Count > 0 then
      GetMem(Attachments, SizeOf(TMapiFileDesc) * Attachment.Count);
      for i := 0 to Attachment.Count - 1 do
        Attachments[i].ulReserved := 0;
        Attachments[i].flFlags := 0;
        Attachments[i].nPosition := ULONG($FFFFFFFF);
        Attachments[i].lpszPathName := StrNew(PChar(Attachment[i]));
        Attachments[i].lpszFileName := StrNew(PChar(ExtractFileName(Attachname[i])));
        Attachments[i].lpFileType := nil;
      msg.nFileCount := Attachment.Count;
      msg.lpFiles := @Attachments^;
    if previewMsg then liFlags := MAPI_DIALOG ;
     MAPIError := mapiSendMail( 0, Application.Handle, msg, liFlags, 0 );

    case MAPIError of
          MAPI_E_AMBIGUOUS_RECIPIENT    :Result:='A recipient matched more than one of the recipients. No message was sent.';
          MAPI_E_ATTACHMENT_NOT_FOUND   :Result:='The specified attachment was not found; no message was sent.';
          MAPI_E_ATTACHMENT_OPEN_FAILURE:Result:='The specified attachment could not be opened; no message was sent.';
          MAPI_E_BAD_RECIPTYPE          :Result:='The email address seems incorrect. No message was sent.';
          MAPI_E_FAILURE                :Result:='One or more unspecified errors occurred; no message was sent.';
          MAPI_E_INSUFFICIENT_MEMORY    :Result:='There was insufficient memory to proceed. No message was sent.';
          MAPI_E_DISK_FULL              :Result:='There was insufficient memory to proceed. No message was sent.';
          MAPI_E_LOGIN_FAILURE          :Result:='There was no default logon in the mail application, and the user failed to log on successfully when the logon dialog box was displayed. No message was sent.';
          MAPI_E_TEXT_TOO_LARGE         :Result:='The text in the message was too large to sent; the message was not sent.';
          MAPI_E_TOO_MANY_FILES         :Result:='There were too many file attachments; no message was sent.';
          MAPI_E_TOO_MANY_RECIPIENTS    :Result:='There were too many recipients; no message was sent.';
          MAPI_E_UNKNOWN_RECIPIENT      :Result:='A recipient did not appear in the address list; no message was sent.';
          MAPI_E_USER_ABORT             :Result:='The user canceled the process; no message was sent.';
          MAPI_E_TYPE_NOT_SUPPORTED     :Result:='Type not supported; no message was sent.';
          MAPI_E_ACCESS_DENIED          :Result:='Access Denied. No message was sent.';

          MAPI_E_ATTACHMENT_WRITE_FAILURE   :Result:='Unable to write attachment. No message was sent.';
          MAPI_E_INVALID_EDITFIELDS         :Result:='Invalid edit field. No message was sent.';
          MAPI_E_INVALID_MESSAGE            :Result:='Message is invalid. No message was sent.';
          MAPI_E_INVALID_RECIPS             :Result:='Receipients are invalid. No message was sent.';
          MAPI_E_INVALID_SESSION            :Result:='Session error. No message was sent.';
          MAPI_E_MESSAGE_IN_USE             :Result:='Message is in use. No message was sent.';
          MAPI_E_NETWORK_FAILURE            :Result:='Network error. No message was sent.';
          MAPI_E_NO_MESSAGES                :Result:='There are no messages. No message was sent.';
          MAPI_E_NOT_SUPPORTED              :Result:='Unsupported error. No message was sent.';
          MAPI_E_TOO_MANY_SESSIONS          :Result:='Too may sessions are open. No message was sent.';
          SUCCESS_SUCCESS                   :Result:='';
          Result:='Email failed with an unknown error code.';

Forced accept.

EE Admin

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now