0

Web application give error after sending few mails: System.Net.Mail.SmtpException: Failure sending mail.System.Net.WebException: The remote name could not be resolved When I send mail from my C#.Net code. My application hosted on cloud server and this error raise after some time, when I restart application pool it work again.

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;

        }

}

I have used smtp.gmail.com and port is 587 used.

0 Answers0