Exemplo n.º 1
0
    private void Page_Load(object sender, System.EventArgs e)
    {
        System.DateTime dateTime = System.DateTime.Now;
        string          s1       = "Message_Backup_\uFFFD" + dateTime.ToString("ddMMyy_HHmmss\uFFFD") + ".zip\uFFFD";

        System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
        ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipOutputStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(memoryStream);
        ActiveUp.Net.Mail.Mailbox mailbox = ((ActiveUp.Net.Mail.Imap4Client)Session["imapobject\uFFFD"]).SelectMailbox(Request.QueryString["b\uFFFD"]);
        char[]   chArr = new char[] { ',' };
        string[] sArr  = Request.QueryString["m\uFFFD"].Split(chArr);
        for (int i = 0; i < sArr.Length; i++)
        {
            string s2   = sArr[i];
            byte[] bArr = mailbox.Fetch.Message(System.Convert.ToInt32(s2));
            ActiveUp.Net.Mail.Header             header   = ActiveUp.Net.Mail.Parser.ParseHeader(bArr);
            ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(header.Subject + ".eml\uFFFD");
            zipOutputStream.PutNextEntry(zipEntry);
            zipOutputStream.SetLevel(9);
            zipOutputStream.Write(bArr, 0, bArr.Length);
            zipOutputStream.CloseEntry();
        }
        zipOutputStream.Finish();
        Response.AddHeader("Content-Disposition\uFFFD", "attachment; filename=\uFFFD" + s1);
        Response.ContentType = "application/zip\uFFFD";
        Response.BinaryWrite(memoryStream.GetBuffer());
        zipOutputStream.Close();
    }
