internal ImapMessage(long number, ImapList list, IEnumerable<string> extensionParameterNames) { Number = number; int uidIndex = list.IndexOfString("UID"); int bodyIndex = list.IndexOfString("BODYSTRUCTURE"); int envelopeIndex = list.IndexOfString("ENVELOPE"); if (uidIndex != -1) { Uid = long.Parse(list.GetStringAt(uidIndex + 1)); } if (envelopeIndex != -1) { ImapList envelopeList = list.GetListAt(envelopeIndex + 1); string timestampString = envelopeList.GetStringAt(0); DateTime timestamp; if (TryParseTimestamp(timestampString, out timestamp)) { Timestamp = timestamp; } Subject = RFC2047Decoder.Parse(envelopeList.GetStringAt(1)); Sender = ParseAddresses(envelopeList.GetListAt(2)).FirstOrDefault(); From = ParseAddresses(envelopeList.GetListAt(3)).FirstOrDefault(); ReplyTo = ParseAddresses(envelopeList.GetListAt(4)).FirstOrDefault(); To = ParseAddresses(envelopeList.GetListAt(5)).ToArray(); Cc = ParseAddresses(envelopeList.GetListAt(6)).ToArray(); Bcc = ParseAddresses(envelopeList.GetListAt(7)).ToArray(); ID = envelopeList.GetStringAt(8); } if (bodyIndex != -1) { ImapList bodyList = list.GetListAt(bodyIndex + 1); if (bodyList.Count != 0) { BodyParts = ParseBodyParts(string.Empty, bodyList).ToArray(); } } if (null != extensionParameterNames) { var extensionParams = new Dictionary<string, object>(); foreach (var paramName in extensionParameterNames) { int index = list.IndexOfString(paramName); if (index != -1) { int valueIndex = index + 1; object value = null; if (list.IsStringAt(valueIndex)) { value = list.GetStringAt(valueIndex); } else if (list.IsListAt(valueIndex)) { value = list.GetListAt(valueIndex).ToBasicTypesList(); } if (null != value) { extensionParams[paramName] = value; } } } if (extensionParams.Count > 0) { ExtensionParameters = extensionParams; } } }
private static IEnumerable<ImapBodyPart> ParseBodyParts(string section, ImapList bodyList) { if (bodyList.IsStringAt(0)) { yield return new ImapBodyPart(string.IsNullOrEmpty(section) ? "1" : section, bodyList); } else { string innerSectionPrefix = string.IsNullOrEmpty(section) ? string.Empty : section + "."; string mutipartType = bodyList.GetStringAt(bodyList.Count - 4); if (!string.IsNullOrEmpty(mutipartType)) { for (int i = 0; i < bodyList.Count - 4; i++) { string innerSection = innerSectionPrefix + (i + 1).ToString(); ImapList innerBodyList = bodyList.GetListAt(i); if (innerBodyList.Count != 0) { foreach (ImapBodyPart part in ParseBodyParts(innerSection, innerBodyList)) { yield return part; } } } } } }