挂钩SMTP客户端事件

时间:2013-01-22 08:37:34

标签: c# smtp windows-server-2008-r2

简介:我支持一段代码,在成功注册网站后发送欢迎/确认邮件。

SMTP客户端在发送完成之前超时。根据我们的Ops部门,我使用正确的凭据和SMTP服务器IP地址。

我想要做的是挂钩任何握手,连接,发送等事件,以便我可以弄清楚过程超时的原因。我在该页面上将Server.ScriptTimeOut设置为500秒,因此它不仅仅是繁忙的服务器问题。

我的问题是:在此过程中我可以处理哪些事件。我在SMTPClient中看到的唯一一个是SendCompleted。但我想必须有办法获得更低级别的东西?

供参考,这是代码:

//Create and send email
MailMessage mm = new MailMessage();
try
{
    mm.From = new MailAddress("admin@site.com");
    mm.To.Add(new MailAddress(Recipient));
    mm.Subject = Subject;
    mm.Body = MailBody;
    mm.IsBodyHtml = true;

    var c = new NetworkCredential("admin@site.com", "YeOlePassword");

    SmtpClient smtp = new SmtpClient("127.0.0.1");//not really, just hiding our SMTP IP from StackOverflow

    smtp.UseDefaultCredentials = false;
    smtp.Credentials = c;
    smtp.Send(mm);

}
catch (Exception x)
{
    DateTime CurrentDateTime = DateTime.Now;
    String appDateTime = CurrentDateTime.ToString();

    String transactionLogEntry = "To: " + Recipient + " " + appDateTime + " " + x.Message;
    StreamWriter streamwriter = new StreamWriter(File.Open(MapPath("~/TransactionLog.txt"), FileMode.Append, FileAccess.Write, FileShare.Write));

    //Append to file
    streamwriter.WriteLine(transactionLogEntry);
    //Close file
    streamwriter.Close();
}

记录的错误只是在将邮件发送到提供的电子邮件地址时发生超时。

1 个答案:

答案 0 :(得分:1)

我使用它来获取隐藏的SMTP消息。

String transactionLogEntry = "To: " + Recipient + " " + appDateTime + " " + GetFullErrorMessage(x);

.
.
.

private string GetFullErrorMessage(Exception e)
    {
        string strErrorMessage = "";
        Exception excpCurrentException = e;

        while (excpCurrentException != null)
        {
            strErrorMessage += "\"" + excpCurrentException.Message + "\"";
            excpCurrentException = excpCurrentException.InnerException;
            if (excpCurrentException != null)
            {
                strErrorMessage += "->";
            }
        }

        strErrorMessage = strErrorMessage.Replace("\n", "");

        return strErrorMessage;
    }

根据我的经验,根据您所描述的内容,您的SMTP端口被ISP或您的服务器/ Web主机阻止。祝你好运!