TMachna
TMachna

Reputation: 289

Check if email was sent -- Delphi Indy10

My app sends emails to the given addresses and this works fine. Problem is, that one of these email addresses could be incorrect and not exist. I am using Delphi 2009 and Indy10 components (TIdSMTP and TIdMessage).

Try
  Try
    SMTP.Connect;
    SMTP.Send(MailMessage);
    resultTmp := ?????
 Except
   on E : Exception do begin
     ShowMessage(E.Message);
   end;
 End;
Finally
  if SMTP.Connected then begin
    SMTP.Disconnect;
  end;
End

How can I check the if an email was sent or not ? On my test Gmail account I got emails like "Address not found" if I try to send email to incorrect address.

Thanks

Upvotes: 2

Views: 2444

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 596317

When you send an email to an SMTP server, all you are really doing is given the email to the server, and it reports back whether it will TRY to deliver it to the specified recipient(s). There is no guarantee of delivery.

IF a given recipient is local to that server/domain, the receiving SMTP server can report immediately (in reply to the SMTP RCPT TO command) whether it will deliver the email to that recipient or not. TIdSMTP can report such errors to your code, via the TIdSMTP.OnFailedRecipient event, or by raising an exception.

However, IF the email cannot be delivered to a recipient immediately, it has to be delivered asynchronously, usually by being relayed to another server (potentially many times), and that delivery does not happen until after you have given the email to the initial server and moved on.

As such, errors that occur during that delivery cannot be reported by TIdSMTP. The SMTP server that fails to deliver the email (which may be the same server you connected to, or may be another server that the email was relayed to) will send an email back to the original sender's inbox explaining the error(s) that occurred.

So, to detect those kind of errors, you have to check your inbox periodically, such as with POP3 or IMAP, and then parse the emails for details. And such errors may take some time to arrive, depending on the nature of the error(s), how long a given server retries before giving up, etc. So, it may take minutes, hours, or even days to get a response back, if at all.

In general, there is no single reliable way to know if a given recipient is valid in a timely manner. You should just let SMTP do its work and handle errors asynchronously.

However, if you must verify, one way would be to use TIdDNSResolver to lookup the MX record(s) of a recipient's domain, then connect TIdSMTP to that domain server(s) and call TIdSMTP.Verify() to check if the recipient exists on those server(s). But this requires the server(s) to implement the SMTP VRFY command, which is optional by the SMTP protocol spec, and may not be be implemented by modern servers to avoid leaking knowledge, and plug vulnerability holes.

Upvotes: 6

Haifisch
Haifisch

Reputation: 909

I'm not sure you can know if a adress exist, because in gmail you sent an email and google server answer you that it's didn't exist.

With Send method you can get error return by your server SMTP, like "adress not valid", "socket error", .. but nothing about the recipient.

The email you use to sent message will get a reply if recipient's SMTP detect an error.

In my code I use EIdSMTPReplyError too.

try
  SMTP.Send(MailMessage);
except
  on E : EIdSMTPReplyError do
  begin
    ShowMessage(E.ErrorCode.ToString + ' : ' + E.Message);
  end;  

  on E : Exception do
  begin
    ShowMessage(E.Message);
  end
end;

Upvotes: -2

Related Questions