尝试通过 SMTP 发送邮件.没有邮件到达,也没有异常错误

问题描述:

问题:制作了一个小邮件程序,它在我的开发人员电脑上运行良好,但在投入生产时却失败了.

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.

我尝试在同一台服务器上使用 telnet,然后我就可以发送邮件了.

I have tried using telnet on the same server and from there I am able to send mails.

我已经尝试安装 WireShark,它正在查看服务器上的网卡,并且在使用 telnet 时我确实得到了我怀疑的响应,但是从我的程序中我没有收到任何东西.

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.

编辑于 2012-03-12 @ 09:46 丹麦时间

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); 
} 

而且很有趣:即使插入一个绝对不存在的 SMTP 服务器,我的生产环境仍然没有出现任何错误.我确实在我的开发人员电脑上遇到了一个异常,这基本上意味着 smtp-server 不存在(我也希望收到一条消息).

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).

案例已解决 :-)

而且我学到了更多关于 IIS 的知识!

And I learned more about IIS!

IIS 设置为将电子邮件存储在分拣目录中",而不是将电子邮件发送到 SMTP 服务器".不幸的是,我不太擅长 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!

不幸的是,@GarryM 的仔细检查您的配置设置"不足以让我或我的邮件管理员让我们查看 IIS.我太像程序员了,我的邮件管理员太像网络人了 :-)

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.