async Task <MimeEntity> CreateAttachmentAsync(ContentType contentType, string path, Stream stream, CancellationToken cancellationToken) { var fileName = GetFileName(path); MimeEntity attachment; if (contentType.IsMimeType("message", "rfc822")) { var message = await MimeMessage.LoadAsync(stream, cancellationToken).ConfigureAwait(false); 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); } await LoadContentAsync(part, stream, cancellationToken).ConfigureAwait(false); 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); }