Can I pass a System.Net.MailMessage to a WCF service?
-
22-07-2019 - |
Question
I'd like to setup a WCF service to send emails. The System.Net.MailMessage doesn't seem to be serializable, and cannot be passed in a [DataContract]
The error I receive is
Type 'System.Net.Mail.MailAddress' cannot be serialized. Consider marking it with the DataContractAttribute
Any suggestions?
Solution
Whatever you pass to a WCF service needs to be either XML or binary serializable.
A "classic" messaging approach would be:
- Create a simple DataContract class that has all the required properties of MailMessage
- Prior to calling the service, set all the properties for the MailMessage in a new instance of your DataContract class
- Call the service, passing the DataContract
- Inside the service, create a new instance of MailMessage and assign the properties into it, then send it
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow