private Attachment CreateAttachment(MimeEntity entity)
{
MemoryStream memoryStream = new MemoryStream(ContentDecoder.DecodeBytes(entity), false);
Attachment attachment = new Attachment(memoryStream, entity.ContentType);
if (entity.ContentDisposition != null)
{
attachment.ContentDisposition.Parameters.Clear();
foreach (string key in entity.ContentDisposition.Parameters.Keys)
{
//Skip values that will be strongly typed copied after this loop: it happens that date string
//will not be parsed correctly when going through Parameters.Add but are already parsed into entity.ContentDisposition
if (key == "creation-date" ||
key == "modification-date" ||
key == "read-date" ||
key == "size" ||
key == "filename")
{
continue;
}
attachment.ContentDisposition.Parameters.Add(key, entity.ContentDisposition.Parameters[key]);
}
attachment.ContentDisposition.CreationDate = entity.ContentDisposition.CreationDate;
attachment.ContentDisposition.DispositionType = entity.ContentDisposition.DispositionType;
attachment.ContentDisposition.FileName = entity.ContentDisposition.FileName;
attachment.ContentDisposition.Inline = entity.ContentDisposition.Inline;
attachment.ContentDisposition.ModificationDate = entity.ContentDisposition.ModificationDate;
attachment.ContentDisposition.ReadDate = entity.ContentDisposition.ReadDate;
attachment.ContentDisposition.Size = entity.ContentDisposition.Size;
}
if (!string.IsNullOrEmpty(entity.ContentId))
{
attachment.ContentId = TrimBrackets(entity.ContentId);
}
attachment.TransferEncoding = entity.ContentTransferEncoding;
return attachment;
}