Matt Dawdy
Matt Dawdy

Reputation: 19707

Is there a way to serialize a .Net MailMessage object

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?

Upvotes: 4

Views: 4910

Answers (2)

Matt Dawdy
Matt Dawdy

Reputation: 19707

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.

Upvotes: 2

Avitus
Avitus

Reputation: 15958

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.

Upvotes: 0

Related Questions