这可能与您的问题有点无关,但我花了 3 天尝试从已签名但未加密的电子邮件。希望这可以帮助处于类似情况的人。以下是在 vb.net 中对我有用的步骤:
- Install Mimekit Nuget 包 https://www.nuget.org/packages/MimeKit/
- 通过查看内容类型和附件名称来正确识别 S/Mime 签名的电子邮件(S/Mime 签名的电子邮件始终附加有 smime.p7m 文件)
If String.Equals(origMessage.Attachments.First.ContentType, "multipart/signed",
StringComparison.OrdinalIgnoreCase) AndAlso
String.Equals(origMessage.Attachments.First.Name, "smime.p7m", StringComparison.OrdinalIgnoreCase) Then
- 将 smime 文件加载为 EWS FileAttachment 并从中创建新的内存流。然后创建该流的 MimeKit.MimeEntity。现在你正在使用 MimeKit 库,它非常适合这个东西
Dim smimeFile As FileAttachment = origMessage.Attachments.First
smimeFile.Load()
Dim memoryStreamSigned As MemoryStream = New MemoryStream(smimeFile.Content)
Dim entity = MimeEntity.Load(memoryStreamSigned)
- 迭代您的 MimeEntity 实例以获取所有附件
If TypeOf entity Is Cryptography.MultipartSigned Then
Dim mltipart As Multipart = entity
Dim attachments As MimeEntity = mltipart(0)
If TypeOf attachments Is Multipart Then
Dim mltipartAttachments As Multipart = attachments
For i As Integer = 0 To mltipartAttachments.Count - 1
If mltipartAttachments(i).IsAttachment Then
**'BOOM, now you're looping your attachment files one by one**
**'Call your decode function to read your attachment as array of Bytes**
End If
Next
End If
End If
- 将附件作为字节数组读取。在上一步的 for 中执行此操作。
'Read and decode content stream
Dim fileStrm = New MemoryStream()
mltipartAttachments(i).Content.DecodeTo(fileStrm)
Dim decodedBytes(0 To fileStrm.Length - 1) As Byte
fileStrm.Position = 0 'This is important because .DecodeTo set the position to the end!!
fileStrm.Read(decodedBytes, 0, Convert.ToInt32(fileStrm.Length))
现在您的附件文件已解码为字节数组,您可以保存它或执行任何您想要的操作:)希望这有帮助!