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
- public static void getmessage()
- {
- try
- {
- List<string> ids = new List<string>();
- List<AE.Net.Mail.MailMessage> mails = new List<AE.Net.Mail.MailMessage>();
- List<Email> emaillist = new List<Email>();
- int count = 1;
- using (var ic = new AE.Net.Mail.ImapClient("imap.gmail.com", "[email protected]", "Abc", AE.Net.Mail.AuthMethods.Login, 993, true))
- {
- ic.SelectMailbox("INBOX");
- int y = ic.GetMessageCount();
- MailMessage[] mm = ic.GetMessages(0, y);
- var msgs = ic.SearchMessages(SearchCondition.Undeleted());
- for (int i =0; i < msgs.Length; i++)
- {
- if (count == 500)
- {
- break;
- }
- string msgId = msgs[y-1].Value.Uid;
- ids.Add(msgId);
- y--;
- count++;
- }
- foreach (string id in ids)
- {
- mails.Add(ic.GetMessage(id, headersonly: false));
- }
-
- foreach (var msg in mails)
- {
- Email email = new Email();
- Attachment attachment = new Attachment();
- email.Body= msg.Body.ToString();
- email.From = msg.From.ToString();
- email.DateSent = msg.Date;
- foreach (var att in msg.Attachments)
- {
- string fName;
- fName = att.Filename;
- string bo= att.Body;
- Attachment a = new Attachment();
- a.Content = fName;
- email.Attachments.Add(a);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- emaillist.Add(email);
- }
- var js = new JavaScriptSerializer();
- HttpContext.Current.Response.Write(js.Serialize(emaillist));
- }
- }
- catch (Exception exe)
- {
-
- }
- }
here is the sample Jquery code:
- $(document).ready(function () {
- debugger;
- $.ajax({
- url: "Reademail.aspx/getmessage",
- type: "POST",
- dataType: "json",
- contentType: "application/json; charset=utf-8",
- success: function (data)
- {
- debugger;
- var datatableVariable = $('#tblmsg').DataTable(
- {
- data: data,
- columns: [
- { 'data': 'From' },
- { 'data': 'Subject' },
- { 'data': 'DateSent' },
- { 'data': 'Body' }
- ]
- });
- },
- error:function(){alert('error');}
- });
- });