Example #1
0
        /// <summary>
        /// Construct FETCH ENVELOPE response.
        /// </summary>
        /// <param name="parser"></param>
        /// <returns></returns>
        public static string ConstructEnvelope(MimeParser parser)
        {
            /* Rfc 3501 7.4.2
             *      ENVELOPE
             *      A parenthesized list that describes the envelope structure of a
             *      message.  This is computed by the server by parsing the
             *      [RFC-2822] header into the component parts, defaulting various
             *      fields as necessary.
             *
             *      The fields of the envelope structure are in the following
             *      order: date, subject, from, sender, reply-to, to, cc, bcc,
             *      in-reply-to, and message-id.  The date, subject, in-reply-to,
             *      and message-id fields are strings.  The from, sender, reply-to,
             *      to, cc, and bcc fields are parenthesized lists of address
             *      structures.
             *
             *      An address structure is a parenthesized list that describes an
             *      electronic mail address.  The fields of an address structure
             *      are in the following order: personal name, [SMTP]
             *      at-domain-list (source route), mailbox name, and host name.
             *
             *      [RFC-2822] group syntax is indicated by a special form of
             *      address structure in which the host name field is NIL.  If the
             *      mailbox name field is also NIL, this is an end of group marker
             *      (semi-colon in RFC 822 syntax).  If the mailbox name field is
             *      non-NIL, this is a start of group marker, and the mailbox name
             *      field holds the group name phrase.
             *
             *      If the Date, Subject, In-Reply-To, and Message-ID header lines
             *      are absent in the [RFC-2822] header, the corresponding member
             *      of the envelope is NIL; if these header lines are present but
             *      empty the corresponding member of the envelope is the empty
             *      string.
             */
            // ((sender))
            // ENVELOPE ("date" "subject" from sender reply-to to cc bcc in-reply-to "messageID")

            string envelope = "ENVELOPE (";

            // date
            envelope += "\"" + parser.MessageDate.ToString("r", System.Globalization.DateTimeFormatInfo.InvariantInfo) + "\" ";

            // subject
            envelope += "\"" + parser.Subject + "\" ";

            // from
            // ToDo: May be multiple senders
            LumiSoft.Net.Mime.Parser.eAddress adr = new LumiSoft.Net.Mime.Parser.eAddress(parser.From);
            envelope += "((\"" + adr.Name + "\" NIL \"" + adr.Mailbox + "\" \"" + adr.Domain + "\")) ";

            // sender
            // ToDo: May be multiple senders
            envelope += "((\"" + adr.Name + "\" NIL \"" + adr.Mailbox + "\" \"" + adr.Domain + "\")) ";

            // reply-to
            string replyTo = MimeParser.ParseHeaderField("reply-to:", parser.Headers);

            if (replyTo.Length > 0)
            {
                envelope += "(";
                foreach (string recipient in replyTo.Split(';'))
                {
                    LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                    envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
                }
                envelope  = envelope.TrimEnd();
                envelope += ") ";
            }
            else
            {
                envelope += "NIL ";
            }

            // to
            string[] to = parser.To;
            envelope += "(";
            foreach (string recipient in to)
            {
                LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
            }
            envelope  = envelope.TrimEnd();
            envelope += ") ";

            // cc
            string cc = MimeParser.ParseHeaderField("CC:", parser.Headers);

            if (cc.Length > 0)
            {
                envelope += "(";
                foreach (string recipient in cc.Split(';'))
                {
                    LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                    envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
                }
                envelope  = envelope.TrimEnd();
                envelope += ") ";
            }
            else
            {
                envelope += "NIL ";
            }

            // bcc
            string bcc = MimeParser.ParseHeaderField("BCC:", parser.Headers);

            if (bcc.Length > 0)
            {
                envelope += "(";
                foreach (string recipient in bcc.Split(';'))
                {
                    LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                    envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
                }
                envelope  = envelope.TrimEnd();
                envelope += ") ";
            }
            else
            {
                envelope += "NIL ";
            }

            // in-reply-to
            string inReplyTo = MimeParser.ParseHeaderField("in-reply-to:", parser.Headers);

            if (inReplyTo.Length > 0)
            {
                envelope += "\"" + inReplyTo + "\"";
            }
            else
            {
                envelope += "NIL ";
            }

            // message-id
            if (parser.MessageID.Length > 0)
            {
                envelope += "\"" + parser.MessageID + "\"";
            }
            else
            {
                envelope += "NIL";
            }

            envelope += ")";

            return(envelope);
        }
