Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

c# - Failed to parse message headers error in Mimekit

I've encountered an error Failed to parse message headers after executing MimeMessage.Load please see the code below:

public MimeEntity decryptString(string responseString)
       {
            responseString = "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 

" + 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;
            }

Here is the value of responseString before executing 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=

What I wanted to do here is to decrypt the content of a MIME Response in string format from an API to be able to use its data. I'm only new to using MimeKit. If anyone knows how can I do this it will be a great help.

question from:https://stackoverflow.com/questions/65560279/failed-to-parse-message-headers-error-in-mimekit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Header names cannot contain spaces.

MIME - Version: -> MIME-Version:

Content - Disposition: -> Content-Disposition:

Content - Type: -> Content-Type:

Content - Transfer - Encoding: -> Content-Transfer-Encoding:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...