Exemplo n.º 2
0
        public static void ParseHeader(ref Header header)
        {
            string hdr = System.Text.Encoding.ASCII.GetString(header.OriginalData);
            hdr = System.Text.RegularExpressions.Regex.Match(hdr, @"[\s\S]+?((?=\r?\n\r?\n)|\Z)").Value;
            hdr = Parser.Unfold(hdr);
            hdr = Codec.RFC2047Decode(hdr);
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(hdr, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)");
            while(m.Success)
            {
                string name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':')));
                string value = m.Value.Substring(m.Value.IndexOf(":") + 1);
                if (name.Equals("received")) header.Trace.Add(Parser.ParseTrace(m.Value.Trim(' ')));
                else if (name.Equals("to")) header.To = Parser.ParseAddresses(value);
                else if (name.Equals("cc")) header.Cc = Parser.ParseAddresses(value);
                else if (name.Equals("bcc")) header.Bcc = Parser.ParseAddresses(value);
                else if (name.Equals("reply-to")) header.ReplyTo = Parser.ParseAddress(value);
                else if (name.Equals("from")) header.From = Parser.ParseAddress(value);
                else if (name.Equals("sender")) header.Sender = Parser.ParseAddress(value);
                else if (name.Equals("content-type")) header.ContentType = Parser.GetContentType(m.Value);
                else if (name.Equals("content-disposition")) header.ContentDisposition = Parser.GetContentDisposition(m.Value);

                header.HeaderFields.Add(name, value);
                header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':')));
                m = m.NextMatch();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the Messages
        /// </summary>
        private void LoadMessages()
        {
            int MessageSize = (int)(Pop.TotalSize/1024);
            int MessageCount = Pop.MessageCount;

            //Set Title
            this.Text = MessageCount.ToString() + " messages, " + MessageSize.ToString() + " KB.";

            for (int i = 1; i <= MessageCount; i++)
            {
                //Get Header of Each Message
                byte[] data = Pop.RetrieveHeader(i);
                Header head = new Header();
                head.OriginalData = data;
                Parser.ParseHeader(ref head);

                ListViewItem litem = new ListViewItem(head.Date.ToShortDateString());
                litem.Tag = i.ToString();
                litem.SubItems.Add(head.Subject);
                litem.SubItems.Add(head.SenderIP.ToString());
                litem.SubItems.Add(head.From.Name);
                lvwMails.Items.Add(litem);
                
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the subject along with the body of an email.
        /// Does not take into account read and unread email messages.
        /// </summary>
        public void getAllEmail()
        {
            Imap4Client imap = new Imap4Client();

            try
            {
                //Authenticate with the Outlook Server
                imap.ConnectSsl(Constant.OutlookImapHost, Constant.ImapPort);
                imap.Login(Constant.OutlookUserName, Constant.GenericPassword);

                //Select a mailbox folder
                Mailbox inbox = imap.SelectMailbox("inbox");
                Console.WriteLine("Message Count: " + inbox.MessageCount);

                //Iterate through the mailbox and fetch the mail objects
                for (int i = 1; i <= inbox.MessageCount; i++)
                {
                    Header  header = (inbox.Fetch.HeaderObject(i));
                    Message msg    = (inbox.Fetch.MessageObject(i));
                    //need not fetch the header if we r fetching the whole object
                    Console.WriteLine(header.Subject);
                    Console.WriteLine(msg.BodyText.Text);
                }
            }
            catch (Imap4Exception)
            {
                throw new Imap4Exception();
            }
            catch (Exception)
            {
                throw new Exception();
            }
            finally
            {
                imap.Disconnect();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Parses the header.
        /// </summary>
        /// <param name="header">The header.</param>
        public static void ParseHeader(ref Header header)
        {
#if !PocketPC
            string hdr = System.Text.Encoding.GetEncoding("iso-8859-1").GetString(header.OriginalData,0,header.OriginalData.Length);
#else
            string hdr = Pop3Client.PPCEncode.GetString(header.OriginalData, 0, header.OriginalData.Length);
#endif
            hdr = System.Text.RegularExpressions.Regex.Match(hdr, @"[\s\S]+?((?=\r?\n\r?\n)|\Z)").Value;
            hdr = Parser.Unfold(hdr);
            //hdr = hdr);
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(hdr, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)");
            while (m.Success)
            {
                string name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':')));
                string value = Codec.RFC2047Decode(m.Value.Substring(m.Value.IndexOf(":") + 1)).Trim('\r', '\n').TrimStart(' ');
                if (name.Equals("received")) header.Trace.Add(Parser.ParseTrace(m.Value.Trim(' ')));
                else if (name.Equals("to")) header.To = Parser.ParseAddresses(value);
                else if (name.Equals("cc")) header.Cc = Parser.ParseAddresses(value);
                else if (name.Equals("bcc")) header.Bcc = Parser.ParseAddresses(value);
                else if (name.Equals("reply-to")) header.ReplyTo = Parser.ParseAddress(value);
                else if (name.Equals("from")) header.From = Parser.ParseAddress(value);
                else if (name.Equals("sender")) header.Sender = Parser.ParseAddress(value);
                else if (name.Equals("content-type")) header.ContentType = Parser.GetContentType(m.Value);
                else if (name.Equals("content-disposition")) header.ContentDisposition = Parser.GetContentDisposition(m.Value);
                //else
                //{
                    header.HeaderFields.Add(name, value);
                    header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':')));
                //}
                m = m.NextMatch();

                if (HeaderFieldParsing != null)
                    HeaderFieldParsing(null, header);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Parses a Header object from a byte array.
 /// </summary>
 /// <returns>The parsed Header as a Header object.</returns>
 /// <example>
 /// 	<code lang="CS">
 /// Header Header = Parser.ParseHeader(someBuffer);
 /// //Expose the subject
 /// string subject = header.Subject;
 ///     </code>
 /// 	<code lang="VB">
 /// Dim Header As Header = Parser.ParseHeader(someBuffer)
 /// 'Expose the subject
 /// Dim subject As String = header.Subject
 ///     </code>
 /// 	<code lang="J#">
 /// var header:Header = Parser.ParseHeader(someBuffer);
 /// //Expose the subject
 /// var subject:string = header.Subject;
 ///     </code>
 /// </example>
 /// <param name="data">The byte array containing the Header data to be parsed.</param>
 public static Header ParseHeader(byte[] data)
 {
     Header hdr = new Header();
     hdr.OriginalData = data;
     Parser.ParseHeader(ref hdr);
     return hdr;
 }
