0

I am working in an Exchange 2010 environment, and I am interested in doing the follow:

  • Having a Server-Side rule that takes all emails sent to a particular address, and forwards them to a new address.
  • However, before the email is forwarded, it needs to be re-encoded with a specific charset. In my case, the email is being received in UTF-8 encoding, and I need it to be re-encoded as US-ASCII and then forwarded over.

So the "flowchart" would be:

  1. UTF-8 Email sent to Mailbox-A
  2. Mailbox-A receives UTF-8 email.
  3. Server-side rule on Mailbox-A re-encodes charset on email to US-ASCII
  4. Newly US-ASCII encoded email is forward to Mailbox-B

I've come across several possible solutions: mailbox rule, transport rules, Journal accounts... but none seem the immediately solve my problem. It could be that I'm not investigating this enough.

Any help on this would be greatly appreciated. Thanks!

  • Side note: you can't encode UTF-8 into US-ASCII as UTF-8 can encode all of unicode and US-ASCII can't. You'll need some handling of the missing characters. – Martijn Aug 25 '14 at 21:13
  • That shouldn't be an issue since I'm actually generating (well, sort of) the UTF-8 emails to begin with. Basically the program that generates the emails can encode the characters in many different charsets, but US-ASCII is not one of them. Since I'm creating the body of the email, I know that there won't be any non US-ASCII characters in the email. – romellem Aug 25 '14 at 22:54
  • 1
    In that case, you don't have to do anything since the subset of Unicode that US-ASCII can encode is encoded identically in UTF-8 and US-ASCII. – user2313067 Aug 26 '14 at 4:53
  • That's not strictly true. An email in UTF-8 will have a content-transfer-encoding Base64 and a US-ASCII email generally 7bit (or omitted as that's the default) in its MIME. Whatever the case, the Content-type will be different and will need to be changed. – Martijn Aug 26 '14 at 10:06
0

Exchange can't automatically transcode an email message.

Fortunately, with transport agents, you can script the hell out of exchange. Unfortunately, you're going to have to write the logic yourself. Let's go through it.

You can basically follow the instructions at http://msdn.microsoft.com/en-us/library/office/aa579185%28v=exchg.140%29.aspx

In your flow chart:

  1. You've already got this covered (right?)
  2. BREAK. We're never going to do this. Write a transport agent that changes everything we need to change. On the OnEndOfData event takes a delegate that has as an argument which contains a MailItem that has everything you need. First check if MailItem.Message.To contains your MailBox-A. If it does, change everything you need to change:
    • MailItem.Message.To add Maibox-B (optionally remove MailBox-A)
    • Get the old body mimepart with MailItem.Message.Body.MimePart. Create a new MimePart with the constructor

-

public MimePart(
  string contentType,
  ContentTransferEncoding transferEncoding,
  Stream contentStream,
  CachingMode cachingMode
)

You will need the contentType "text/plain", ContentTransferEncoding of the old ContentTransferEncoding, a new content stream, and CachingMode CachingMode.Copy

Write the contents of the stream of the old MimePart unchanged into the content stream of the new part (since you indicated you will not be using any characters outside of 7bit ASCII anyway, the content is actually identical. If it's not, read the stream as 7bit ascii, base64 decode the resulting string to a byte stream, decode the bystream with UTF-8 encoding to a string, replace what you need to replace, write to a bytestream with 7bit ASCII encoding, base64 encode the resulting bytestream, write the resulting string to the target stream encoding with 7bit ASCII) (phew!).

Replace the old MimePart with the new MimePart. You can use old.Parent.ReplaceChild(old, new).

Compile the thing and attach the dll with the compiled agent to Exchange. Copy-paste from the documentation: Execute Install-TransportAgent -Name "MyCustomAgent" -TransportAgentFactory "MyAgents.MyAgentFactory" -AssemblyPath "C:\myagents\MyAgent.dll" in the Exchange Management Shell.

The adjusted email will now be in 7bit ASCII and have the correct recipient.

Is it possible? Yes. Is this all worth it? Probably not. What silly email clients can't read UTF-8 anyway? I personally wouldn't know of any, and the cost of supporting them is probably much higher than just upgrading the email clients.

If you are going to attempt this (I don't suggest you do) and get lost with the coding part, those questions would be in scope of StackOverflow.

  • Thanks for the detailed response! It actually has to do with a ticketing system. A ticketing system we are using can generate tickets from emails, but it chokes on UTF-8 emails. It only works with US-ASCII and ISO-8859-1 (at least that is what we've seen with our tests). I know, the whole thing is pretty ridic, but I at least have to go back to management and give our options. :) – romellem Aug 26 '14 at 14:48
  • re-engineering the client of the ticketing system (if it's an in-house project) to take UTF-8 rather than ASCII is likely less work than this, and if it's not then, eh, godspeed ;) – Martijn Aug 26 '14 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.