Example #2
0
        /// <summary>
        /// Construct FETCH ENVELOPE response.
        /// </summary>
        /// <param name="parser"></param>
        /// <returns></returns>
        public static string ConstructEnvelope(MimeParser parser)
        {
            /* Rfc 3501 7.4.2
                ENVELOPE
                A parenthesized list that describes the envelope structure of a
                message.  This is computed by the server by parsing the
                [RFC-2822] header into the component parts, defaulting various
                fields as necessary.

                The fields of the envelope structure are in the following
                order: date, subject, from, sender, reply-to, to, cc, bcc,
                in-reply-to, and message-id.  The date, subject, in-reply-to,
                and message-id fields are strings.  The from, sender, reply-to,
                to, cc, and bcc fields are parenthesized lists of address
                structures.

                An address structure is a parenthesized list that describes an
                electronic mail address.  The fields of an address structure
                are in the following order: personal name, [SMTP]
                at-domain-list (source route), mailbox name, and host name.

                [RFC-2822] group syntax is indicated by a special form of
                address structure in which the host name field is NIL.  If the
                mailbox name field is also NIL, this is an end of group marker
                (semi-colon in RFC 822 syntax).  If the mailbox name field is
                non-NIL, this is a start of group marker, and the mailbox name
                field holds the group name phrase.

                If the Date, Subject, In-Reply-To, and Message-ID header lines
                are absent in the [RFC-2822] header, the corresponding member
                of the envelope is NIL; if these header lines are present but
                empty the corresponding member of the envelope is the empty
                string.
            */
            // ((sender))
            // ENVELOPE ("date" "subject" from sender reply-to to cc bcc in-reply-to "messageID")

            string envelope = "ENVELOPE (";

            // date
            envelope += "\"" + parser.MessageDate.ToString("r",System.Globalization.DateTimeFormatInfo.InvariantInfo) + "\" ";

            // subject
            envelope += "\"" + parser.Subject + "\" ";

            // from
            // ToDo: May be multiple senders
            LumiSoft.Net.Mime.Parser.eAddress adr = new LumiSoft.Net.Mime.Parser.eAddress(parser.From);
            envelope += "((\"" + adr.Name + "\" NIL \"" + adr.Mailbox + "\" \"" + adr.Domain + "\")) ";

            // sender
            // ToDo: May be multiple senders
            envelope += "((\"" + adr.Name + "\" NIL \"" + adr.Mailbox + "\" \"" + adr.Domain + "\")) ";

            // reply-to
            string replyTo = MimeParser.ParseHeaderField("reply-to:",parser.Headers);
            if(replyTo.Length > 0){
                envelope += "(";
                foreach(string recipient in replyTo.Split(';')){
                    LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                    envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
                }
                envelope = envelope.TrimEnd();
                envelope += ") ";
            }
            else{
                envelope += "NIL ";
            }

            // to
            string[] to = parser.To;
            envelope += "(";
            foreach(string recipient in to){
                LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
            }
            envelope = envelope.TrimEnd();
            envelope += ") ";

            // cc
            string cc = MimeParser.ParseHeaderField("CC:",parser.Headers);
            if(cc.Length > 0){
                envelope += "(";
                foreach(string recipient in cc.Split(';')){
                    LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                    envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
                }
                envelope = envelope.TrimEnd();
                envelope += ") ";
            }
            else{
                envelope += "NIL ";
            }

            // bcc
            string bcc = MimeParser.ParseHeaderField("BCC:",parser.Headers);
            if(bcc.Length > 0){
                envelope += "(";
                foreach(string recipient in bcc.Split(';')){
                    LumiSoft.Net.Mime.Parser.eAddress adrTo = new LumiSoft.Net.Mime.Parser.eAddress(recipient);
                    envelope += "(\"" + adrTo.Name + "\" NIL \"" + adrTo.Mailbox + "\" \"" + adrTo.Domain + "\") ";
                }
                envelope = envelope.TrimEnd();
                envelope += ") ";
            }
            else{
                envelope += "NIL ";
            }

            // in-reply-to
            string inReplyTo = MimeParser.ParseHeaderField("in-reply-to:",parser.Headers);
            if(inReplyTo.Length > 0){
                envelope += "\"" + inReplyTo + "\"";
            }
            else{
                envelope += "NIL ";
            }

            // message-id
            if(parser.MessageID.Length > 0){
                envelope += "\"" + parser.MessageID + "\"";
            }
            else{
                envelope += "NIL";
            }

            envelope += ")";

            return envelope;
        }
        /// <summary>
        /// Parse sender from message.
        /// </summary>
        /// <param name="headers"></param>
        /// <returns></returns>
        private string ParseFrom(string headers)
        {
            using(TextReader r = new StreamReader(new MemoryStream(System.Text.Encoding.ASCII.GetBytes(headers)))){
                string line = r.ReadLine();

                while(line != null){
                    if(line.ToUpper().StartsWith("FROM:")){
                        LumiSoft.Net.Mime.Parser.eAddress e = new LumiSoft.Net.Mime.Parser.eAddress(line.Substring(5).Trim());

                        return CDecode(e.Email);
                    }

                    line = r.ReadLine();
                }
            }

            return "";
        }