System.Net.Mail.SmtpException:发送mail.System.Net.WebException失败:无法解析远程名称

时间:2014-11-06 09:35:49

标签: c# asp.net .net asp.net-mvc email

发送几封邮件后,Web应用程序会出错: System.Net.Mail.SmtpException:发送mail.System.Net.WebException失败:无法解析远程名称 当我从C#.Net代码发送邮件时。 我的应用程序托管在云服务器上,并且在一段时间后,当我重新启动应用程序池时,此错误会再次发生。

public void SendMail(string ToEmailID,string CCEmailID,string Subject,string Body,System.Collections.ArrayList _Attachments = null)             MailMessage mm = null;             SmtpClient smtp = null;

        try
        {
            string strPath = "";
            string strReplacepath = "";

            if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request != null)
            {
                strPath = System.Web.HttpContext.Current.Request.Url.ToString();
                strReplacepath = System.Web.HttpContext.Current.Request.Url.PathAndQuery;
                strPath = strPath.Replace(strReplacepath, "/");
            }
            else if (!string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["DomainName"]))
            {
                strPath = System.Configuration.ConfigurationManager.AppSettings["DomainName"];
            }

            Body = Body.Replace("@@LOGO@@", GetImage(strPath, "Content/images/logo.jpg"));
            Body = Body.ToString().Replace("@@Year@@", DateTime.Now.Year.ToString());

            mm = new MailMessage();
            mm.Subject = Subject;
            mm.Body = Body;
            mm.IsBodyHtml = true;
            mm.To.Add(ToEmailID);

            if (!string.IsNullOrEmpty(CCEmailID))
                mm.CC.Add(CCEmailID);

            mm.Bcc.Add("My Email Address");

            if (_Attachments != null && _Attachments.Count > 0)
            {
                for (int i = 0; i < _Attachments.Count; i++)
                {
                    mm.Attachments.Add((Attachment)_Attachments[i]);
                }
            }
            smtp = new SmtpClient();
            smtp.EnableSsl = true;
            smtp.Timeout = 25000;

            smtp.Send(mm);

        }
        finally
        {
            foreach (Attachment attachment in mm.Attachments)
            {
                attachment.Dispose();
            }

            if (_Attachments != null)
                _Attachments.Clear();

            if (mm != null)
                mm.Dispose();

            if (smtp != null)
                smtp.Dispose();

            mm = null;
            smtp = null;
            _Attachments = null;

        }

}

我使用过smtp.gmail.com,端口是587使用。

0 个答案:

没有答案