// GET: MailBox
        public ActionResult Index()
        {
            DashBoardMailBoxJob model = new DashBoardMailBoxJob();

            model      = ReceiveMails();
            model.data = "";
            return(View(model));
        }
        private DashBoardMailBoxJob ReceiveMails()
        {
            DashBoardMailBoxJob model = new DashBoardMailBoxJob();

            model.Inbox = new List <MailMessege>();

            try
            {
                EmailConfiguration email = new EmailConfiguration();
                email.POPServer    = "imap.gmail.com"; // type your popserver
                email.POPUsername  = "";               // type your username credential
                email.POPpassword  = "";               // type your password credential
                email.IncomingPort = "993";
                email.IsPOPssl     = true;


                int        success = 0;
                int        fail    = 0;
                ImapClient ic      = new ImapClient(email.POPServer, email.POPUsername, email.POPpassword, AuthMethods.Login, Convert.ToInt32(email.IncomingPort), (bool)email.IsPOPssl);
                // Select a mailbox. Case-insensitive
                ic.SelectMailbox("INBOX");
                int i        = 1;
                int msgcount = ic.GetMessageCount("INBOX");
                int end      = msgcount - 1;
                int start    = msgcount - 40;
                // Note that you must specify that headersonly = false
                // when using GetMesssages().
                MailMessage[] mm = ic.GetMessages(start, end, false);
                foreach (var item in mm)
                {
                    MailMessege obj = new MailMessege();
                    try
                    {
                        obj.UID      = item.Uid;
                        obj.subject  = item.Subject;
                        obj.sender   = item.From.ToString();
                        obj.sendDate = item.Date;
                        if (item.Attachments == null)
                        {
                        }
                        else
                        {
                            obj.Attachments = item.Attachments;
                        }

                        model.Inbox.Add(obj);
                        success++;
                    }
                    catch (Exception e)
                    {
                        DefaultLogger.Log.LogError(
                            "TestForm: Message fetching failed: " + e.Message + "\r\n" +
                            "Stack trace:\r\n" +
                            e.StackTrace);
                        fail++;
                    }
                    i++;
                }
                ic.Dispose();
                model.Inbox = model.Inbox.OrderByDescending(m => m.sendDate).ToList();
                model.mess  = "Mail received!\nSuccesses: " + success + "\nFailed: " + fail + "\nMessage fetching done";

                if (fail > 0)
                {
                    model.mess = "Since some of the emails were not parsed correctly (exceptions were thrown)\r\n" +
                                 "please consider sending your log file to the developer for fixing.\r\n" +
                                 "If you are able to include any extra information, please do so.";
                }
            }

            catch (Exception e)
            {
                model.mess = "Error occurred retrieving mail. " + e.Message;
            }
            finally
            {
            }
            return(model);
        }
예제 #3
0
    protected void Page_Load(object sender, EventArgs e)
    {
        int page = -1;

        if (Request.QueryString["page"] == null)
        {
            Response.Redirect("ImapClientProtocolAll.aspx?page=1");
            Response.Flush();
            Response.End();
        }
        else
        {
            page = Convert.ToInt32(Request.QueryString["page"]);
        }
        try
        {
            Email    = Session["email"].ToString();
            Password = Session["pwd"].ToString();

            int totalEmails;
            //List<EMail> emails;
            string             emailAddress;
            EmailConfiguration email = new EmailConfiguration();
            email.IMAPServer   = "imap.gmail.com"; // type your popserver
            email.IMAPUsername = Email;            // type your username credential
            email.IMAPpassword = Password;         // type your password credential
            email.IncomingPort = "993";
            email.IsIMAPssl    = true;
            int success = 0;
            //int fail = 0;


            DashBoardMailBoxJob model = new DashBoardMailBoxJob();
            ImapClient          ic    = new ImapClient(email.IMAPServer, email.IMAPUsername, email.IMAPpassword, AuthMethods.Login, Convert.ToInt32(email.IncomingPort), (bool)email.IsIMAPssl);
            ic.SelectMailbox("INBOX");
            uids = ic.Search("ALL");
            int co = uids.Count();
            totalEmails = ic.GetMessageCount();

            emailAddress = Email;


            int totalPages;
            int mod = (co) % NoOfEmailsPerPage;
            if (mod == 0)
            {
                totalPages = (co) / NoOfEmailsPerPage;
            }
            else
            {
                totalPages = ((co - mod) / NoOfEmailsPerPage) + 1;
            }

            int i = 0;
            foreach (var uid in uids)
            {
                MailMessege obj     = new MailMessege();
                var         message = ic.GetMessage(uid);

                obj.sender   = message.From.ToString();
                obj.sendDate = message.Date;
                obj.subject  = message.Subject;
                obj.UID      = message.Uid; //UID is unique identifier assigned to each message by mail server
                if (message.Attachments == null)
                {
                }
                else
                {
                    obj.Attachments = message.Attachments;
                }
                //model.Inbox.Add(obj);
                success++;
                int       emailId = ((page - 1) * NoOfEmailsPerPage) + i + 1;
                TableCell noCell  = new TableCell();
                noCell.CssClass = "emails-table-cell";
                noCell.Text     = Convert.ToString(emailId);
                TableCell fromCell = new TableCell();
                fromCell.CssClass = "emails-table-cell";
                fromCell.Text     = obj.sender;
                TableCell subjectCell = new TableCell();
                subjectCell.CssClass       = "emails-table-cell";
                subjectCell.Style["width"] = "300px";
                subjectCell.Text           = String.Format(DisplayEmailLink, emailId, obj.subject);
                TableCell dateCell = new TableCell();
                dateCell.CssClass = "emails-table-cell";
                if (obj.sendDate != DateTime.MinValue)
                {
                    dateCell.Text = obj.sendDate.ToString();
                }
                TableRow emailRow = new TableRow();
                emailRow.Cells.Add(noCell);
                emailRow.Cells.Add(fromCell);
                emailRow.Cells.Add(subjectCell);
                emailRow.Cells.Add(dateCell);
                EmailsTable.Rows.AddAt(2 + i, emailRow);
                i = i + 1;
            }

            if (totalPages > 1)
            {
                if (page > 1)
                {
                    PreviousPageLiteral.Text = String.Format(SelfLink, page - 1, "Previous Page");
                }
                if (page > 0 && page < totalPages)
                {
                    NextPageLiteral.Text = String.Format(SelfLink, page + 1, "Next Page");
                }
            }
            EmailFromLiteral.Text  = Convert.ToString(((page - 1) * NoOfEmailsPerPage) + 1);
            EmailToLiteral.Text    = Convert.ToString(page * NoOfEmailsPerPage);
            EmailTotalLiteral.Text = Convert.ToString(totalEmails);
            EmailLiteral.Text      = emailAddress;
        }

        catch (Exception ex)
        {
            Response.Redirect("Login.aspx");
        }
    }