我有一个需要解析电子邮件的应用程序,该电子邮件可以加密也可以不加密。对于加密的电子邮件,我的整个流程工作正常,但是未加密的电子邮件给我带来了麻烦,因为一旦我为自己的工作解密了电子邮件,我想使用相同的代码。当电子邮件未加密时,我有一个MimeMessage,我需要以某种方式将其转换为MimeEntity,以使其通过为加密消息编写的解析代码。
这是我的代码示例,请注意
var pkcs7 = message.BodyParts.OfType<ApplicationPkcs7Mime>().FirstOrDefault();
MimeEntity decrypted;
if (pkcs7 != null)
{
decrypted = decryptAndValidateEmail(pkcs7, certLocation, certPassword);
}
else
{
//********************//
//THIS is the code that is not working at this point
decrypted = message.BodyParts as MimeEntity;
//********************//
}
var decryptedParts = new List<MimePart>();
if (decrypted is Multipart)
{
decryptedParts = breakMultiPart((Multipart)decrypted);
}
else if (decrypted is MimePart)
{
decryptedParts.Add((MimePart)decrypted);
}
else
{
throw new InvalidOperationException("Unknown Mime part found");
}
var textParts = decryptedParts.Where(r => r is TextPart);
var htmlParts = textParts.Where(x => ((TextPart)x).IsHtml);
var textBodyParts = textParts.Where(x => !((TextPart)x).IsHtml);
var attachmentParts = decryptedParts.Where(r => !(r is TextPart));
if (htmlParts.Any())
{
if (htmlParts.Count() > 1)
{
throw new InvalidOperationException("multiple html body parts.");
}
var htmlPart = (TextPart)htmlParts.First();
...
答案 0 :(得分:1)
您想要的是什么
decrypted = message.Body as MimeEntity;
BodyParts
属性是IEnumerable<MimeEntity>
,这就是为什么将其强制转换为MimeEntity
的原因。