internal static List <RfcMailAddress> ParseMailAddresses(string input) { if (input == null) { throw new ArgumentNullException("input"); } List <RfcMailAddress> returner = new List <RfcMailAddress>(); IEnumerable <string> mailAddresses = Utils.SplitStringWithCharNotInsideQuotes(input, ','); foreach (string mailAddress in mailAddresses) { returner.Add(RfcMailAddress.ParseMailAddress(mailAddress)); } return(returner); }
/// <summary> /// Parses an email address from a MIME header /// </summary> /// <param name="input"></param> /// <returns></returns> internal static RfcMailAddress ParseMailAddress(string input) { if (input == null) { throw new ArgumentNullException("input"); } input = Utils.Decode(input.Trim()); int indexStartEmail = input.LastIndexOf('<'); int indexEndEmail = input.LastIndexOf('>'); RfcMailAddress result; try { if (indexStartEmail >= 0 && indexEndEmail >= 0) { string username; if (indexStartEmail > 0) { username = input.Substring(0, indexStartEmail).Trim(); } else { username = string.Empty; } indexStartEmail++; int emailLength = indexEndEmail - indexStartEmail; string emailAddress = input.Substring(indexStartEmail, emailLength).Trim(); if (!string.IsNullOrEmpty(emailAddress)) { result = new RfcMailAddress(new MailAddress(emailAddress, username), input); return(result); } } if (input.Contains("@")) { result = new RfcMailAddress(new MailAddress(input), input); return(result); } } catch (FormatException) { throw new Exception("RfcMailAddress: Improper mail address: \"" + input + "\""); } result = new RfcMailAddress(input); return(result); }
private void ParseHeader(string headerName, string headerValue) { if (headerName == null) { throw new ArgumentNullException("headerName"); } if (headerValue == null) { throw new ArgumentNullException("headerValue"); } string text = headerName.ToUpperInvariant(); switch (text) { case "TO": this.To = RfcMailAddress.ParseMailAddresses(headerValue); return; case "CC": this.Cc = RfcMailAddress.ParseMailAddresses(headerValue); return; case "BCC": this.Bcc = RfcMailAddress.ParseMailAddresses(headerValue); return; case "FROM": this.From = RfcMailAddress.ParseMailAddress(headerValue); return; case "REPLY-TO": this.ReplyTo = RfcMailAddress.ParseMailAddress(headerValue); return; case "SENDER": this.Sender = RfcMailAddress.ParseMailAddress(headerValue); return; case "KEYWORDS": { string[] keywordsTemp = headerValue.Split(new char[] { ',' }); string[] array = keywordsTemp; for (int i = 0; i < array.Length; i++) { string keyword = array[i]; this.Keywords.Add(Utils.RemoveQuotesIfAny(keyword.Trim())); } return; } case "RECEIVED": this.Received.Add(new Received(headerValue.Trim())); return; case "IMPORTANCE": this.Importance = HeaderFieldParser.ParseImportance(headerValue.Trim()); return; case "DISPOSITION-NOTIFICATION-TO": this.DispositionNotificationTo = RfcMailAddress.ParseMailAddresses(headerValue); return; case "MIME-VERSION": this.MimeVersion = headerValue.Trim(); return; case "SUBJECT": this.Subject = Utils.Decode(headerValue); return; case "RETURN-PATH": this.ReturnPath = RfcMailAddress.ParseMailAddress(headerValue); return; case "MESSAGE-ID": this.MessageId = HeaderFieldParser.ParseId(headerValue); return; case "IN-REPLY-TO": this.InReplyTo = HeaderFieldParser.ParseMultipleIDs(headerValue); return; case "REFERENCES": this.References = HeaderFieldParser.ParseMultipleIDs(headerValue); return; case "DATE": this.Date = headerValue.Trim(); this.DateSent = Utils.StringToDate(headerValue); return; case "CONTENT-TRANSFER-ENCODING": this.ContentTransferEncoding = HeaderFieldParser.ParseContentTransferEncoding(headerValue.Trim()); return; case "CONTENT-DESCRIPTION": this.ContentDescription = Utils.Decode(headerValue.Trim()); return; case "CONTENT-TYPE": this.ContentType = HeaderFieldParser.ParseContentType(headerValue); return; case "CONTENT-DISPOSITION": this.ContentDisposition = HeaderFieldParser.ParseContentDisposition(headerValue); return; case "CONTENT-ID": this.ContentId = HeaderFieldParser.ParseId(headerValue); return; } this.UnknownHeaders.Add(headerName, headerValue); }