[DUG] Open new email in default browser

Robert Martin rob at chreos.co.nz
Thu Jul 28 16:10:36 NZST 2016


Hi Joylon

No problem !

I usually use Indy to send emails but in this case I wanted to use the 
default email client.  I HAVE found some MAPI code that seems to work!  
I am working through it now.  Third time lucky :)

Code below for others (in its cut and paste format) ...

procedure ArtMAPISendMail(
             const Subject, MessageText, MailFromName, MailFromAddress,
                   MailToName, MailToAddress: String;
             const AttachmentFileNames: array of String);
//Originally by Brian Long: The Delphi Magazine issue 60 - Delphi And Email
var
   MAPIError: DWord;
   MapiMessage: TMapiMessage;
   Originator, Recipient: TMapiRecipDesc;
   Files, FilesTmp: PMapiFileDesc;
   FilesCount: Integer;
begin
    FillChar(MapiMessage, Sizeof(TMapiMessage), 0);

    MapiMessage.lpszSubject := PAnsiChar(AnsiString(Subject));
    MapiMessage.lpszNoteText := PAnsiChar(AnsiString(MessageText));

    FillChar(Originator, Sizeof(TMapiRecipDesc), 0);

    Originator.lpszName := PAnsiChar(AnsiString(MailFromName));
    Originator.lpszAddress := PAnsiChar(AnsiString(MailFromAddress));
//   MapiMessage.lpOriginator := @Originator;
    MapiMessage.lpOriginator := nil;


    MapiMessage.nRecipCount := 1;
    FillChar(Recipient, Sizeof(TMapiRecipDesc), 0);
    Recipient.ulRecipClass := MAPI_TO;
    Recipient.lpszName := PAnsiChar(AnsiString(MailToName));
    Recipient.lpszAddress := PAnsiChar(AnsiString(MailToAddress));
    MapiMessage.lpRecips := @Recipient;

    MapiMessage.nFileCount := High(AttachmentFileNames) - 
Low(AttachmentFileNames) + 1;
    Files := AllocMem(SizeOf(TMapiFileDesc) * MapiMessage.nFileCount);
    MapiMessage.lpFiles := Files;
    FilesTmp := Files;
    for FilesCount := Low(AttachmentFileNames) to 
High(AttachmentFileNames) do
    begin
      FilesTmp.nPosition := $FFFFFFFF;
      FilesTmp.lpszPathName := 
PAnsiChar(AnsiString(AttachmentFileNames[FilesCount]));
      Inc(FilesTmp)
    end;

    try
      MAPIError := MapiSendMail(
        0,
        Application.MainForm.Handle,
        MapiMessage,
        {MAPI_LOGON_UI or }{MAPI_NEW_SESSION} MAPI_DIALOG,
        0);
    finally
      FreeMem(Files)
    end;

    case MAPIError of
      MAPI_E_AMBIGUOUS_RECIPIENT:
       Showmessage('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:
       Showmessage('The specified attachment was not found; no message 
was sent.');
      MAPI_E_ATTACHMENT_OPEN_FAILURE:
       Showmessage('The specified attachment could not be opened; no 
message was sent.');
      MAPI_E_BAD_RECIPTYPE:
       Showmessage('The type of a recipient was not MAPI_TO, MAPI_CC, or 
MAPI_BCC. No message was sent.');
      MAPI_E_FAILURE:
       Showmessage('One or more unspecified errors occurred; no message 
was sent.');
      MAPI_E_INSUFFICIENT_MEMORY:
       Showmessage('There was insufficient memory to proceed. No message 
was sent.');
      MAPI_E_LOGIN_FAILURE:
       Showmessage('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:
       Showmessage('The text in the message was too large to sent; the 
message was not sent.');
      MAPI_E_TOO_MANY_FILES:
       Showmessage('There were too many file attachments; no message was 
sent.');
      MAPI_E_TOO_MANY_RECIPIENTS:
       Showmessage('There were too many recipients; no message was sent.');
      MAPI_E_UNKNOWN_RECIPIENT:
        Showmessage('A recipient did not appear in the address list; no 
message was sent.');
      MAPI_E_USER_ABORT:
        Showmessage('The user canceled the process; no message was sent.');
      SUCCESS_SUCCESS:
        Showmessage('MAPISendMail successfully sent the message.');
    else
      Showmessage('MAPISendMail failed with an unknown error code.');
    end;
end;


On 28/07/2016 4:00 PM, Jolyon Direnko-Smith wrote:
> Ignore my previous response - it was just re-stating what you already 
> learned.  Sorry 'bout that.
>
> As for alternatives, is there any reason you must use the default 
> email client and cannot simply construct and send an email using 
> (e.g.) Indy SMTP or IMAP components, suitably configured ?
>
>
>
> On 28 July 2016 at 15:58, Jolyon Direnko-Smith <jsmith at deltics.co.nz 
> <mailto:jsmith at deltics.co.nz>> wrote:
>
>     The mailto URI scheme does not support attachments.
>
>     https://tools.ietf.org/html/rfc6068
>
>     Specific email clients may provide extensions to the URI scheme
>     that allow that email client to support attachments via a mailto:
>     URI but by definition these extensions will not be universally
>     supported and so fail the test of "working with the user's default
>     email client (whatever that may be)".
>
>     On 28 July 2016 at 15:33, Robert Martin <rob at chreos.co.nz
>     <mailto:rob at chreos.co.nz>> wrote:
>
>         Hi All
>
>         I am trying to open the default system email client with a new
>         email
>         (subject and attachment).  I know about MailTo but that
>         doesn't support
>         file attachments.  I have tried a couple of MAPI functions I
>         got off the
>         web but to no avail (one errors the other just runs and does
>         nothing).
>         Does anyone have code to do this?
>
>
>         Cheers
>
>         Rob
>
>
>         _______________________________________________
>         NZ Borland Developers Group - Delphi mailing list
>         Post: delphi at listserver.123.net.nz
>         <mailto:delphi at listserver.123.net.nz>
>         Admin: http://delphi.org.nz/mailman/listinfo/delphi
>         Unsubscribe: send an email to
>         delphi-request at listserver.123.net.nz
>         <mailto:delphi-request at listserver.123.net.nz> with Subject:
>         unsubscribe
>
>
>
>
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at listserver.123.net.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at listserver.123.net.nz with Subject: unsubscribe
>
>
> No virus found in this message.
> Checked by AVG - www.avg.com <http://www.avg.com>
> Version: 2016.0.7690 / Virus Database: 4627/12695 - Release Date: 07/27/16
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20160728/7829162c/attachment.html 


More information about the Delphi mailing list