title: How can a Delphi Program send an Email with Attachments via the DEFAULT E-mail Client?
body: <p>Within my program, I am composing an email to send using the default e-mail client software installed on a user's machine. </p>
  
  <p>I have composed the mailto address, the subject, the multilined body, and I have several attachments to be included.</p>
  
  <p>I almost got this working using mailto and ShellExecute as follows:</p>
  
  <pre><code>  Message := 'mailto:someone@somewhere.com'
      + '?subject=This is the subjectBehold Error Report'
      + '&amp;body=This is line 1' + '%0D%0A'
      + 'This is line 2' + '%0D%0A'
      + 'This is line 3'
      + '&amp;Attach=c:\file1.txt';
    RetVal := ShellExecute(Handle, 'open', PChar(Message), nil, nil, SW_SHOWNORMAL);
    if RetVal &lt;= 32 then
      MessageDlg('Cannot find program to send e-mail.', mtWarning, [mbOK], 0);
  </code></pre>
  
  <p>Using Delphi 2009 on a Windows Vista machine, this will open a Microsoft Mail "Create Mail" window, with the To, Subject and Body filled correctly. However the file does not get attached.</p>
  
  <p>As I researched this, I noticed some commentary stating that this technique does not work with all mail clients. However, most of the commentary is fairly old, as I realize this is a very old technique.</p>
  
  <p>Then I found that <a href="http://delphi.about.com/od/indy/a/email-send-indy.htm" rel="noreferrer">Zarko Gajic said</a> that "this approach is ok, but you are unable to send attachments in this way".</p>
  
  <p>I have seen theres also the Windows Simple Mail API (MAPI), but Zarko says that only works if the end-user has MAPI-compliant email software. There are well documented techniques on using MAPI with Delphi (e.g. <a href="http://kurapaty.blogspot.com/2007/08/sending-email-using-mapi.html" rel="noreferrer">Sending e-mail using mapi</a>), but they all have the disclaimer that MAPI is not always installed with Windows.</p>
  
  <p>Besides, I really want the message to come up first in the user's default email program, so they will have it as part of their email records and they can edit it and decide if and when they want to send it. I'm not sure how MAPI works and if it will do that.</p>
  
  <p>So my requirements are:</p>
  
  <ol>
  <li><p>To bring the email up in the user's mail program.</p></li>
  <li><p>To allow one or more attachments.</p></li>
  <li><p>To work with (hopefully) all email clients on any Windows machine from XP up (i.e. XP, Vista or 7).</p></li>
  </ol>
  
  <p>Is there such an animal?  Or maybe does someone know how to get attachments to work with the mailto/ShellExecute technique?</p>
  
  <p>What do most people do?</p>
  
  <hr>
  
  <p>Edit: </p>
  
  <p>There have been a few answers with MAPI solutions and even an Indy solution.</p>
  
  <p>The problem I have with them is that they don't necessarily use the default mail client. On my Vista machine, for example, I have set up Windows Mail as my default client. When I do a MAPI send, it does not bring up Windows Mail, but it brings up and sets up the email in Outlook instead. I don't want that. </p>
  
  <p>Two of my users of my program complained:</p>
  
  <blockquote>
    <p>Your debug routine fails to send the file, as it tries to start windows mail for some reason known to it's self rather than using the default mail client (in my case thunderbird)</p>
    
    <p>I tried to fill up the exception
    report but gave up when it asked for
    this  server, that server! I then got
    really annoyed because it launched
    Outlook 
    - I never, ever use it or want to use it.</p>
  </blockquote>
  
  <p>I don't need code for MAPI or Indy. They are readily available. But if you suggest MAPI or Indy, what I really need is a way to find the default client and ensure that it is the one that is passed the email to be sent.</p>
  
  <p>Also, I need to know if MAPI is now universal. 5 years ago, it wasn't guaranteed to work on all machines because it wasn't installed as part of the operating system. Is that still true, or does MAPI now come with Windows XP, Vista and 7 by default?</p>
  
  <p>Same questions go for Indy or any other suggested solutions. Can it work with the default client and will it work on almost all Windows XP and later machines?</p>
  
  <p>The reason why the "mailto" solution is so nice, is that all machines have to support it for the purpose of handling the HTML mailto statement found on webpages. Now if only I could use it to add attachments ...</p>
  
  <hr>
  
  <p>Likely solution found: mjustin pointed out an alternative that makes use of the Operating System's sendto command. That most likely is the way to go. </p>
  
  <p>The mailto was not limited to 256 characters like the HTML mailto is, but I was devastated to find out it ended up being limited to 2048 characters. Fortunately a few hours later, mjustin gave his answer. </p>
  
  <p>If implementation of that goes okay, his answer will have done it for me. If not, I'll add my comments here.</p>
  
  <hr>
  
  <p>No. As it turns out, the sendto solution will not always open the default email program. On my machine, it opens Outlook when my default mailer is Windows Mail. Too bad. I've had to go back to the mailto method, despite the 2048 character limit.</p>
  
  <p>I did, however, find in the article: <a href="http://www.codeproject.com/KB/shell/sendtomail.aspx" rel="noreferrer">SendTo mail recipient</a> that:</p>
  
  <blockquote>
    <p>At this point, you could replace
    ::ShellExecute with a well thought
    ::WinExec call, using the actual
    mailto command line declared in the
    registry and target the current e-mail
    client (for instance,
    "%ProgramFiles%\Outlook
    Express\msimn.exe" /mailurl:%1). But
    then the limitation is 32 KB. As a
    conclusion, there is no way to send
    e-mails larger than 32KB using the
    mailto protocol.</p>
  </blockquote>
  
  <p>but then I'd have to determine who the mail client is in each case. I expect that would lead to further complications.</p>
  
  <p>The one other thing I found out is that mailto allows setting of "to", "cc", "bcc", "subject" and "body" but no attachments. Whereas sendto ONLY allows attachments and then sets up a default email with a default message and no way for you to set the various fields and body.</p>