Trying to send mail through SMTP. No mail arrives and no exception errors

Problem. I made a small mail program that works fine on my developer's computer, but when it starts, it fails.

protected void Page_Load(object sender, EventArgs e) { string smtpHost = ConfigurationManager.AppSettings["SmtpAddress"]; MailMessage mail = new MailMessage(); mail.From = new MailAddress(ConfigurationManager.AppSettings["FromMailAddress"]); mail.Sender = new MailAddress(ConfigurationManager.AppSettings["FromMailAddress"]); mail.To.Add(new MailAddress(" zzz@xxx.yy ")); mail.Subject = "Test mail"; mail.Body = string.Format("Is this mail sent via {0} ?", smtpHost); lblMsg2.Text = string.Format("SmtpHost: {0}", smtpHost); ; SmtpClient client = new SmtpClient(smtpHost); try { client.Send(mail); } catch (Exception exception) { lblMsg3.Text = exception.Message.ToString(); lblMsg4.Text = exception.InnerException.ToString(); } } 

I get the correct mailing address and everything on the production server, but nothing ends in my inbox :-(. I do not get any exceptions.

I tried using telnet on the same server, and from there I can send emails.

I tried installing WireShark, which looks at the network card on the server, and when using telnet I get an answer that I suspected, but I get nothing from my program.

Edited 2012-03-12 @ 09: 46 Danish time

Now we updated the code to look like this:

 protected void Page_Load(object sender, EventArgs e) { string smtpHost = ConfigurationManager.AppSettings["SmtpAddress"]; MailMessage mail = new MailMessage(); mail.From = new MailAddress(ConfigurationManager.AppSettings["FromMailAddress"]); mail.Sender = new MailAddress(ConfigurationManager.AppSettings["FromMailAddress"]); mail.To.Add(new MailAddress(" zzz@xxx.yy ")); mail.Subject = "Test mail"; mail.Body = string.Format("Is this mail sent via {0} ?", smtpHost); lblMsg2.Text = string.Format("SmtpHost: {0}", smtpHost); ; SmtpClient client = new SmtpClient(smtpHost); client.Send(mail); } 

And quite interesting: even when inserting an SMTP server that definitely does not exist, I still don't get any errors in my production environment. I get an exception on my developer's computer, which basically means that the smtp server does not exist (which I also expected to receive a message about).

+4
source share
3 answers

Case resolved :-)

And I learned more about IIS!

IIS was configured to "Save email to the distribution directory" instead of "Deliver email to the SMTP server." Unfortunately, I am not so good at IIS, and I did not know that there was such a situation ... I am now!

The โ€œdouble check of configuration settingsโ€ from @GarryM, unfortunately, was not enough for me or my mail administrator to make us take a look at IIS. I am too much programmer, and my mail admin is too much network guy :-)

Thank you all for your answers. This was partly due to all your good hints and ideas that I could leave more and more details.

+4
source

Double check your mail server configuration. If he does not throw an exception, the problem is most likely that the mail has been accepted, but it does not relay it or treat it as spam.

Some hosting providers also quietly block some of the major free email hosts due to spam problems if you use a shared hosting provider, which is also worth asking.

Also, you tried to run the same code locally, but pointing to the direct mail host? (Giving you access to it from the outside) This way you can go through the code and make sure that the exception is not thrown.

+1
source

This will help if you remove try / catch, as this will throw any exception.
Another thing: client.EnableSSL , if you use a secure connection, which is required by many mail servers.

0
source

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


All Articles