Search code examples
c#.netemailmailmessage

System.Net.Mail.MailMessage Load from a String


I'm implementing an email sending logic. Here is what I'm planing to do.

Whenever system wants to send a email construct the MailMessage object (with all the details) and save that object in database (as string) instead of sending at the same time. From a background process those saved emails will be picked up and processed.

String will be taken from MailMessge.ToString() method.

Now my issue is, I can't see a way to get MailMessage object back from the saved string. I'm not sure whether this is supported by MailMessage class.

My second option is save the raw data in the queued table (to, subject, body etc) and construct the MailMessage object at the time of sending. I would prefer the first option due to it looks much neater.

If someone know how to achieve this, please let me know.

Thanks in advance, Wijitha


Solution

    1. MailMessage.ToString() is not option.

    2. In order to store any object in database or file Class need to be marked with Serializable but sadly in case of MailMessage it is not marked with Serializable attribute.

    3. So you have to go for Second Option.

    If you don't want to go for second option then Make Serializable version of MailMessage class that help you to store data in Database. Later you get that object back and convert to mailmessage and send email.

    https://github.com/keyvan/Gopi/blob/master/Gopi/Gopi/SerializableMailMessage.cs