0

I am sending email from within the web application using NetMail and it works fine when there is nothing wrong! I am trying to catch some of the errors, specifically wrong email address that should cause an exception but it seems it doesn't. My email address is fisrstname.dot.middleinitial.lastname@domain_name. I don't include middle initial on purpose to cause an error but when I trace the code it goes through. This is what I have:

SmtpClient sc = new SmtpClient(<Mail_relay>);
sc.Credentials = new NetworkCredential();

try
{
    sc.Send(mm);
}
catch (SmtpFailedRecipientsException FRE)  
{                          
    foreach (SmtpFailedRecipientException smtpFailedRecipientException in FRE.InnerExceptions)  
    {  
        // Get the email that is causing the exception  
        string sFailedRecipient = smtpFailedRecipientException.FailedRecipient;

        // Get the status code  
        SmtpStatusCode sc = FRE.StatusCode;  
    }  
}  
catch (SmtpException smtpEx)
{
    SmtpStatusCode sc = smtpEx.StatusCode;
}
catch (Exception generalEx)
{
    string sMessage = generalEx.Message;
}

I thought bad email address would be caught in the first catch block but it is not, it just goes right through all catch blocks.

dmarietta
  • 1,940
  • 3
  • 25
  • 32
NoBullMan
  • 2,032
  • 5
  • 40
  • 93

2 Answers2

0

user these links for reference

  1. Validating an email Address in C#

  2. Regex Email validation

there are two option

  1. you can create regex and match the mail address and if it doesn't match then you can throw an excption.

  2. or you can create mailaddress object and put it into try block and use FormatException which is in build in c#

Community
  • 1
  • 1
  • I am not concerned about validating email format but just sending an email to wrong email address that has correct format. – NoBullMan Jun 06 '13 at 15:55
0

Basically this is due to how SMTP works. What you have to realize is that when using SMTP as the message transport, the catch section of your code is typically only going to fire if the server that your machine is trying to hand off the message to does not accept the message and returns a valid status, or there is a communication error while sending. Depending on what machine you are sending from (meaning where it lives in your netowkr organization, etc), this may be all you can reliably count on.

The key is that the server your machine is attempting to hand off the message to may not be the server which contains the inbox to which the message should be delivered; or the destination SMTP server does not validate it during communication (yes, that is possible). In the latter case, the message is bounced asynchronously from your initial communication attempt. This is because there are many mail servers which simply accept incoming messages as quickly as possible with pretty minimal validation and queue them for processing on a separate thread.

dmarietta
  • 1,940
  • 3
  • 25
  • 32
  • So there is no way for me to check if the email has actually gone through or not? We store unsent notifications in a database table and an application runs every hour and check for unsent messages and re-sends them. But if the message was not initially sent becuase of wrong email address, there is no point storing it; this is what I am trying to catch and prevent the notification from being stored. – NoBullMan Jun 06 '13 at 15:59
  • What use is "SmtpFailedRecipientsException" if it cannot be caught? – NoBullMan Jun 06 '13 at 16:29
  • If the underlying transport protocol is SMTP, no there is no way to know if the message really was sent successfully. The functionality you see in the framework is there to support other message transport protocols where you can know this (i.e. think of a direct Exchange server to Exchange server message that does NOT use SMTP as the transport). This is a limitation of how SMTP works and just because you see methods and features in the .NET framework does not mean they apply to any/every message protocol. The framework is built to support more than just SMTP. – dmarietta Jun 10 '13 at 14:17
  • OK, thanks for the info. The API's name, "SmtpFailedRecipientsException", gave me the impression I can catch all SMTP failed recipient exceptions. – NoBullMan Jun 11 '13 at 13:57