【问题标题】:ASP.NET Send email from VStudio is running fast but very slow in IISASP.NET 从 VStudio 发送电子邮件运行速度很快,但在 IIS 中速度很慢
【发布时间】:2013-01-02 04:06:33
【问题描述】:

使用 Visual Studio 从我的 ASP.NET 项目发送电子邮件非常快——只需一秒钟——但在同一台机器上的 IIS 7 中发布时,需要 50 秒或更长时间。有没有人遇到过这种速度降低?我已将 C# 代码和我的设置粘贴到 web.config 中。非常感谢。

public static bool EnviarMail(String eOrigen, String eDestino, String asunto, String cueMensaje)
    {
        Boolean EstadoEnvio;
        MailMessage eMail = new MailMessage();
        eMail.From = new MailAddress(eOrigen);
        eMail.To.Add(new MailAddress(eDestino));
        eMail.Subject = asunto;
        eMail.IsBodyHtml = true;
        cueMensaje = cueMensaje.Replace("\r\n", "<BR>");
        eMail.Body = cueMensaje;
        eMail.Priority = MailPriority.Normal;

        SmtpClient clienteSMTP = new SmtpClient();
        try
        {   
            clienteSMTP.Send(eMail);
            EstadoEnvio = true;
        }
        catch 
        {
            EstadoEnvio = false;
        }
        return EstadoEnvio;            
    }

在我的 web.config 中:

    <mailSettings>
        <smtp from="iso@hmoore.com.ar">
            <network host="174.120.190.6" port="25" userName="iso@hmoore.com.ar" password="-----" defaultCredentials="true"/>
        </smtp>
    </mailSettings>

【问题讨论】:

  • 如果将defaultCredentials 设置为false 会发生什么?
  • 您是否尝试过在 IIS 中使用与您的 Visual Studio 实例相同的用户运行应用程序池?
  • 你是如何测量你的 50 秒的?
  • @ken2k 你好,我使用的是完全相同的用户,我也是计算机管理员。谢谢
  • 嗨@mathieu,我在调用该方法之前捕获了时间,然后在您运行该方法时再次捕获。

标签: c# asp.net performance email iis


【解决方案1】:

在您的 ASP.NET 应用程序中发送电子邮件时,有时您不希望用户体验因为等待电子邮件发送而变慢。下面的代码示例是如何异步发送 System.Net.Mail.MailMessage 以便当前线程可以在辅助线程发送电子邮件时继续。

public static void SendEmail(System.Net.Mail.MailMessage m)
{
    SendEmail(m, true);
}



public static void SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
{
    System.Net.Mail.SmtpClient smtpClient = null;
    smtpClient = new System.Net.Mail.SmtpClient("localhost");    
    if (Async)
    {
        SendEmailDelegate sd = new SendEmailDelegate(smtpClient.Send);
        AsyncCallback cb = new AsyncCallback(SendEmailResponse);
        sd.BeginInvoke(m, cb, sd);
    }
    else
    {
        smtpClient.Send(m);
    }
}

private delegate void SendEmailDelegate(System.Net.Mail.MailMessage m);
private static void SendEmailResponse(IAsyncResult ar)
{
    SendEmailDelegate sd = (SendEmailDelegate)(ar.AsyncState);

    sd.EndInvoke(ar);
}

要使用它,只需使用 System.Net.Mail.MailMessage 对象调用 SendEmail() 方法。

【讨论】:

  • 感谢@Aftab Ahmed 经过几次战斗之后发生的事情是我的团队网络设置不佳。当我在 win2008 服务器上发布该项目时,工作完美无延迟。谢谢大家。
猜你喜欢
  • 2013-02-27
  • 2021-05-02
  • 1970-01-01
  • 2015-11-20
  • 2019-11-18
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
相关资源
最近更新 更多