none
How do I decode an S/MIME e-mail message with EWS (E2010) RRS feed

  • Question

  • When I get an Email:

                    PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties)
                    itempropertyset.Add(ItemSchema.MimeContent);
                    itempropertyset.Add(ItemSchema.Attachments);
                    itempropertyset.RequestedBodyType = BodyType.Text;
                    src = _Exchange.LoadPropertiesForItems(findMails, itempropertyset);

    and the message was sent with S/MIME, how do I get the Text decoded?

    Assume: EmailMessage em;

    em.Body.Text is null

    em.MimeContent.Content is a byte array, but I need to decode this


    • Edited by TimHoffmann Wednesday, March 20, 2013 3:59 PM
    Wednesday, March 20, 2013 3:59 PM

All replies

  • When I get an Email:

                    PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties)
                    itempropertyset.Add(ItemSchema.MimeContent);
                    itempropertyset.Add(ItemSchema.Attachments);
                    itempropertyset.RequestedBodyType = BodyType.Text;
                    src = _Exchange.LoadPropertiesForItems(findMails, itempropertyset);

    and the message was sent with S/MIME, how do I get the Text decoded?

    Assume: EmailMessage em;

    em.Body.Text is null

    em.MimeContent.Content is a byte array, but I need to decode this


    I also have this problem and doesn't found a solution..

    Any idea?

    Thank's

    Tuesday, June 11, 2013 10:11 AM
  • I have a crude answer.  It's not very robust, but it's working for my environment:

     private static string[] trySmime(EmailMessage em)
            {
                string[] mimeLines = System.Text.UTF8Encoding.UTF8.GetString(em.MimeContent.Content).Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                int ln;
                for (ln = 0; ln < mimeLines.Length; ln++)
                {
                    if (mimeLines[ln] == "MIME-Version: 1.0") break;
                }

                StringBuilder sb = new StringBuilder(em.MimeContent.Content.Length);
                for (int sb1 = ln + 1; sb1 < mimeLines.Length; sb1++)
                    sb.Append(mimeLines[sb1]);

                byte[] b = Convert.FromBase64String(sb.ToString());
                string m1 = System.Text.Encoding.UTF8.GetString(b);
                string[] m2 = m1.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
                int i = 0;
                while (i < m2.Length)
                {
                    if (m2[i].StartsWith("Content-Transfer-Encoding"))
                    {
                        string[] ret = new string[m2.Length - i];
                        int r = 0;
                        for (int z = i + 1; z < m2.Length; z++)
                        {
                            if (m2[z].StartsWith("------=_NextPart")) return ret;
                            ret[r] = m2[z];
                            r++;
                        }
                        return ret;
                    }
                    i++;
                }
                Log.AppendSession(LogLevel.TrapError, _sessionID, "Can't find Content-Transfer-Encoding, " + em.Subject);
                return new string[0];
            }

    Wednesday, June 12, 2013 12:49 PM