1

It's working fine in my local and in the Test Server. But upon deploying this in Production server.

I'm getting the Internal Server Error 500 with this function.

All the function works fine except for this one.

Function to call the sending of email in the view:

function ProcessSendingEmail(NextMRB, to, originator, owner, probDesc, hold_day, MRB_Type) { $.ajax({ type: "POST", url: '@Url.Action("SendMailToFutureHold", "MRB")', data: { MRBNumber: NextMRB, MailTo: to, Originator: originator, Owner: owner, ProbDesc: probDesc, hold_days: hold_day, mrb_type: MRB_Type}, success: function (data) { console.log("Email Sent"); }, error: function (data) { console.log("Email Not Sent"); } }); } 

Controller

[HttpPost] public ActionResult SendMailToFutureHold(string MRBNumber, string MailTo, string Originator, string Owner, string ProbDesc, string hold_days, string mrb_type) { MailId = Originator + "@domain.com"; DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type); byte[] bytes; //dt.Columns.RemoveAt(dt.Columns.Count - 1); dt.TableName = "MRB_Future_Hold_List_" + MRBNumber; Response.ClearContent(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename = MRB_Future_Hold_List_" + MRBNumber + ".csv"); Response.ContentType = "text/csv"; Response.Charset = ""; using (StringWriter sw = new StringWriter()) { int iColCount = dt.Columns.Count; int iRowcount = dt.Rows.Count; if (iRowcount != 0) { for (int i = 0; i < iColCount; i++) { sw.Write(dt.Columns[i]); if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); foreach (DataRow dr in dt.Rows) { for (int i = 0; i < iColCount; i++) { if (!Convert.IsDBNull(dr[i])) { sw.Write(dr[i].ToString()); } if (i < iColCount - 1) { sw.Write(","); } } sw.Write(sw.NewLine); } } else { sw.Write("Something went wrong. Please contact [email protected]"); } using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { using (MemoryStream mStream = new MemoryStream(Encoding.ASCII.GetBytes(sw.ToString()))) { string messageBody = "<b>Originator :</b> " + Originator.ToUpper() + "<br> <b>Owner :</b> " + Owner.ToUpper() + "<br> <b>Problem Description :</b> <br>" + ProbDesc.ToUpper() + "<br><br><font> Attached file are the list of lots to put on MRB Hold. </font><br><br>"; bytes = mStream.ToArray(); Mail_Service sndmail = new Mail_Service("mailrelay.domain.com", "[email protected]", MailTo, QA_Contact_Email + "," + MailId + ",[email protected]"); sndmail.Send_Mail("LOTS FOR MRB FUTURE HOLD", messageBody, bytes, MRBNumber); } Response.Flush(); Response.End(); } } return View(); } 

If anyone can help me. It is much appreciated. Thanks.

Error Result

5
  • You will need to add logging to the server-side to narrow down where the issue is. Commented Jul 21, 2020 at 6:42
  • It is quite weird to flush the response then return View();. Commented Jul 21, 2020 at 6:44
  • 1
    Which line is line 375? Commented Jul 21, 2020 at 6:45
  • The actual return was this. return Json(new { success = true, responseText = "Email Sent to : " + MailTo }, JsonRequestBehavior.AllowGet); Commented Jul 21, 2020 at 7:00
  • @NiñoAngeloReyes You claim it returned json but your stacktrace says otherwise, you got an unhandled exception at server. Is the mail server reachable in production environment? Commented Jul 21, 2020 at 9:41

2 Answers 2

1

The issue is not with your Javascript function but with your controller method SendMailToFutureHold.

From stack-error we can read null reference exception object not set to an instance of an object

Assuming that DataTable dt = MRB.GetDataForAttachmentForFH(MRBNumber, MailTo, mrb_type); is correctly initialised( not null) -

Response might not be initialised.

The only way to be sure is to do some logging - that is the only way - I am aware - of debugging Production code.

You can try log4net

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alex. log4Net can define which line hit the error. I finally fix the internal error 500.
@NiñoAngeloReyes glad it has helped - its always a good idea to put some logging in - will save you long hours of headaches - please dont forget to upvote - in order to help others
1

Basically this issue not related with you front-end code. It's coming from the back-end method which you consuming in Ajax call.

I Would say, use Try and Catch in the .cs method and log the error in catch. Because 500 internal server error coming due to runtime error in your code.

Create you own Logger class, you can implement the Log4net in your project and write the log in your server. In this way you can get the answer what reason causing the 500 Internal Server error in your Ajax Response

[HttpPost] public ActionResult SendMailToFutureHold(string MRBNumber, string MailTo, string Originator, string Owner, string ProbDesc, string hold_days, string mrb_type) { Try { Your code just paste it here return View(); } catch(Exception ex) { //Log the exception here with following things:- Stack Trace, Error } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.