Question

I want to pass a MailMessage as a parameter to my WCF Web Service. The problem is MailMessage is not serializable and a lot of it's properties, such as MailAddress, are not serializable either. How do I serialize it?

I've seen a tutorial for serializing it by using it as a property in a wrapper class, but that doesn't help me send it over WCF because the MailMessage property doesn't show up on the client side (because it's not serializable). I don't want to just send basic string values (to, from, body). I want to send the entire MailMessage and all its properties. How can I do this?

Update:

I decided it's simply not worth it to use a Web Service and serialize the MailMessage class. The serialization classes did work, but a lot of their methods/constructors weren't available on the WCF client/consumer. So I had to re-write the code to convert from the regular class (e.g. MailMessage) to the serialized class (e.g. SerializedMailMessage) on the client side.

I've decided to create a class library with an EmailHelper and use the calling app's config to determine the SMTP settings.

SmtpSection smtpConfig = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
if (smtpConfig != null) {
    // extract values to your SmtpClient
}
Was it helpful?

Solution

I had to make a Serializeable version of the MailMessage class and make Serializeable classes for each of it's properties (e.g. MailAddress, Attachment...).

Luckily, I found this free open source code (Composite C1 Contrib by burning ice) that has already done all that:

Source Code

License Details

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top