RFC 2822 3.4. (Address Specification) Mailbox address.

Syntax: ["display-name"<SP>]<local-part@domain>.

Inheritance: Address
		/// <summary>
		/// Inserts a new mailbox into the collection at the specified location.
		/// </summary>
		/// <param name="index">The location in the collection where you want to add the mailbox.</param>
		/// <param name="mailbox">Mailbox to add.</param>
		public void Insert(int index,MailboxAddress mailbox)
		{
			m_pMailboxes.Insert(index,mailbox);

			OnCollectionChanged();
		}
		/// <summary>
		/// Removes specified mailbox from the collection.
		/// </summary>
		/// <param name="mailbox">Mailbox to remove.</param>
		public void Remove(MailboxAddress mailbox)
		{
			m_pMailboxes.Remove(mailbox);

			OnCollectionChanged();
		}
        /// <summary>
        /// Parses addresses from IMAP ENVELOPE addresses structure.
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        private MailboxAddress[] ParseAddresses(StringReader r)
        {
            r.ReadToFirstChar();
            if(r.StartsWith("NIL",false)){
                // Remove NIL
                r.ReadSpecifiedLength("NIL".Length);

                return null;
            }
            else{
                r.ReadToFirstChar();

                // This must be ((address)[*(address)])
                if(!r.StartsWith("(")){
                    throw new Exception("Invalid IMAP ENVELOPE structure !");
                }
                else{
                    // Read addresses
                    string addressesString = r.ReadParenthesized();

                    ArrayList addresses = new ArrayList();
                    StringReader rAddresses = new StringReader(addressesString.Trim());
                    // Now we have (address)[*(address)], read addresses
                    while(rAddresses.StartsWith("(")){
                        addresses.Add(ParseAddress(rAddresses.ReadParenthesized()));

                        rAddresses.ReadToFirstChar();
                    }

                    MailboxAddress[] retVal = new MailboxAddress[addresses.Count];
                    addresses.CopyTo(retVal);

                    return retVal;
                }
            }
        }
		/// <summary>
		/// Adds a new mailbox to the end of the collection.
		/// </summary>
		/// <param name="mailbox">Mailbox to add.</param>
		public void Add(MailboxAddress mailbox)
		{
			m_pMailboxes.Add(mailbox);

			OnCollectionChanged();
		}
        /// <summary>
        /// Parses ENVELOPE from IMAP envelope string.
        /// </summary>
        /// <param name="envelopeString">Envelope string.</param>
        public void Parse(string envelopeString)
        {
            if(envelopeString.StartsWith("(")){
                envelopeString = envelopeString.Substring(1);
            }
            if(envelopeString.EndsWith(")")){
                envelopeString = envelopeString.Substring(0,envelopeString.Length - 1);
            }

            string word = "";
            StringReader r = new StringReader(envelopeString);

            #region Date

            // Date
            word = r.ReadWord();
            if(word == null){
                throw new Exception("Invalid IMAP ENVELOPE structure !");
            }
            if(word.ToUpper() == "NIL"){
                m_Date = DateTime.MinValue;
            }
            else{
                try{
                    m_Date = MimeUtils.ParseDate(word);
                }
                catch{
                    // Failed to parse date, return minimum.
                    m_Date = DateTime.MinValue;
                }
            }

            #endregion

            #region Subject

            // Subject
            word = r.ReadWord();
            if(word == null){
                throw new Exception("Invalid IMAP ENVELOPE structure !");
            }
            if(word.ToUpper() == "NIL"){
                m_Subject = null;
            }
            else{
                m_Subject = Core.CanonicalDecode(word);
            }

            #endregion

            #region From

            // From
            m_From = ParseAddresses(r);

            #endregion

            #region Sender

            // Sender
            //	NOTE: There is confusing part, according rfc 2822 Sender: is MailboxAddress and not AddressList.
            MailboxAddress[] sender = ParseAddresses(r);
            if(sender != null && sender.Length > 0){
                m_Sender = sender[0];
            }
            else{
                m_Sender = null;
            }

            #endregion

            #region ReplyTo

            // ReplyTo
            m_ReplyTo = ParseAddresses(r);

            #endregion

            #region To

            // To
            m_To = ParseAddresses(r);

            #endregion

            #region Cc

            // Cc
            m_Cc = ParseAddresses(r);

            #endregion

            #region Bcc

            // Bcc
            m_Bcc = ParseAddresses(r);

            #endregion

            #region InReplyTo

            // InReplyTo
            r.ReadToFirstChar();
            word = r.ReadWord();
            if(word == null){
                throw new Exception("Invalid IMAP ENVELOPE structure !");
            }
            if(word.ToUpper() == "NIL"){
                m_InReplyTo = null;
            }
            else{
                m_InReplyTo = word;
            }

            #endregion

            #region MessageID

            // MessageID
            r.ReadToFirstChar();
            word = r.ReadWord();
            if(word == null){
                throw new Exception("Invalid IMAP ENVELOPE structure !");
            }
            if(word.ToUpper() == "NIL"){
                m_MessageID = null;
            }
            else{
                m_MessageID = word;
            }

            #endregion
        }
        /// <summary>
        /// Constructs ENVELOPE address structure.
        /// </summary>
        /// <param name="address">Mailbox address.</param>
        /// <returns></returns>
        private static string ConstructAddress(MailboxAddress address)
        {
            /* 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.
            */

            // NOTE: all header fields and parameters must in ENCODED form !!!

            StringBuilder retVal = new StringBuilder();
            retVal.Append("(");

            // personal name
            retVal.Append(TextUtils.QuoteString(MimeUtils.EncodeHeaderField(address.DisplayName)));

            // source route, always NIL (not used nowdays)
            retVal.Append(" NIL");

            // mailbox name
            retVal.Append(" " + TextUtils.QuoteString(MimeUtils.EncodeHeaderField(address.LocalPart)));

            // host name
            retVal.Append(" " + TextUtils.QuoteString(MimeUtils.EncodeHeaderField(address.Domain)));

            retVal.Append(")");

            return retVal.ToString();
        }
        /// <summary>
        /// Removes specified mailbox from the collection.
        /// </summary>
        /// <param name="mailbox">Mailbox to remove.</param>
        public void Remove(MailboxAddress mailbox)
        {
            m_pMailboxes.Remove(mailbox);

            OnCollectionChanged();
        }
        /// <summary>
        /// Inserts a new mailbox into the collection at the specified location.
        /// </summary>
        /// <param name="index">The location in the collection where you want to add the mailbox.</param>
        /// <param name="mailbox">Mailbox to add.</param>
        public void Insert(int index, MailboxAddress mailbox)
        {
            m_pMailboxes.Insert(index, mailbox);

            OnCollectionChanged();
        }
        /// <summary>
        /// Adds a new mailbox to the end of the collection.
        /// </summary>
        /// <param name="mailbox">Mailbox to add.</param>
        public void Add(MailboxAddress mailbox)
        {
            m_pMailboxes.Add(mailbox);

            OnCollectionChanged();
        }
