· Nov 15, 2016

Looking for best practice to persist any kind of Email Message through Ensemble

One of my application needs to be able to receive and forward (almost) any kind of Email Message that will be thrown at it. I am able to do that using %Net.MailMessage and %Net.SMTP and %NET.POP3 classes from Caché code.  So far no problem.

The challenge: for a specific reason I want to be able to persist and track those message within ensemble. Now, If I simply use %Net.MailMessage and persist it as a serial object within an ensemble message body,  I get issues, most of them caused by maximum string length definitions of the %Net.MailMessage. So, unfortunately it is not that simple. The MailMessage coming from the POP3 inbound Adapter needs to be serialized, and this serialization needs to be generic enough to handly any kind of mail message and transform it back to a MailMessage to send it via the SMTP outbound Adapter.  I am for example thinking of a rfc822 file export that can be persisted as a stream and be imported back into a %Net.MailMessage.

Has somebody already done that or something similar and can suggest an approach, perhaps share some code to serialize to persist any kind of larger Mail message within an ensemble MessageBody or recommend a simpler solution that would make my work easier?

Discussion (3)1
Log in or sign up to continue

Thanks. Yes, i thought so. I still have not yet decided which approach to take for this

1) Serialize the Mail Message into an RFC822 character stream and write methods to create %Net.MailMessage to save and load a stream (sounds simple, still it is quite a lot of work to write and test this to make it work conforming to the RFC and working with any Email message one can think of)

2) Copy the complete structure of %Net.MailMessage into own classes, remove or extend the string length limitations and perstist this within an ensemble message body object, for example MYPACKAGE.EnsMessage.EMailForwardRequest

I wonder, might there be anything wrong with taking the 2nd approach and removing the string length limitations. If not, why are the string lengths so tightly restricted in the %Net.MailMessage class in the first place? After all, %Net.MailMessage is a serial object. Shouldn't one be able to persist a Mail Message within another persistent object using this class directly?

I was hoping to find a clever way to use the SMTP and POP3 classes to trick them into generating a RFC822 string. After all, they need to be able to send and receive this. But so far no easy success.

%Net.SMTP method Send handles connecting to the SMTP server, extracting all the necessary data from %Net.MailMessage, formatting it for SMTP, and writing it to the server.

If you copy this method and modify it to skip the connecting to server portion and instead have it OPEN and USE a file, then all of the write commands (well, $$$WriteLine commands) will write to a file rather than to the SMTP server.  This might be the simplest way to serialize the %Net.MailMessage