views:

217

answers:

2

I am trying to write a proc that will take in as a parameter a MailMessage object, and the split it apart to store the subject, body, to addresses, from address, and attachments (the hard part) in a database so the email can be sent at some point in the future.

My first take on this was to rip out the parts I need and store them in a database, and that works great except for attachments. I can't figure out how to loop through the collection and then actually do anything with them.

It there an easy way to serialize a MailMessage object that will actually take the content of the attachments with it?

Am I doing this all wrong? Has anyone done this before?

A: 

If I had to guess the strategy I would use would be for each attachment turn that into a byte array and then put those byte arrays and the message details into an xml document and then pass that xml document to the database as a parameter.

Avitus
Thanks for the advice. Not a bad idea, but I'm more looking for a way to get the file from the actual attachment collection so that we don't have to alter any of our existing codebase. Well, only alteration I'm ok with right now is replacing "smtp.Send(msg);" with "MyClass.Send(msg);" and the MyClass.Send method would actually store this info in the database instead of actually sending it. Then a server process would wake up every 10 minutes and actually do the sending of email.
Matt Dawdy
A: 

There really isn't a good way to do this. So, I continued on with my original method of looping through the MailMessage object and getting all the info I cared about. For attachments, which was the hardest part, each attachment has a ContentStream, and I just read that stream in and wrote it out to disk, stored the filename, and then I can recreate it when I want to actually send it.

I haven't fully tested this method, so I don't recommend it to anyone else yet, but it seems like the best solution in our specific case.

Matt Dawdy