【问题标题】:Hooking into SMTP client events连接到 SMTP 客户端事件
【发布时间】:2013-01-22 08:37:33
【问题描述】:

简介:我支持在成功注册到网站时发送欢迎/确认邮件的代码。

发送完成前 SMTP 客户端超时。根据我们的运营部门的说法,我使用了正确的凭据和 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();
}

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

【问题讨论】:

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


    【解决方案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 或您的服务器/网络主机阻止有关。祝你好运!

    【讨论】:

    • 哈哈。具有讽刺意味的是,我为上述 ISP/Host 工作。我并不是说这不是问题...
    • 这是一个方便的脚本。您可能对阻塞的端口是正确的,所以我接受您的回答,因为没有人愿意冒险甚至猜测。必须保持良好的声誉等等。
    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多