Question

I am having a page, where everything works perfect! The issue or bug is that sometimes electricity runs out, I get a bug only then. The bug or exception is that the server cannot resolve the hostname:

The remote name could not be resolved: 'smtp.gmail.com'

I am using IIS 8 and I am sure that this isn't an IIS error neither is an ASP.NET issue.

What I have searched: I have searched alot on Google, and every answer was to do a try {} catch {} So I did. I have a try catch inside the block. Like this:

try {
    WebMail.Send(// i will not write the tags because they are fine!
    );
} 
catch (System.Net.WebException e) {
            error = e.Message;
}
Response.Write(error);

This doesnot catch the error perfectly! Am I having an issue while writing the try catch block, or am I not catching the error correctly.

And also to be noted, I am having a ajax request. Otherwise I would have used:

ModelState.AddError(e.Message);

But I cannot use that (I think I cannot). Can I get a better way, so that the emails are sent only when connected to the server otherwise show a popup or do nothing. But I am not able to do that! I get a 500 Internal Server Error. I can prevent that using a try catch. But how. I am not able to tackle this one.

Was it helpful?

Solution

The issue was System.Mail.SmtpException.

This exception was being thrown since the network was not able to connect or resolve the smtpserver (smtp.gmail.com)

Because of this, the email was not sent and there was still an error.

So I just used

try {
  // send the email
} catch (System.Mail.SmtpException e) {
  // error message
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top