LumiSoft.Net.Mime.MailboxAddress.Parse C# (CSharp) Method

Parse() public static method

Parses mailbox from mailbox address string.
public static Parse ( string mailbox ) : MailboxAddress
mailbox string Mailbox string. Format: ["diplay-name"<SP>]<local-part@domain>.
return MailboxAddress
        public static MailboxAddress Parse(string mailbox)
        {
            mailbox = mailbox.Trim();

            /* We must parse following situations:
                "Ivar Lumi" <[email protected]>
                "Ivar Lumi" [email protected]
                <[email protected]>
                [email protected]
                Ivar Lumi <[email protected]>
            */

            string name = "";
            string emailAddress = mailbox;

            // Email address is between <> and remaining left part is display name
            if(mailbox.IndexOf("<") > -1 && mailbox.IndexOf(">") > -1){
                name = TextUtils.RemoveQuotes(mailbox.Substring(0,mailbox.IndexOf("<")));
                emailAddress = mailbox.Substring(mailbox.IndexOf("<") + 1,mailbox.Length - mailbox.IndexOf("<") - 2);
            }
            else{
                // There is name included, parse it
                if(mailbox.StartsWith("\"")){
                    int startIndex = mailbox.IndexOf("\"");
                    if(startIndex > -1 && mailbox.LastIndexOf("\"") > startIndex){
                        name = mailbox.Substring(startIndex + 1,mailbox.LastIndexOf("\"") - startIndex - 1).Trim();
                    }

                    emailAddress = mailbox.Substring(mailbox.LastIndexOf("\"") + 1).Trim();
                }

                // Right part must be email address
                emailAddress = emailAddress.Replace("<","").Replace(">","").Trim();
            }

            return new MailboxAddress(name,emailAddress);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Parses address-list from string.
        /// </summary>
        /// <param name="addressList">Address list string.</param>
        /// <returns></returns>
        public void Parse(string addressList)
        {
            addressList = addressList.Trim();

            StringReader reader = new StringReader(addressList);

            while (reader.SourceString.Length > 0)
            {
                // See if mailbox or group. If ',' is before ':', then mailbox
                // Example: [email protected],	group:[email protected];
                int commaIndex = TextUtils.QuotedIndexOf(reader.SourceString, ',');
                int colonIndex = TextUtils.QuotedIndexOf(reader.SourceString, ':');

                // Mailbox
                if (colonIndex == -1 || (commaIndex < colonIndex && commaIndex != -1))
                {
                    // FIX: why quotes missing
                    //System.Windows.Forms.MessageBox.Show(reader.SourceString + "#" + reader.OriginalString);

                    // Read to ',' or to end if last element
                    MailboxAddress address = MailboxAddress.Parse(reader.QuotedReadToDelimiter(','));
                    m_pAddresses.Add(address);
                    address.Owner = this;
                }
                // Group
                else
                {
                    // Read to ';', this is end of group
                    GroupAddress address = GroupAddress.Parse(reader.QuotedReadToDelimiter(';'));
                    m_pAddresses.Add(address);
                    address.Owner = this;

                    // If there are next items, remove first comma because it's part of group address
                    if (reader.SourceString.Length > 0)
                    {
                        reader.QuotedReadToDelimiter(',');
                    }
                }
            }

            OnCollectionChanged();
        }