C#/ MimeKit:确定附件是否不是签名的最佳方法

时间:2018-10-04 11:14:22

标签: c# digital-signature smime mimekit

我正在使用MailKit(基于MimeKit)发送和接收经过签名和加密的邮件。

我想检查我收到的一封电子邮件中是否只有一个文件附件,该方法我希望该电子邮件中只有一个.txt附件。

我当前的解决方案是这样的:

private string GetContentOfSingleAttachmentFromMail(MimeEntity decryptedEntity)
    {
        if (decryptedEntity == null)
        {
            throw new ArgumentNullException("decryptedEntity");
        }

        var mimeMessage = new MimeMessage(decryptedEntity);

        var attachments = mimeMessage.Attachments.Where(attachment =>
        //signatures don't matter for the attachment count
            !attachment.ContentType.MimeType.Equals("application/pkcs7-signature")).ToList();

        if (attachments.Count() > 1)
        {
            throw new InvalidOperationException(
                "More than one attachment was found.");
        }
        var firstAttachment = attachments.FirstOrDefault();
        if (firstAttachment != null)
        {
            using (var memoryStream = new MemoryStream())
            {
                HandleMimeEntity(firstAttachment, memoryStream);
                return Encoding.UTF8.GetString(memoryStream.ToArray());
            }
        }
        throw new MailMessageGetAttachmentContentException("The message did not contain an attachment");
    }

是否有比检查MimeType更好的方法? (在第12行中) @jstedfast你有个主意吗?

0 个答案:

没有答案