EWS: extract attachments from signed emails

I have a C # program that manages a resource mailbox, receiving attachments and classifying emails into subfolders. Recently, there was a problem when a client wants to send us signed letters, so when the program extracts their attachments, instead of the attachment files, a file named "smime.p7m" is saved. This file is missing when viewing email in Outlook, only the attachments we want. However, when you go through the code, the attachments listed in the Email object contain only this .p7mfile.

I got mime content from email, but these are just bytes. When I look at the .p7m file in a text editor, I see the contents of the file (s) that I want in the bottom file (the final tease)! How to get the original attachments without having to parse the .p7m file for content of interest?

The exchange server is 2010 SP2, and all this happens with a C # program that uses the EWS managed API.

+4
source share
5 answers

You can use the class EnvelopedCMSto get MIME from an encrypted attachment. Assuming your security context has key access.

byte[] content = ...The byte[] from the smime.p7m attachment ...
var encrypted = new EnvelopedCms();
encrypted.Decode(content);
encrypted.Decrypt();
byte[] unencryptedButRawMimeEntity = encrypted.ContentInfo.Content;

This will allow you to get an unencrypted MIME object (the original message without transport headers).

, , MIME SMIME Type, signed-data. , SignedCMS, . Decrypt .

/ MIME, .

System.Net.Mime, Microsoft - . , . , . , quoted-printable .

MIME, . , - IP.

. NuGet, , . OpenPOP.Net.


, NuGet.

+3

MimeKit (MIME- + S/MIME PGP) MailKit ( SMTP, POP3 / IMAP).

, API- MimeKit GitHub.

+2

OpenPOP.NET, NuGet EWS Managed API, , mime-, .

System.Exchange.WebServices.Data.Item email = new System.Exchange.WebServices.Data.Item(myEmail);
OpenPop.Mime.Message message = new OpenPop.Mime.Message(email.MimeContent.Content);
List<OpenPop.Mime.MessagePart> validMessageParts = message.FindAllAttachments().Where(x => x.FileName.Contains(".csv") == true || x.FileName.Contains(".xlsx") == true || x.FileName.Contains(".xls") == true).ToList<MessagePart>();

foreach (MessagePart messagePart in validMessageParts)
{
  if (messagePart != null)
  {
    using (FileStream fileStream = new FileStream(savingPath + messagePart.ContentDisposition.FileName, FileMode.Create, FileAccess.ReadWrite))
    {
        messagePart.Save(fileStream);
    }
  }
}

csv, xlsx xls .

+1

3 , . vb.net, #. , :

  1. Mimekit Nuget
  2. 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
  1. smime EWS FileAttachment memoryStream. 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)
  1. 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
  1. . .
'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))

, , , :) , !

0

MimeKit.

:

mltipartAttachments(i).Content.DecodeTo(fileStrm)

"" "MimeEntity"

:

Dim mp As MimePart = mltipartAttachments(i)
mp.Content.DecodeTo(fileStrm)
0

All Articles