public ActionResult Index(int currentPageNumber, int nextPageNumber)
        {
            int startSerialNumber = ((nextPageNumber - 1) * 30) + 1;
            int endSerialNumber = ((nextPageNumber) * 30);

            List<Email> emails = new List<Email>();

            try
            {

                userGmailConfig = FetchUserGmailProfile();
                using (ImapClient client = new ImapClient())
                {
                    client.Connect(userGmailConfig.IncomingServerAddress, userGmailConfig.IncomingServerPort, true);
                    client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword));

                    var inbox = client.Inbox;
                    inbox.Open(FolderAccess.ReadOnly);

                    if (inbox.Count > 0)
                    {
                        int pageEndIndex = Math.Max(inbox.Count - startSerialNumber, 0);
                        int pageStartIndex = Math.Max(inbox.Count - endSerialNumber, 0);

                        var messages = inbox.Fetch(pageStartIndex, pageEndIndex, MessageSummaryItems.Envelope);

                        messages = messages.OrderByDescending(message => message.Envelope.Date.Value.DateTime).ToList();

                        foreach (var message in messages)
                        {
                            if (startSerialNumber <= endSerialNumber)
                            {
                                Email tempEmail = new Email()
                                {
                                    SerialNo = startSerialNumber,
                                    Uid = message.UniqueId,
                                    FromDisplayName = message.Envelope.From.First().Name,
                                    FromEmail = message.Envelope.From.First().ToString(),
                                    To = message.Envelope.To.ToString(),
                                    Subject = message.NormalizedSubject,
                                    TimeReceived = message.Envelope.Date.Value.DateTime,
                                    HasAttachment = message.Attachments.Count() > 0 ? true : false
                                };
                                emails.Add(tempEmail);
                                startSerialNumber++;
                            }
                        }
                    }
                }

                ViewBag.EmailId = userGmailConfig.GmailUsername;
                ViewBag.NoOfEmails = endSerialNumber;
                if (currentPageNumber > nextPageNumber)
                {
                    ViewBag.PageNumber = currentPageNumber - 1;
                }
                else
                {
                    ViewBag.PageNumber = currentPageNumber + 1;
                }
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = "There was an error in processing your request. Exception: " + ex.Message;
            }

            return View(emails);
        }
        public JsonResult Compose(Email email)
        {
            JsonResult jsonResult = new JsonResult();
            string outputMessage = "";
            try
            {

                userGmailConfig = FetchUserGmailProfile();

                var message = new MimeMessage();

                message.From.Add(new MailboxAddress(email.FromEmail, email.FromEmail));

                if (email.ToAsCsv.Contains(','))
                {
                    foreach (var item in email.ToAsCsv.Split(','))
                    {
                        message.To.Add(new MailboxAddress(item, item));
                    }
                }
                else if (email.ToAsCsv.Contains(';'))
                {
                    foreach (var item in email.ToAsCsv.Split(';'))
                    {
                        message.To.Add(new MailboxAddress(item, item));
                    }
                }
                else
                {
                    message.To.Add(new MailboxAddress(email.ToAsCsv, email.ToAsCsv));
                }
                message.Subject = email.Subject;
                message.Body = new TextPart("plain")
                {
                    Text = email.Body
                };

                using (var client = new SmtpClient())
                {
                    try
                    {
                        client.Connect(userGmailConfig.OutgoingServerAddress, userGmailConfig.OutgoingServerPort);
                        client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword));

                        client.Send(message);
                        client.Disconnect(true);

                        outputMessage = "Your message was sent successfully";
                    }
                    catch (Exception)
                    {
                        outputMessage = "There was an error sending your mail.";
                    }
                }
            }
            catch (Exception ex)
            {
                outputMessage = "There was an error in processing your request. Exception: " + ex.Message;
            }

            jsonResult.Data = new
            {
                message = outputMessage,
            };
            return jsonResult;
        }
        public ActionResult Index()
        {
            long noOfEmails = 1;
            UniqueId lastUid = new UniqueId();
            List<Email> emails = new List<Email>();

            try
            {
                userGmailConfig = FetchUserGmailProfile();

                using (ImapClient client = new ImapClient())
                {
                    client.Connect(userGmailConfig.IncomingServerAddress, userGmailConfig.IncomingServerPort, true);
                    client.Authenticate(new NetworkCredential(userGmailConfig.GmailUsername, userGmailConfig.GmailPassword));

                    var inbox = client.Inbox;
                    inbox.Open(FolderAccess.ReadOnly);

                    if (inbox.Count > 0)
                    {
                        int index = Math.Max(inbox.Count - 30, 0);

                        var uids = (from c in inbox.Fetch(index, -1, MessageSummaryItems.UniqueId)
                                    select c.UniqueId).ToList();

                        var messages = inbox.Fetch(uids, MessageSummaryItems.Envelope);

                        messages = messages.OrderByDescending(message => message.Envelope.Date.Value.DateTime).ToList();

                        foreach (var message in messages)
                        {
                            Email tempEmail = new Email()
                            {
                                SerialNo = noOfEmails,
                                Uid = message.UniqueId,
                                FromDisplayName = message.Envelope.From.First().Name,
                                FromEmail = message.Envelope.From.First().ToString(),
                                To = message.Envelope.To.ToString(),
                                Subject = message.NormalizedSubject,
                                TimeReceived = message.Envelope.Date.Value.DateTime,
                                HasAttachment = message.Attachments.Count() > 0 ? true : false
                            };
                            lastUid = tempEmail.Uid;
                            emails.Add(tempEmail);
                            noOfEmails++;
                        }
                    }
                }

                ViewBag.EmailId = userGmailConfig.GmailUsername;
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = "There was an error in processing your request. Exception: " + ex.Message;
            }
            ViewBag.NoOfEmails = 30;
            ViewBag.PageNumber = 1;

            return View(emails);
        }