Exemplo n.º 1
0
        protected void Page_Command(object sender, CommandEventArgs e)
        {
            try
            {
                sbTrace = new StringBuilder();
                if (e.CommandName == "Mailbox.CheckBounce")
                {
                    EmailUtils.CheckInbound(Application, gID, true);
                }
                if (e.CommandName == "Mailbox.CheckMail" || e.CommandName == "Mailbox.CheckBounce")
                {
                    string sSERVER_URL     = Sql.ToString(ViewState["SERVER_URL"]);
                    string sEMAIL_USER     = Sql.ToString(ViewState["EMAIL_USER"]);
                    string sEMAIL_PASSWORD = Sql.ToString(ViewState["EMAIL_PASSWORD"]);
                    int    nPORT           = Sql.ToInteger(ViewState["PORT"]);
                    string sSERVICE        = Sql.ToString(ViewState["SERVICE"]);
                    bool   bMAILBOX_SSL    = Sql.ToBoolean(ViewState["MAILBOX_SSL"]);

                    // 01/08/2008 Paul.  Decrypt at the last minute to ensure that an unencrypted password is never sent to the browser.
                    Guid gINBOUND_EMAIL_KEY = Sql.ToGuid(Application["CONFIG.InboundEmailKey"]);
                    Guid gINBOUND_EMAIL_IV  = Sql.ToGuid(Application["CONFIG.InboundEmailIV"]);
                    sEMAIL_PASSWORD = Security.DecryptPassword(sEMAIL_PASSWORD, gINBOUND_EMAIL_KEY, gINBOUND_EMAIL_IV);

                    dtMain = new DataTable();
                    dtMain.Columns.Add("From", typeof(System.String));
                    dtMain.Columns.Add("Sender", typeof(System.String));
                    dtMain.Columns.Add("ReplyTo", typeof(System.String));
                    dtMain.Columns.Add("To", typeof(System.String));
                    dtMain.Columns.Add("CC", typeof(System.String));
                    dtMain.Columns.Add("Bcc", typeof(System.String));
                    dtMain.Columns.Add("Subject", typeof(System.String));
                    dtMain.Columns.Add("DeliveryDate", typeof(System.DateTime));
                    dtMain.Columns.Add("Priority", typeof(System.String));
                    dtMain.Columns.Add("Size", typeof(System.Int32));
                    dtMain.Columns.Add("ContentID", typeof(System.String));
                    dtMain.Columns.Add("MessageID", typeof(System.String));
                    dtMain.Columns.Add("Headers", typeof(System.String));
                    dtMain.Columns.Add("Body", typeof(System.String));

                    Pop3.Pop3MimeClient pop = new Pop3.Pop3MimeClient(sSERVER_URL, nPORT, bMAILBOX_SSL, sEMAIL_USER, sEMAIL_PASSWORD);
                    try
                    {
                        pop.Trace      += new Pop3.TraceHandler(this.Pop3Trace);
                        pop.ReadTimeout = 60 * 1000;                         //give pop server 60 seconds to answer
                        pop.Connect();

                        int nTotalEmails = 0;
                        int mailboxSize  = 0;
                        pop.GetMailboxStats(out nTotalEmails, out mailboxSize);

                        List <int> arrEmailIds = new List <int>();
                        pop.GetEmailIdList(out arrEmailIds);
                        foreach (int i in arrEmailIds)
                        {
                            int nEmailSize = pop.GetEmailSize(i);
                            if (nEmailSize < 1 * 1024 * 1024)
                            {
                                Pop3.RxMailMessage mm = null;
#if DEBUG
                                pop.IsCollectRawEmail = true;
#endif
                                pop.GetHeaders(i, out mm);
                                if (mm == null)
                                {
                                    sbTrace.Append("Email " + i.ToString() + " cannot be displayed." + ControlChars.CrLf);
                                }
                                else
                                {
                                    DataRow row = dtMain.NewRow();
                                    dtMain.Rows.Add(row);
                                    row["From"]    = Server.HtmlEncode(Sql.ToString(mm.From));
                                    row["Sender"]  = Server.HtmlEncode(Sql.ToString(mm.Sender));
                                    row["ReplyTo"] = Server.HtmlEncode(Sql.ToString(mm.ReplyTo));
                                    row["To"]      = Server.HtmlEncode(Sql.ToString(mm.To));
                                    row["CC"]      = Server.HtmlEncode(Sql.ToString(mm.CC));
                                    row["Bcc"]     = Server.HtmlEncode(Sql.ToString(mm.Bcc));
                                    row["Subject"] = Server.HtmlEncode(mm.Subject);
                                    // 01/23/2008 Paul.  DateTime in the email is in universal time.
                                    row["DeliveryDate"] = T10n.FromUniversalTime(mm.DeliveryDate);
                                    row["Priority"]     = mm.Priority.ToString();
                                    row["Size"]         = nEmailSize;
                                    row["ContentId"]    = mm.ContentId;
                                    row["MessageId"]    = mm.MessageId;
                                    row["Headers"]      = "<pre>" + Server.HtmlEncode(mm.RawContent) + "</pre>";
                                    //row["Body"        ] = mm.Body        ;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
                        lblError.Text = ex.Message;
                    }
                    finally
                    {
                        pop.Disconnect();
                    }
                    ViewState["Inbox"] = dtMain;
                    vwMain             = new DataView(dtMain);
                    grdMain.DataSource = vwMain;
                    grdMain.DataBind();
                }
            }
            catch (Exception ex)
            {
                SplendidError.SystemError(new StackTrace(true).GetFrame(0), ex);
                lblError.Text = ex.Message;
            }
            finally
            {
#if DEBUG
                RegisterClientScriptBlock("Pop3Trace", "<script type=\"text/javascript\">sDebugSQL += '" + Sql.EscapeJavaScript(sbTrace.ToString()) + "';</script>");
#endif
            }
        }