简体   繁体   中英

How can serialize and send a MailMessage over a WCF Web Service?

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 (eg MailMessage) to the serialized class (eg 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
}

I had to make a Serializeable version of the MailMessage class and make Serializeable classes for each of it's properties (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM