加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ASP.NET从VStudio发送电子邮件在IIS中运行速度很快但速度

发布时间:2020-12-15 21:46:07 所属栏目:百科 来源:网络整理
导读:使用Visual Studio从我的ASP.NET项目发送电子邮件非常快 – 它只需一秒钟 – 但是当在同一台机器上的IIS 7中发布时,它需要50秒或更长时间.以前有没有人遇到这种速度的降低?我已经在web.config中粘贴了我的C#代码和我的设置.非常感谢你. public static bool
使用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("rn","<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()方法即可.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读