[英]ASP.NET Send email from VStudio is running fast but very slow in IIS
使用Visual Studio从我的ASP.NET项目发送电子邮件非常快 - 它只需一秒钟 - 但是当在同一台机器上的IIS 7中发布时,它需要50秒或更长时间。 以前有没有人遇到这种速度的降低? 我已经在web.config中粘贴了我的C#代码和我的设置。 非常感谢你。
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>
在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()
方法即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.