示例#10
0
        /// <summary>
        /// Converts address array to RFC 2822 address field fomat.
        /// </summary>
        /// <param name="addresses"></param>
        /// <returns></returns>
        private string AddressesToString(MailboxAddress[] addresses)
        {
            string retVal = "";
            for(int i=0;i<addresses.Length;i++){
                // For last address don't add , and <TAB>
                if(i == (addresses.Length - 1)){
                    retVal += addresses[i].MailboxString;
                }
                else{
                    retVal += addresses[i].MailboxString + ",\t";
                }
            }

            return retVal;
        }
示例#11
0
        /// <summary>
        ///  Converts RFC 2822 address filed value to address array.
        /// </summary>
        /// <param name="addressFieldValue"></param>
        /// <returns></returns>
        private MailboxAddress[] AddressesToArray(string addressFieldValue)
        {
            // We need to parse right !!!
            // Can't use standard String.Split() because commas in quoted strings must be skiped.
            // Example: "ivar, test" <*****@*****.**>,"xxx" <*****@*****.**>

            string[] retVal = TextUtils.SplitQuotedString(addressFieldValue,',');

            MailboxAddress[] xxx = new MailboxAddress[retVal.Length];
            for(int i=0;i<retVal.Length;i++){
                xxx[i] = MailboxAddress.Parse(retVal[i].Trim()); // Trim <TAB>s
            }

            return xxx;
        }