Esempio n. 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            int emailId = -1;

            if (Request.QueryString["emailId"] == null)
            {
                Response.Redirect("Pop3Client.aspx");
                Response.Flush();
                Response.End();
            }
            else
            {
                Email = Session["email"].ToString();
            }
            Password = Session["pwd"].ToString();
            emailId  = Convert.ToInt32(Request.QueryString["emailId"]);
            Email email = null;
            List <MessagePart> msgParts = null;
            Pop3Client         client   = new Pop3Client(Host, Port, Email, Password, true);

            client.Connect();
            email    = client.FetchEmail(emailId);
            msgParts = client.FetchMessageParts(emailId);


            if (email == null || msgParts == null)
            {
                Response.Redirect("Pop3Client.aspx");
                Response.Flush();
                Response.End();
            }
            MessagePart preferredMsgPart = FindMessagePart(msgParts, "text/html");

            if (preferredMsgPart == null)
            {
                preferredMsgPart = FindMessagePart(msgParts,
                                                   "text/plain");
            }

            else if (preferredMsgPart == null && msgParts.Count > 0)
            {
                preferredMsgPart = msgParts[0];
            }
            string contentType, charset, contentTransferEncoding, body = null;

            if (preferredMsgPart != null)
            {
                contentType             = preferredMsgPart.Headers["Content-Type"];
                charset                 = "us-ascii";
                contentTransferEncoding = preferredMsgPart.Headers
                                          ["Content-Transfer-Encoding"];
                Match m = CharsetRegex.Match(contentType);
                if (m.Success)
                {
                    charset = m.Groups["charset"].Value;
                }
                HeadersLiteral.Text = contentType != null
                        ? "Content-Type: " + contentType +
                                      "<br />" : string.Empty;
                HeadersLiteral.Text += contentTransferEncoding !=
                                       null ?"Content-Transfer-Encoding: " +
                                       contentTransferEncoding : string.Empty;
                if (contentTransferEncoding != null)
                {
                    if (contentTransferEncoding.ToLower() == "base64")
                    {
                        body = DecodeBase64String(charset,
                                                  preferredMsgPart.MessageText);
                    }
                    else
                    if (contentTransferEncoding.ToLower() ==
                        "quoted-printable")
                    {
                        body = DecodeQuotedPrintableString
                                   (preferredMsgPart.MessageText);
                    }
                    else
                    {
                        body = preferredMsgPart.MessageText;
                    }
                }
                else
                {
                    body = preferredMsgPart.MessageText;
                }
            }
            EmailIdLiteral.Text = Convert.ToString(emailId);
            DateLiteral.Text    = email.UtcDateTime.ToString();;
            FromLiteral.Text    = email.From;
            SubjectLiteral.Text = email.Subject;
            BodyLiteral.Text    = preferredMsgPart != null ?
                                  (preferredMsgPart.Headers["Content-Type"].IndexOf("text/plain")
                                   != -1 ?"<pre>" + FormatUrls(body) + "</pre>" : body) : null;
            ListAttachments(msgParts);
        }