harry code

harry code

  • NA
  • 124
  • 21.5k

Ajax function always calling error function

Mar 1 2019 7:35 AM
Hi,
 
I am trying to read messages from gmail and display them in juery datatable.
 
Ajax web method is calling error method.
 
here is the .cs code
  1. public static void getmessage()  
  2. {  
  3. try  
  4. {  
  5. List<string> ids = new List<string>();  
  6. List<AE.Net.Mail.MailMessage> mails = new List<AE.Net.Mail.MailMessage>();  
  7. List<Email> emaillist = new List<Email>();  
  8. int count = 1;  
  9. using (var ic = new AE.Net.Mail.ImapClient("imap.gmail.com""[email protected]""Abc", AE.Net.Mail.AuthMethods.Login, 993, true))  
  10. {  
  11. ic.SelectMailbox("INBOX");  
  12. int y = ic.GetMessageCount();  
  13. MailMessage[] mm = ic.GetMessages(0, y);  
  14. var msgs = ic.SearchMessages(SearchCondition.Undeleted());  
  15. for (int i =0; i < msgs.Length; i++)  
  16. {  
  17. if (count == 500)  
  18. {  
  19. break;  
  20. }  
  21. string msgId = msgs[y-1].Value.Uid;  
  22. ids.Add(msgId);  
  23. y--;  
  24. count++;  
  25. }  
  26. foreach (string id in ids)  
  27. {  
  28. mails.Add(ic.GetMessage(id, headersonly: false));  
  29. }  
  30. //DataTable dt = new DataTable();  
  31. foreach (var msg in mails)  
  32. {  
  33. Email email = new Email();  
  34. Attachment attachment = new Attachment();  
  35. email.Body= msg.Body.ToString();  
  36. email.From = msg.From.ToString();  
  37. email.DateSent = msg.Date;  
  38. foreach (var att in msg.Attachments)  
  39. {  
  40. string fName;  
  41. fName = att.Filename;  
  42. string bo= att.Body;  
  43. Attachment a = new Attachment();  
  44. a.Content = fName;  
  45. email.Attachments.Add(a);  
  46. }  
  47. //dt.Columns.Add("From", typeof(string));  
  48. //dt.Columns.Add("Subject", typeof(string));  
  49. //dt.Columns.Add("Date", typeof(DateTime));  
  50. //dt.Columns.Add("Body", typeof(string));  
  51. //DataRow dr1 = dt.NewRow();  
  52. //dr1 = dt.NewRow();  
  53. //dr1["From"] = email.From.ToString();  
  54. //dr1["Subject"] = email.Subject.ToString();  
  55. //dr1["Date"] = email.DateSent;  
  56. //dr1["Body"] = email.Body;  
  57. //dt.Rows.Add(dr1);  
  58. //DataSet ds = new DataSet();  
  59. emaillist.Add(email);  
  60. }  
  61. var js = new JavaScriptSerializer();  
  62. HttpContext.Current.Response.Write(js.Serialize(emaillist));  
  63. }  
  64. }  
  65. catch (Exception exe)  
  66. {  
  67. //MessageBox.Show("Check your network connection");  
  68. }  
  69. }  
here is the sample Jquery code:
  1. $(document).ready(function () {  
  2. debugger;  
  3. $.ajax({  
  4. url: "Reademail.aspx/getmessage",  
  5. type: "POST",  
  6. dataType: "json",  
  7. contentType: "application/json; charset=utf-8",  
  8. success: function (data)  
  9. {  
  10. debugger;  
  11. var datatableVariable = $('#tblmsg').DataTable(  
  12. {  
  13. data: data,  
  14. columns: [  
  15. 'data''From' },  
  16. 'data''Subject' },  
  17. 'data''DateSent' },  
  18. 'data''Body' }  
  19. ]  
  20. });  
  21. },  
  22. error:function(){alert('error');}  
  23. });  
  24. });  

Answers (7)