Capturing SMTP errors in .NET.

I usually sent emails as follows:

int statusCode = 0; string error = null; try { smtp.Send(mailMessage); statusCode = 250; } catch (SmtpFailedRecipientException e) { Log(String.Format("[Exception] {0}\n\tSmtp: {1}{2}:{3}\n\tStatus Code: {4}\n\tFaild Recipient: {5}", e.Message, smtp.Key, smtp.Value.Host, smtp.Value.Port, e.StatusCode, e.FailedRecipient)); statusCode = (int)e.StatusCode; error = e.Message; } catch (SmtpException e) { Log(String.Format("[Exception] {0}\n\tSmtp: {1} - {2}:{3}\n\tStatus Code: {4}", e.Message, smtp.Key, smtp.Value.Host, smtp.Value.Port, e.StatusCode)); statusCode = (int)e.StatusCode; error = e.Message; } catch (Exception e) { Log(String.Format("[Exception] {0}.\n\tSource: {1}\n\tStack Trace: {2}", e.Message, e.Source, e.StackTrace)); statusCode = -1; error = "General Failure"; } 

But this method does not allow me to catch some of the more “advanced” SMTP errors, such as “No domain”, “No such email”, etc.

How can I capture such SMTP errors? Is it possible?

For example, when I try to send Gmail to an address such as asdfkljhadf@dgdfasdf.com , Gmail sends me after a while an email that asdfkljhadf@dgdfasdf.com does not exist.

Thanks.

0
source share
2 answers

SMTP is the store and forward protocol, which means that the server can receive the message for forwarding, but not know when the message cannot be delivered. So, for example, when you tell your SMP server provider the message " invalidaddress@example.com ", the server response is "Good, I will forward it." After some time, the ISP SMTP server will contact the SMTP server for "example.com" and try to deliver the message. Only after that he knows that the mail is not available.

So, as for the connection between your client and the SMTP server, the message was delivered successfully - the server agreed to forward it. Therefore, no exception.

+5
source

I do not think that these errors are known to the SMTP server when sending emails. The Smtp server finds out about these errors when it receives a response from other servers, trying to find the domain / bad email address, etc.

If you use IIS, you may find such emails in a bad email folder in IIS.

+2
source

Source: https://habr.com/ru/post/1444619/


All Articles