Exemplo n.º 7
0
 /// <summary>
 /// Parses a MemoryStream's content to a Header object.
 /// </summary>
 /// <param name="inputStream">The MemoryStream containing the Header data to be parsed.</param>
 /// <returns>The parsed Header as a Header object.</returns>
 /// <example>
 /// <code>
 /// C#
 /// 
 /// Header Header = Parser.ParseHeader(someStream);
 /// //Expose the subject
 /// string subject = header.Subject;
 /// 
 /// VB.NET
 /// 
 /// Dim Header As Header = Parser.ParseHeader(someStream)
 /// 'Expose the subject
 /// Dim subject As String = header.Subject
 /// 
 /// JScript.NET
 /// 
 /// var header:Header = Parser.ParseHeader(someStream);
 /// //Expose the subject
 /// var subject:string = header.Subject;
 /// </code>
 /// </example> 
 public static Header ParseHeader(System.IO.MemoryStream inputStream)
 {
     byte[] buf = new byte[inputStream.Length];
     inputStream.Read(buf, 0, buf.Length);
     Header hdr = new Header();
     hdr.OriginalData = buf;
     Parser.ParseHeader(ref hdr);
     return hdr;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Parses a Header from a file to a Header object.
 /// </summary>
 /// <param name="filePath">The path of the file to be parsed.</param>
 /// <returns>The parsed file as a Header object.</returns>
 /// <example>
 /// <code>
 /// C#
 /// 
 /// Header Header = Parser.ParseHeader("C:\\My headers\\header.txt");
 /// //Expose the subject
 /// string subject = header.Subject;
 /// 
 /// VB.NET
 /// 
 /// Dim Header As Header = Parser.ParseHeader("C:\My headers\header.txt")
 /// 'Expose the subject
 /// Dim subject As String = header.Subject
 /// 
 /// JScript.NET
 /// 
 /// var header:Header = Parser.ParseHeader("C:\\My headers\\header.txt");
 /// //Expose the subject
 /// var subject:string = header.Subject;
 /// </code>
 /// </example> 
 public static Header ParseHeader(string filePath)
 {
     System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
     byte[] data = new byte[fs.Length];
     fs.Read(data, 0, System.Convert.ToInt32(fs.Length));
     fs.Close();
     Header hdr = new Header();
     hdr.OriginalData = data;
     Parser.ParseHeader(ref hdr);
     return hdr;
 }
Exemplo n.º 9
0
        /// <summary>
        /// Fill the mail message object based on Header object.
        /// </summary>
        /// <param name="message">The ActiveUp.Net.Mail.Header.</param>
        public void FillMessage(Header header)
        {
            this.Id = header.MessageId;
            this.From = header.From.ToString();
            this.Subject = header.Subject;
            this.SentDate = header.Date;

            int index = 1;
            foreach (ActiveUp.Net.Mail.Address address in header.To)
            {
                this.To = string.Concat(this.To, address.ToString());
                if (index < header.To.Count)
                {
                    this.To = string.Concat(this.To, ",");
                }
                index++;
            }

            // verify if the messages directory exist, if not create it.
            string directory = "Messages";
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            // create the file with the message.
            //string fileName = string.Concat(this.Id, ".eml");
            //string path = System.IO.Path.Combine(directory, fileName);
            //if (!File.Exists(path))
            //{
            //    FileStream fileStream = File.Create(path);
            //    StreamWriter sw = new StreamWriter(fileStream);
            //    sw.Write(message.BodyHtml.Text);
            //    sw.Close();
            //}

            //this.Path = header.Path;
        }
Exemplo n.º 10
0
 public Message(Header header)
 {
     HeaderFields = header.HeaderFields;
     HeaderFieldNames = header.HeaderFieldNames;
     Trace.AddRange(header.Trace);
     To.AddRange(header.To);
     Cc.AddRange(header.Cc);
     Bcc.AddRange(header.Bcc);
     ReplyTo = header.ReplyTo;
     From = header.From;
     Sender = header.Sender;
     ContentType = header.ContentType;
     ContentDisposition = header.ContentDisposition;
 }
Exemplo n.º 11
0
        public static bool TryParseDefectiveHeader(string origanal_header_data, out Header header)
        {
            if (string.IsNullOrEmpty(origanal_header_data))
            {
                header = null;
                return false;
            }

            var header_bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(origanal_header_data);

            header = new Header { OriginalData = header_bytes };

            var hdr = origanal_header_data;

            hdr = RegxHeader.Match(hdr).Value;
            hdr = RegxUnfold.Replace(hdr, "");

            var m = RegxHeaderLines.Match(hdr);
            while (m.Success)
            {
                try
                {
                    var name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':')));
                    var value =
                        Codec.RFC2047Decode(m.Value.Substring(m.Value.IndexOf(":", StringComparison.Ordinal) + 1))
                             .Trim('\r', '\n')
                             .TrimStart(' ');

                    if (name.Equals("received"))
                    {
                        TraceInfo trace;
                        if (TryParseTrace(m.Value.Trim(' '), out trace))
                        {
                            header.Trace.Add(trace);
                        }
                    }
                    else if (name.Equals("to"))
                    {
                        var address_collection = new AddressCollection();
                        TryParseAddresses(value, out address_collection);
                        header.To = address_collection;
                    }
                    else if (name.Equals("cc"))
                    {
                        AddressCollection address_collection;
                        if (TryParseAddresses(value, out address_collection))
                        {
                            header.Cc = address_collection;
                        }
                    }
                    else if (name.Equals("bcc"))
                    {
                        AddressCollection address_collection;
                        if (TryParseAddresses(value, out address_collection))
                        {
                            header.Bcc = address_collection;
                        }
                    }
                    else if (name.Equals("reply-to"))
                    {
                        Address address;
                        if (TryParseAddress(value, out address))
                        {
                            header.ReplyTo = address;
                        }
                    }
                    else if (name.Equals("from"))
                    {
                        Address address;
                        if (TryParseAddress(value, out address))
                        {
                            header.From = address;
                        }
                    }
                    else if (name.Equals("sender"))
                    {
                        Address address;
                        if (TryParseAddress(value, out address))
                        {
                            header.Sender = address;
                        }
                    }
                    else if (name.Equals("content-type"))
                    {
                        header.ContentType = GetContentType(m.Value);
                    }
                    else if (name.Equals("content-disposition"))
                    {
                        header.ContentDisposition = GetContentDisposition(m.Value);
                    }

                    header.HeaderFields.Add(name, value);
                    header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':')));
                }
                catch { }

                m = m.NextMatch();
            }

            return true;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Parses the header.
        /// </summary>
        /// <param name="header">The header.</param>
        public static void ParseHeader(ref Header header)
        {
#if !PocketPC
            var hdr = Encoding.GetEncoding("iso-8859-1").GetString(header.OriginalData,0,header.OriginalData.Length);
#else
            string hdr = Pop3Client.PPCEncode.GetString(header.OriginalData, 0, header.OriginalData.Length);
#endif
            hdr = _regxHeader.Match(hdr).Value; 
            hdr = Unfold(hdr);

            var m = _regxHeaderLines.Match(hdr);
            while (m.Success)
            {
                var name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':')));
                var value = Codec.RFC2047Decode(m.Value.Substring(m.Value.IndexOf(":", StringComparison.Ordinal) + 1)).Trim('\r', '\n').TrimStart(' ');
                if (name.Equals("received")) header.Trace.Add(ParseTrace(m.Value.Trim(' ')));
                else if (name.Equals("to")) header.To = ParseAddresses(value);
                else if (name.Equals("cc")) header.Cc = ParseAddresses(value);
                else if (name.Equals("bcc")) header.Bcc = ParseAddresses(value);
                else if (name.Equals("reply-to")) header.ReplyTo = ParseAddress(value);
                else if (name.Equals("from")) header.From = ParseAddress(value);
                else if (name.Equals("sender")) header.Sender = ParseAddress(value);
                else if (name.Equals("content-type")) header.ContentType = GetContentType(m.Value);
                else if (name.Equals("content-disposition")) header.ContentDisposition = GetContentDisposition(m.Value);
                //else
                //{
                    header.HeaderFields.Add(name, value);
                    header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':')));
                //}
                m = m.NextMatch();

                if (HeaderFieldParsing != null)
                    HeaderFieldParsing(null, header);
            }
        }
