-1

I am continuously getting this error failure sending mail. I have already tried by changing port 465/25/587 but nothing worked. My credentials are also correct. All these configurations are for outlook. Checked all the ports they are also not blocked. The same code was working fine before but from last couple of days getting this error. Unable to get what's happening in this code.

Below is my code-

 protected void btnSubmitClick(object sender, EventArgs e)
    {
         try
        {
            if (ddlSubCategory.SelectedIndex > 0)
            {
                if (Session["sessionID"] != null && Session["sessionID"].ToString().ToUpper().Trim() == "SUBMITTED")
                {
                   // string textForMessage = @"<script language='javascript'> alert('Request Already Submitted for the same...!');</script>";
                   // ClientScript.RegisterClientScriptBlock(this.GetType(), "Stop", textForMessage); return;
                }
                if (fupAttachment.HasFile) { ViewState["file1"] = "0"; }
                if (ViewState["file1"].ToString() == "0")
                {
                    IssuesCommon issueCommon = new IssuesCommon();
                    issueCommon.IssueCateId = Convert.ToInt32(ddlSubCategory.SelectedValue);
                    issueCommon.Status = "OPEN";
                    issueCommon.LogBy = issueCommon.EmpId = Convert.ToInt32(getEmpDetail().Split('$')[0]);
                    issueCommon.Description = txtDesc.Text.Trim();
                    issueCommon.WorkStation = txtWorkStation.Text.Trim();
                    issueCommon.IPAddress = hfIP.Value;
                    issueCommon.Contact = txtphn.Text.Trim();
                    issueCommon.DepartmentId = Convert.ToInt32(Request.QueryString["help"]);
                    issueCommon.Supervisor = hfSuperId.Value;
                    string ticket = lblDeskName.Text + "#00" + Convert.ToString(Convert.ToInt32(objBal.getMaxIssueID()) + 1);
                    issueCommon.TicketNo = ticket;
                    issueCommon.Priority = (ddlPriority.SelectedValue == "Low" ? ddlSubPriority.SelectedValue : ddlPriority.SelectedValue);
                    issueCommon.attachmentName = fupAttachment.HasFile ? Server.MapPath("~/image/") + fupAttachment.FileName : null;

                    if (trSelectClient.Visible == true)
                    {
                        issueCommon.clientName = ddlClientList.SelectedValue.ToString() == "--Select--" ? "" : ddlClientList.SelectedValue.ToString();
                    }
                    else
                    {
                        issueCommon.clientName = "";
                    }

                    objBal.InsertIssue(issueCommon);

                     #region Mail Section
                    MailMessage mM = new MailMessage();
                    mM.From = new MailAddress("no-reply@xxxxxxx.com");
                    mM.To.Add(getDapartmentEmailId());
                    
                    if (txtAddRecipient.Text != "" && txtAddRecipient.Text != null && trAddRecipient.Visible == true)
                    {
                        mM.CC.Add(txtAddRecipient.Text);
                    }
                    if (Session["help"] != null)
                    {
                        if (Convert.ToString(Session["help"]) == "1")
                        {
                            mM.Bcc.Add("abc@xxxxxx.com");
                        }                  
                    }
                    if (fupAttachment.HasFile)
                    {
                        string filename = fupAttachment.FileName;
                        fupAttachment.SaveAs(Server.MapPath("~/images/") + filename);
                        Attachment attach = new Attachment(Server.MapPath("~/images/") + filename);
                        mM.Attachments.Add(attach);
                    }
                    mM.Subject = "Issue Ticket#: " + issueCommon.TicketNo + " [Priority: " + issueCommon.Priority + "]";
                    string mailBody = "Hello," +
                      "<br/> I am having the below issue: <br/><br> Issue Category : " + ddlSubCategory.SelectedItem.Text
                       + " <br/> <br/> Workstation: " + txtWorkStation.Text + " <br/><br/> IP Address : " + txtIP.Text
                       + "<br /><br /> Client Name: " + ddlClientList.SelectedValue.ToString().Replace("--Select--", "N/A")
                        + "<br /><br /> My ContactNo: " + txtphn.Text
                       + "<br/><br/> Description : " + txtDesc.Text.Trim() + "<br/><br/> Thanks, <br/>" + hfEmpid.Value + "/" + username.Text;
                    mM.Body = mailBody;
                    mM.IsBodyHtml = true;
                    mM.Priority = MailPriority.High;
                    SmtpClient sC = new SmtpClient("smtp.office365.com");
                    sC.Port = 587;
                    sC.EnableSsl = true;
                    sC.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                    sC.Credentials = new NetworkCredential("email", "password");               
                    sC.Send(mM); 
                   SendMailtoUser(issueCommon.TicketNo, lblUserName.Text.Trim());
                    Session["sessionID"] = "Submitted"; ViewState["file1"] = "0"; clearView();
                    Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Your request has been submitted successfully');", true);
                    #endregion
                }
                else
                {
                    string textForMessage = @"<script language='javascript'> alert('Select Attachment ');</script>";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "UserPopup", textForMessage);
                }
            }
            else { Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Select Issue Category');", true); }
        }
        catch (Exception ex) { 
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('" + ex.Message.Replace("'", "") + "');", true); }
    }

Please see the exception screenshot here enter image description here

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Pl see this: https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-connection-an-existing-connection-was-f – Zeeshanef Jan 05 '22 at 09:24

2 Answers2

0

if you try this i think sending mail will be successful

var smtpClient = new SmtpClient(hostname, 25);
// userNameShort = "ab", domain = "outlook" for example
var networkCredential = new NetworkCredential(userNameShort, password, domain);
smtpClient.Credentials = networkCredential;
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
smtpClient.EnableSsl = true;
smtpClient.Send(message);
Serdar GUL
  • 47
  • 2
  • 5
0

Please can you trying:

SmtpClient sC = new SmtpClient("smtp.office365.com");

after adding

sC.Credentials = new NetworkCredential("email", "password");  

then

 sC.Port = 587;
//your codes