无法解析 Mimekit 中的消息 headers 错误
Failed to parse message headers error in Mimekit
执行MimeMessage.Load
后遇到错误Failed to parse message headers
请看下面的代码:
public MimeEntity decryptString(string responseString)
{
responseString = "MIME - Version: 1.0 \n"+
"Content - Disposition: attachment; filename =\'smime.p7m\' \n" +
"Content - Type: application / pkcs7 - mime; smime - type = enveloped - data; name =\'smime.p7m\' \n"+
"Content - Transfer - Encoding: base64 \n\n" + responseString;
byte[] Msg = Encoding.ASCII.GetBytes(responseString);
MemoryStream mm = new MemoryStream(Msg);
MimeMessage responseMessage = MimeMessage.Load(mm);
string filename = HttpContext.Current.Request.PhysicalApplicationPath + "/Certificates/1608104889.txt";
var message = new MimeMessage();
message.Subject = Path.GetFileNameWithoutExtension(filename);
message.Date = DateTimeOffset.Now;
message.Body = responseMessage.Body;
var pkcs7 = message.Body as ApplicationPkcs7Mime;
if (pkcs7 != null && pkcs7.SecureMimeType == SecureMimeType.EnvelopedData)
{
// the top-level MIME part of the message is encrypted using S/MIME
return pkcs7.Decrypt();
}
else
{
// the top-level MIME part is not encrypted
return message.Body;
}
这里是responseString
执行前的值Encoding.ASCII.GetBytes
:
MIME - Version: 1.0
Content - Disposition: attachment; filename ='smime.p7m'
Content - Type: application / pkcs7 - mime; smime - type = enveloped - data; name ='smime.p7m'
Content - Transfer - Encoding: base64
MIIB8AYJKoZIhvcNAQcDoIIB4TCCAd0CAQAxggFAMIIBPAIBADAkMBYxFDASBgNVBAMTC1NpbmFwdElRIENBAgoeg+bBAAAAAAAMMA0GCSqGSIb3DQEBAQUABIIBAIlcT4+v5h69Rh17Edz/6h08PZAG63xfWDw3JkAET0MLqgmGlZTDeUOukLiZuC3Oahy4o3NaWH0LQMGmsaO14HKkxoxsLmMEVCLD2MfJO1seIC2tjQcZBXGWNyYYq4B6cbqYuK3t5KJtLebU8a1ep46tEDoqNRSgeb7+T3/AbMq6K9vi+vkIJ7s/aMY6gHjTbPhaTytZ5EeM4kiwA6mr1E8zUSQ26i6HqdVhxpqyV1AjXrXsZWxD0uTR+QrJzmSlXA9l1ghd5pEyUObvxl8yX2f8KvUW9BKfZYqpzNz060jD2v4v4zih88RYtvrpIs43ZojgMMoq9aWulV9hfZmY9v4wgZMGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIW7kUs2r/kxqAcMy+kMFM9YbnLJynANOlH6/DauuSUncDsqWhgf9fksm/0RYNlwp3qAjbYkxp1DLeR2AUr0ESZxG6mSKIPnRSwcO1wRJnZBBzloyo926naZ1aL+tz3RtNNXXtkNtz9ps4ldxMCrETh6wmiL6L99vpY7s=
我想在这里做的是从 API 中解密字符串格式的 MIME 响应的内容,以便能够使用其数据。我只是刚开始使用 MimeKit。如果有人知道我该怎么做,那将是一个很大的帮助。
Header 名称不能包含空格。
MIME - Version:
-> MIME-Version:
Content - Disposition:
-> Content-Disposition:
Content - Type:
-> Content-Type:
Content - Transfer - Encoding:
-> Content-Transfer-Encoding:
执行MimeMessage.Load
后遇到错误Failed to parse message headers
请看下面的代码:
public MimeEntity decryptString(string responseString)
{
responseString = "MIME - Version: 1.0 \n"+
"Content - Disposition: attachment; filename =\'smime.p7m\' \n" +
"Content - Type: application / pkcs7 - mime; smime - type = enveloped - data; name =\'smime.p7m\' \n"+
"Content - Transfer - Encoding: base64 \n\n" + responseString;
byte[] Msg = Encoding.ASCII.GetBytes(responseString);
MemoryStream mm = new MemoryStream(Msg);
MimeMessage responseMessage = MimeMessage.Load(mm);
string filename = HttpContext.Current.Request.PhysicalApplicationPath + "/Certificates/1608104889.txt";
var message = new MimeMessage();
message.Subject = Path.GetFileNameWithoutExtension(filename);
message.Date = DateTimeOffset.Now;
message.Body = responseMessage.Body;
var pkcs7 = message.Body as ApplicationPkcs7Mime;
if (pkcs7 != null && pkcs7.SecureMimeType == SecureMimeType.EnvelopedData)
{
// the top-level MIME part of the message is encrypted using S/MIME
return pkcs7.Decrypt();
}
else
{
// the top-level MIME part is not encrypted
return message.Body;
}
这里是responseString
执行前的值Encoding.ASCII.GetBytes
:
MIME - Version: 1.0
Content - Disposition: attachment; filename ='smime.p7m'
Content - Type: application / pkcs7 - mime; smime - type = enveloped - data; name ='smime.p7m'
Content - Transfer - Encoding: base64
MIIB8AYJKoZIhvcNAQcDoIIB4TCCAd0CAQAxggFAMIIBPAIBADAkMBYxFDASBgNVBAMTC1NpbmFwdElRIENBAgoeg+bBAAAAAAAMMA0GCSqGSIb3DQEBAQUABIIBAIlcT4+v5h69Rh17Edz/6h08PZAG63xfWDw3JkAET0MLqgmGlZTDeUOukLiZuC3Oahy4o3NaWH0LQMGmsaO14HKkxoxsLmMEVCLD2MfJO1seIC2tjQcZBXGWNyYYq4B6cbqYuK3t5KJtLebU8a1ep46tEDoqNRSgeb7+T3/AbMq6K9vi+vkIJ7s/aMY6gHjTbPhaTytZ5EeM4kiwA6mr1E8zUSQ26i6HqdVhxpqyV1AjXrXsZWxD0uTR+QrJzmSlXA9l1ghd5pEyUObvxl8yX2f8KvUW9BKfZYqpzNz060jD2v4v4zih88RYtvrpIs43ZojgMMoq9aWulV9hfZmY9v4wgZMGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIW7kUs2r/kxqAcMy+kMFM9YbnLJynANOlH6/DauuSUncDsqWhgf9fksm/0RYNlwp3qAjbYkxp1DLeR2AUr0ESZxG6mSKIPnRSwcO1wRJnZBBzloyo926naZ1aL+tz3RtNNXXtkNtz9ps4ldxMCrETh6wmiL6L99vpY7s=
我想在这里做的是从 API 中解密字符串格式的 MIME 响应的内容,以便能够使用其数据。我只是刚开始使用 MimeKit。如果有人知道我该怎么做,那将是一个很大的帮助。
Header 名称不能包含空格。
MIME - Version:
-> MIME-Version:
Content - Disposition:
-> Content-Disposition:
Content - Type:
-> Content-Type:
Content - Transfer - Encoding:
-> Content-Transfer-Encoding: