ASP.. NET从VStudio发送电子邮件运行得很快,但在IIS中非常慢
本文关键字:但在 很快 IIS 非常 运行 NET VStudio 电子邮件 ASP | 更新日期: 2023-09-27 17:54:25
从ASP发送电子邮件。使用Visual Studio的。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;
}
在my 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>
在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()
方法。