Exchange 2010/2013 TransportAgent Content Conversion for Internal Recipients

674 views Asked by At

I am trying to create a TransportAgent that reroutes an internal email to a remote server for further processing, but the email comes over in Microsoft's internal TNEF format (no content conversion is being applied). I would like it to be in the same format that would be used if the email was going to an external recipient.

I am currently using a RoutingAgent to reroute the internal emails to the remote server. If someone can explain why this happens in terms of the Categorizer's pipeline, that would be very helpful too, even if I can't avoid it. This agent will be implemented for both Exchange 2010 and 2013, in case the answer differs based on the version of Exchange.

1

There are 1 answers

0
Pascal Martin On

Exemple using reflexion ItemConversion.convertAnyMimeToMimeMethod method

var mimeDocument = new MimeDocument();
using (var mimeDocumentLoadStream = mimeDocument.GetLoadStream())
{
    ConvertAnyMimeToMimeMethod(email.Message.MimeDocument, mimeDocumentLoadStream);
}
var convertedEmail = EmailMessage.Create(mimeDocument);


private static void ConvertAnyMimeToMimeMethod(MimeDocument documentIn, Stream mimeOut)
{
    var assembly = Assembly.Load("Microsoft.Exchange.Data.Storage");
    var outboundConversionOptionsType = assembly?.GetType("Microsoft.Exchange.Data.Storage.OutboundConversionOptions");
    var outboundOptionsConstructor = outboundConversionOptionsType?.GetConstructor(
        BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public,
        null,
        new[] { typeof(string) },
        null);
    var outboundOptions = outboundOptionsConstructor?.Invoke(new object[] { "OurDomain.tld" });
    var itemConversionType = assembly?.GetType("Microsoft.Exchange.Data.Storage.ItemConversion");
    var convertAnyMimeToMimeMethod = itemConversionType?.GetMethod(
        "ConvertAnyMimeToMime",
        BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static,
        null,
        new[] { documentIn.GetType(), mimeOut.GetType(), outboundOptions?.GetType() },
        null);

    convertAnyMimeToMimeMethod.Invoke(null, new[] { documentIn, mimeOut, outboundOptions });
}