Exemplo n.º 13
0
 /// <summary>
 /// Parses a Header object from a byte array.
 /// </summary>
 /// <returns>The parsed Header as a Header object.</returns>
 /// <example>
 ///     <code lang="CS">
 /// Header Header = Parser.ParseHeader(someBuffer);
 /// //Expose the subject
 /// string subject = header.Subject;
 ///     </code>
 ///     <code lang="VB">
 /// Dim Header As Header = Parser.ParseHeader(someBuffer)
 /// 'Expose the subject
 /// Dim subject As String = header.Subject
 ///     </code>
 ///     <code lang="J#">
 /// var header:Header = Parser.ParseHeader(someBuffer);
 /// //Expose the subject
 /// var subject:string = header.Subject;
 ///     </code>
 /// </example>
 /// <param name="data">The byte array containing the Header data to be parsed.</param>
 public static Header ParseHeader(byte[] data)
 {
     var hdr = new Header {OriginalData = data};
     ParseHeader(ref hdr);
     return hdr;
 }
Exemplo n.º 14
0
 /// <summary>
 /// Parses a MemoryStream's content to a Header object.
 /// </summary>
 /// <param name="input_stream">The MemoryStream containing the Header data to be parsed.</param>
 /// <returns>The parsed Header as a Header object.</returns>
 /// <example>
 /// <code>
 /// C#
 /// 
 /// Header Header = Parser.ParseHeader(someStream);
 /// //Expose the subject
 /// string subject = header.Subject;
 /// 
 /// VB.NET
 /// 
 /// Dim Header As Header = Parser.ParseHeader(someStream)
 /// 'Expose the subject
 /// Dim subject As String = header.Subject
 /// 
 /// JScript.NET
 /// 
 /// var header:Header = Parser.ParseHeader(someStream);
 /// //Expose the subject
 /// var subject:string = header.Subject;
 /// </code>
 /// </example> 
 public static Header ParseHeader(MemoryStream input_stream)
 {
     var buf = new byte[input_stream.Length];
     input_stream.Read(buf, 0, buf.Length);
     var hdr = new Header {OriginalData = buf};
     ParseHeader(ref hdr);
     return hdr;
 }
