Problem: Have made a small mail program which works perfectly on my developer pc but when put into production 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 do get the correct mail address and everything on the production server, but nothing ends in my email inbox :-(. I do not receive any exceptions.
I have tried using telnet on the same server and from there I am able to send mails.
I have tried installing WireShark which is looking at the network card on the server and when using telnet I do get the reponse I suspected, but from my program I do not receive anything.
Edited 2012-03-12 @ 09:46 danish time
Have now 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 does definately not exist, I still do not get any errors on my production environment. I do get an exception on my developer pc which basically means that the smtp-server does not exist (which I also expected to get a message about).
Double-check the configuration of your mail server. If it isn't throwing an exception then the issue is most likely that the mail has been accepted, but it's not relaying it or potentially treating it as spam.
Some hosting providers also silently block some of the larger free mail hosts due to spam issues, if you are using a shared hosting provider it's worth asking them too.
Also have you tried running the same code locally but pointing at the live mail host? (Providing you can access it externally) That way you can step-through the code and ensure that an exception isn't getting thrown.
Case solved :-)
And I learned more about IIS!
The IIS was set up to "Store e-mail in Pickup directory" instead of "Deliver e-mail to SMTP server". Unfortunately I am not that good at IIS and I did not know that there was a setting like that... I am now!
The "double check you configuration settings" from @GarryM was unfortunately not enough for me or my mail-administrator to make us look at the IIS. I am too much a programmer and my mail-adm is too much a network guy :-)
Thank you everyone for your answers. It was partly because of all your good hints and ideas that I could leave out more and more parts.
It would help if you removed the try/catch as this will allow any exception to propagate up.
Another thing to try is
client.EnableSSL
if you are using a secure connection, which many mail servers require.