private void SetContent(bool allowUnicode)
{
//the attachments may have changed, so we need to reset the message
if (_bodyView != null)
{
_bodyView.Dispose();
_bodyView = null;
}
if (AlternateViews.Count == 0 && Attachments.Count == 0)
{
if (!string.IsNullOrEmpty(_body))
{
_bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, (_isBodyHtml ? MediaTypeNames.Text.Html : null));
_message.Content = _bodyView.MimePart;
}
}
else if (AlternateViews.Count == 0 && Attachments.Count > 0)
{
MimeMultiPart part = new MimeMultiPart(MimeMultiPartType.Mixed);
if (!string.IsNullOrEmpty(_body))
{
_bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, (_isBodyHtml ? MediaTypeNames.Text.Html : null));
}
else
{
_bodyView = AlternateView.CreateAlternateViewFromString(string.Empty);
}
part.Parts.Add(_bodyView.MimePart);
foreach (Attachment attachment in Attachments)
{
if (attachment != null)
{
//ensure we can read from the stream.
attachment.PrepareForSending(allowUnicode);
part.Parts.Add(attachment.MimePart);
}
}
_message.Content = part;
}
else
{
// we should not unnecessarily use Multipart/Mixed
// When there is no attachement and all the alternative views are of "Alternative" types.
MimeMultiPart part = null;
MimeMultiPart viewsPart = new MimeMultiPart(MimeMultiPartType.Alternative);
if (!string.IsNullOrEmpty(_body))
{
_bodyView = AlternateView.CreateAlternateViewFromString(_body, _bodyEncoding, null);
viewsPart.Parts.Add(_bodyView.MimePart);
}
foreach (AlternateView view in AlternateViews)
{
//ensure we can read from the stream.
if (view != null)
{
view.PrepareForSending(allowUnicode);
if (view.LinkedResources.Count > 0)
{
MimeMultiPart wholeView = new MimeMultiPart(MimeMultiPartType.Related);
wholeView.ContentType.Parameters["type"] = view.ContentType.MediaType;
wholeView.ContentLocation = view.MimePart.ContentLocation;
wholeView.Parts.Add(view.MimePart);
foreach (LinkedResource resource in view.LinkedResources)
{
//ensure we can read from the stream.
resource.PrepareForSending(allowUnicode);
wholeView.Parts.Add(resource.MimePart);
}
viewsPart.Parts.Add(wholeView);
}
else
{
viewsPart.Parts.Add(view.MimePart);
}
}
}
if (Attachments.Count > 0)
{
part = new MimeMultiPart(MimeMultiPartType.Mixed);
part.Parts.Add(viewsPart);
MimeMultiPart attachmentsPart = new MimeMultiPart(MimeMultiPartType.Mixed);
foreach (Attachment attachment in Attachments)
{
if (attachment != null)
{
//ensure we can read from the stream.
attachment.PrepareForSending(allowUnicode);
attachmentsPart.Parts.Add(attachment.MimePart);
}
}
part.Parts.Add(attachmentsPart);
_message.Content = part;
}
// If there is no Attachement, AND only "1" Alternate View AND !!no body!!
// then in fact, this is NOT a multipart region.
else if (viewsPart.Parts.Count == 1 && string.IsNullOrEmpty(_body))
{
_message.Content = viewsPart.Parts[0];
}
else
{
_message.Content = viewsPart;
}
}
if (_bodyView != null && _bodyTransferEncoding != TransferEncoding.Unknown)
{
_bodyView.TransferEncoding = _bodyTransferEncoding;
}
}