Класс Message.cs
Листинг 3.15.
(html, txt)
case "multipart/alternative": MultipartMixed(); break;
case "multipart/related": MultipartMixed(); break;
case "text/html": _body = _message; _body_type = BodyTypes.HTML; break;
case "text/plain": _body = _message; _body_type = BodyTypes.Text; break; } }
/// <summary> /// Анализирует строку для получения e-mail. /// </summary> /// <param name="mail">Строка с адресом</param> /// <returns>адрес типа [mail@host.com], [mail@localhost] или [host@123.123.123.123]</returns> public string ExtractEmailFromAddress(string mail) { // mail@(ip)|(host) Regex ex = new Regex(@"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.?)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)"); Match m = ex.Match(mail); if (!m.Success) { throw new ParseException("email не найден."); }
return m.ToString(); }
public void Dispose() { foreach(AttachDescriptor ad in Attachments) { ad.Close(); }
_attaches = null; _body = null; _message = null; Headers = null; ChildMimes = null;
GC.SuppressFinalize(this); } /// <summary> /// Перегруженный метод ToString. Возвращает информацию о сообщении. /// </summary> /// <returns></returns> public override string ToString() { StringBuilder sb = new StringBuilder();
sb.AppendFormat("Size: {0}b\r\n", GetSize()); sb.AppendFormat("From: '{0}', email: '{1}'\r\n", From, FromEmail); sb.AppendFormat("Subject: '{0}'\r\n", Subject);
if (Attachments.Length > 0) { sb.Append("Attachments:\r\n");
foreach(AttachDescriptor ad in Attachments) { sb.Append("\tName: " + ad.Name + " Size: " + ad.Size + "\r\n"); } }
return sb.ToString(); } // /// <summary> /// Возможные типы тела сообщения. /// </summary> public enum BodyTypes { Unknown, HTML, Text }
/// <summary> /// Возвращение тела сообщения из MIME. /// </summary> /// <param name="type">Тип тела сообщения.</param> /// <param name="mimes">MIME</param> /// <returns></returns> string GetBodyFromMime(BodyTypes type, Mime [] mimes) { foreach (Mime m in mimes) { if (m.ChildMimes.Length == 0) { switch(type) { case BodyTypes.HTML: if (m.ContentType.IndexOf("text/html") > -1) { return m.PreparedBody(); } break;
case BodyTypes.Text: if (m.ContentType.IndexOf("text/plain") > -1) { return m.PreparedBody(); } break; } } else { string r = GetBodyFromMime(type, m.ChildMimes); if (r != "") return r; } }
return ""; }
/// <summary> /// Открытый метод, возвращающий тело сообщения. /// </summary> /// <param name="type">Тип тела сообщения.</param> /// <returns></returns> public string GetBody(BodyTypes type) { if (_body_type == BodyTypes.Unknown) return GetBodyFromMime(type, ChildMimes); else return PreparedString(_body);
}
/// <summary> /// Возвращение тела сообщения. /// </summary> /// <returns></returns> public string Text { get { string text; if (_body_type == BodyTypes.Unknown) { text = GetBodyFromMime(BodyTypes.Text, ChildMimes); if (text == null || text.Trim() == "") text = Utils.ExtractTextFromHtml(GetBodyFromMime(BodyTypes.HTML, ChildMimes)); } else { text = PreparedString(_body); if (_body_type == BodyTypes.HTML) text = Utils.ExtractTextFromHtml(text); }
return text.Trim(); } }
#region Common headers /// <summary> /// Организация. /// </summary> public string Organization { get { return (string)Headers["organization"]; } }
/// <summary> /// Копии письма. /// </summary> public string CC { get { return (string)Headers["cc"]; } }
/// <summary> /// Дата сообщения. /// </summary> public string Date { get { return (string)Headers["date"]; } }
/// <summary> /// Адрес отправителя. /// </summary> public string ReturnPath { get { return (string)Headers["return-path"]; } }
/// <summary> /// Адрес отправителя. /// </summary> public string From { get { return (string)Headers["from"]; } } /// <summary> /// От кого. /// </summary> public string FromEmail { get { return ExtractEmailFromAddress((string)Headers["from"]); } } /// <summary> /// Кому /// </summary> public string To { get { return (string)Headers["to"]; } } /// <summary> /// Тема /// </summary> public string Subject { get { return Utils.WordDecoder((string)Headers["subject"]); } } /// <summary> /// Повтор /// </summary> public string ReplyTo { get { return (string)Headers["reply-to"]; } } #endregion } }
Листинг 3.15.