I need to be able to send an email from my app. I am using this code:

I need to be able to send an email from my app. I am using this code:
function SendEmail(const Handle: HWND; const Address, Subject, AttachmentName: string): Integer;
var
  anAddress, anAttachment, aSubject, Message: string;
begin
  anAddress := IfThen( Pos( 'mailto:', Address ) > 0, '', 'mailto:' ) + Address;
  aSubject := IfThen( Pos( '?subject=', Subject ) > 0, '', '?subject=' ) + Subject;
  anAttachment := IfThen( Pos( '&Attach=', AttachmentName ) > 0, '', '&Attach=' ) + AttachmentName;
  Message := anAddress + aSubject + anAttachment;
  Result := ShellExecute( Handle, 'open', PChar( Message ), nil, nil, SW_SHOWNORMAL );
  //  if Result <= 32 then
  //    MessageDlg('Cannot find program to send e-mail.', mtWarning, [mbOK], 0);
end;

Sometimes it just works, but when it fails to return from ShellExecute, then I try to reset, and I am getting this error from XE:
"Debugger fatal error during process reset: 'Not connected to remote host'. Please save your work and restart Embarcadero RAD Studio XE."

Googling shows me there was a report of such an error on D2010, and that a particular hotfix handled the problem. I have found nothing on it with respect to DXE.

So the question, really, is why it does not return from ShellExecute. Any suggestions?

Comments

  1. Kevin McCoy Well, as the server which would be used is subject to the sysadmins of the state level, presumably they could eliminate the relay issues.

    Yes, the app-level setup would be simple. The e-mail address would be just another persistent user setting to be saved to  the server, as these settings attach to the login, not the machine.

    Thanks  for your assistance, and for your patience.

    ReplyDelete
  2. If it is an "owned server" then you can almost certainly use the user's email address as the From: address. Replies to your automated messages would go back to the teacher's email account.

    ReplyDelete
  3. OK, round two. My client wants me to use the JCL stuff. Also, the users are all using Outlook, so the problem is simpler. Or not. ;) I have a class I found on StackOverflow, (kind of large to embed here), and it seems to work fine.
    http://stackoverflow.com/questions/1962765/how-can-a-delphi-program-send-an-email-with-attachments-via-the-default-e-mail-c

    See the Mapi.SendMail unit, well down the page.

    However, if I am debugging, and Outlook is running, I get a MAPI General Failure error (2). However, if Outlook is not running, then it apparently just caches it to send when Outlook is next run. Nice. Clear as mud.

    So my immediate question is, is there some simple and reliable way to discover that Outlook is running before I try to send? It would be nice, to say the least, to let the user know that if Outlook is not running, his email is simply sitting in the outbox....

    ReplyDelete

Post a Comment