0

I have an ASP.NET application running on a Windows Server 2012 R2. I have configured Relay Server and it is working fine but my application can not send an email by using the following code. Error "Failure sending mail."

This code is working fine without a Windows Server:

protected void Unnamed_Click(object sender, EventArgs e)
{
        try
        {
                using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter hw = new HtmlTextWriter(sw))
                {
                    GridView1.AllowPaging = true;

                    GridView1.RenderControl(hw);
                    StringReader sr = new StringReader(sw.ToString());

                    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        PdfWriter.GetInstance(pdfDoc, memoryStream);
                        pdfDoc.Open();
                        htmlparser.Parse(sr);
                        pdfDoc.Close();
                        byte[] bytes = memoryStream.ToArray();
                        memoryStream.Close();

                        MailMessage mm = new MailMessage("emailid", "emialid");
                        mm.Subject = "GridView Email";
                        mm.Body = "GridView:<hr />" + sw.ToString();

                        mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridViewPDF.pdf"));

                        mm.IsBodyHtml = true;
                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = "smtp.office365.com";
                        smtp.EnableSsl = true;
                        System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                        NetworkCred.UserName = "emailid";
                        NetworkCred.Password = "password;
                        smtp.UseDefaultCredentials = true;
                        smtp.Credentials = NetworkCred;
                        smtp.Port = 587;
                        smtp.Send(mm);

                        string script = "<script type=\"text/javascript\">alert('Email Send Successfully');</script>";
                        ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
        }
}
  • What is the exception? – Martheen Mar 10 '22 at 05:40
  • I have received this error "Failure sending mail." while sending an email from windows server 2012 R2 – Sameer Ali Mar 10 '22 at 05:43
  • Could be the port is blocked, try https://stackoverflow.com/questions/14722556/using-curl-to-send-email and https://stackoverflow.com/questions/9507353/how-do-i-install-and-use-curl-on-windows to get a more complete error message – Martheen Mar 10 '22 at 07:58
  • I try to use smtp.Port = 25; after configuring relay on windows server but it can't work. even relay server works fine as I tested – Sameer Ali Mar 10 '22 at 08:07
  • Is this server owned by your company or rented from a third party? Check their policy on SMTP traffic. – Martheen Mar 10 '22 at 08:17
  • We own this server by my company and when I configure the relay server, after it, I generate a .txt file and place that entire folder so it sends an email which means the relay server works fine. But when I send it through asp.net so it generates an error. kindly help me how to resolve this issue – Sameer Ali Mar 10 '22 at 09:09

0 Answers0