Exemplo n.º 15
0
 /// <summary>
 /// Parses a Header from a file to a Header object.
 /// </summary>
 /// <param name="file_path">The path of the file to be parsed.</param>
 /// <returns>The parsed file as a Header object.</returns>
 /// <example>
 /// <code>
 /// C#
 /// 
 /// Header Header = Parser.ParseHeader("C:\\My headers\\header.txt");
 /// //Expose the subject
 /// string subject = header.Subject;
 /// 
 /// VB.NET
 /// 
 /// Dim Header As Header = Parser.ParseHeader("C:\My headers\header.txt")
 /// 'Expose the subject
 /// Dim subject As String = header.Subject
 /// 
 /// JScript.NET
 /// 
 /// var header:Header = Parser.ParseHeader("C:\\My headers\\header.txt");
 /// //Expose the subject
 /// var subject:string = header.Subject;
 /// </code>
 /// </example> 
 public static Header ParseHeader(string file_path)
 {
     var fs = File.OpenRead(file_path);
     var data = new byte[fs.Length];
     fs.Read(data, 0, Convert.ToInt32(fs.Length));
     fs.Close();
     var hdr = new Header {OriginalData = data};
     ParseHeader(ref hdr);
     return hdr;
 }
        private Header GetImapHeader(Mailbox mb, int uid)
        {
            var header_bytes = mb.Fetch.UidHeader(uid);

            var header = new Header {OriginalData = header_bytes};

            var hdr = Encoding.GetEncoding("iso-8859-1")
                              .GetString(header.OriginalData, 0, header.OriginalData.Length);

            hdr = RegxHeader.Match(hdr).Value;
            hdr = RegxUnfold.Replace(hdr, "");

            var m = RegxHeaderLines.Match(hdr);
            while (m.Success)
            {
                try
                {
                    var name = FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':')));
                    var value =
                        Codec.RFC2047Decode(m.Value.Substring(m.Value.IndexOf(":", StringComparison.Ordinal) + 1))
                             .Trim('\r', '\n')
                             .TrimStart(' ');
                    if (name.Equals("received")) header.Trace.Add(Parser.ParseTrace(m.Value.Trim(' ')));
                    else if (name.Equals("to")) header.To = Parser.ParseAddresses(value);
                    else if (name.Equals("cc")) header.Cc = Parser.ParseAddresses(value);
                    else if (name.Equals("bcc")) header.Bcc = Parser.ParseAddresses(value);
                    else if (name.Equals("reply-to")) header.ReplyTo = Parser.ParseAddress(value);
                    else if (name.Equals("from")) header.From = Parser.ParseAddress(value);
                    else if (name.Equals("sender")) header.Sender = Parser.ParseAddress(value);
                    else if (name.Equals("content-type")) header.ContentType = Parser.GetContentType(m.Value);
                    else if (name.Equals("content-disposition"))
                        header.ContentDisposition = Parser.GetContentDisposition(m.Value);

                    header.HeaderFields.Add(name, value);
                    header.HeaderFieldNames.Add(name, m.Value.Substring(0, m.Value.IndexOf(':')));
                }
                catch (Exception parser_header_line_ex)
                {
                    _log.Debug("Parser header field error: {0} ", parser_header_line_ex.ToString());
                }

                m = m.NextMatch();
            }

            return header;
        }