MimeEntity CreateAttachment(ContentType contentType, string path, Stream stream)
{
var fileName = GetFileName(path);
MimeEntity attachment;
if (contentType.IsMimeType("message", "rfc822"))
{
var message = MimeMessage.Load(stream);
attachment = new MessagePart {
Message = message
};
}
else
{
MimePart part;
if (contentType.IsMimeType("text", "*"))
{
// TODO: should we try to auto-detect charsets if no charset parameter is specified?
part = new TextPart(contentType);
}
else
{
part = new MimePart(contentType);
}
LoadContent(part, stream);
attachment = part;
}
attachment.ContentDisposition = new ContentDisposition(linked ? ContentDisposition.Inline : ContentDisposition.Attachment);
attachment.ContentDisposition.FileName = fileName;
attachment.ContentType.Name = fileName;
if (linked)
{
attachment.ContentLocation = new Uri(fileName, UriKind.Relative);
}
return(attachment);
}