/// <summary> /// Sets the message body. /// </summary> /// <param name="message">The message.</param> /// <param name="child">The child.</param> private void SetMessageBody(MailMessageEx message, MimeEntity child) { Encoding encoding = child.GetEncoding(); message.Body = DecodeBytes(child.Content.ToArray(), encoding); message.BodyEncoding = encoding; message.IsBodyHtml = string.Equals(MediaTypes.TextHtml, child.ContentType.MediaType, StringComparison.InvariantCultureIgnoreCase); }
/// <summary> /// Builds the multi part message. /// </summary> /// <param name="entity">The entity.</param> /// <param name="message">The message.</param> private void BuildMultiPartMessage(MimeEntity entity, MailMessageEx message) { foreach (MimeEntity child in entity.Children) { if (string.Equals(child.ContentType.MediaType, MediaTypes.MultipartAlternative, StringComparison.InvariantCultureIgnoreCase) || string.Equals(child.ContentType.MediaType, MediaTypes.MultipartMixed, StringComparison.InvariantCultureIgnoreCase)) { BuildMultiPartMessage(child, message); } //if the message is mulitpart/alternative or multipart/mixed then the entity will have children needing parsed. else if (!IsAttachment(child) && (string.Equals(child.ContentType.MediaType, MediaTypes.TextPlain) || string.Equals(child.ContentType.MediaType, MediaTypes.TextHtml))) { message.AlternateViews.Add(CreateAlternateView(child)); SetMessageBody(message, child); } //add the alternative views. else if (string.Equals(child.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase) && string.Equals(child.ContentDisposition.DispositionType, DispositionTypeNames.Attachment, StringComparison.InvariantCultureIgnoreCase)) { message.Children.Add(ToMailMessageEx(child)); } //create a child message and else if (IsAttachment(child)) { message.Attachments.Add(CreateAttachment(child)); } } }
/// <summary> /// Creates the mail message from entity. /// </summary> /// <param name="entity">The entity.</param> /// <returns></returns> public static MailMessageEx CreateMailMessageFromEntity(MimeEntity entity) { MailMessageEx message = new MailMessageEx(); string value; foreach (string key in entity.Headers.AllKeys) { value = entity.Headers[key]; if (value.Equals(string.Empty)) { value = " "; } message.Headers.Add(key.ToLower(), value); switch (key.ToLower()) { case MailHeaders.Bcc: MailMessageEx.PopulateAddressList(value, message.Bcc); break; case MailHeaders.Cc: MailMessageEx.PopulateAddressList(value, message.CC); break; case MailHeaders.From: message.From = MailMessageEx.CreateMailAddress(value); break; case MailHeaders.ReplyTo: message.ReplyTo = MailMessageEx.CreateMailAddress(value); break; case MailHeaders.Subject: message.Subject = value; break; case MailHeaders.To: MailMessageEx.PopulateAddressList(value, message.To); break; } } return message; }
/// <summary> /// Builds the single part message. /// </summary> /// <param name="entity">The entity.</param> /// <param name="message">The message.</param> private void BuildSinglePartMessage(MimeEntity entity, MailMessageEx message) { SetMessageBody(message, entity); }
public void SendMessage(MailAddress Recipient, string Subject, Attachment att) { try { MailMessageEx mme = new MailMessageEx(); mme.To.Add(Recipient); mme.Subject = Subject; mme.Attachments.Add(att); mme.From = new MailAddress(FromAddress); Client.Send(mme); } catch (Exception ex) { ThrowException(new Exception("Не удалось отправить сообщение", ex)); } }