简体   繁体   English

从字符串加载System.Net.Mail.MailMessage

[英]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. 每当系统想要发送电子邮件时,构造MailMessage对象(包含所有详细信息)并将该对象保存在数据库中(作为字符串),而不是同时发送。 From a background process those saved emails will be picked up and processed. 从后台进程中,将提取并处理那些保存的电子邮件。

String will be taken from MailMessge.ToString() method. 字符串将从MailMessge.ToString()方法获取。

Now my issue is, I can't see a way to get MailMessage object back from the saved string. 现在我的问题是,我找不到从保存的字符串取回MailMessage对象的方法。 I'm not sure whether this is supported by MailMessage class. 我不确定MailMessage类是否支持此功能。

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. 我的第二个选择是将原始数据保存在排队的表中(收件人,主题,正文等),并在发送时构造MailMessage对象。 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 在此先感谢Wijitha

  1. MailMessage.ToString() is not option. MailMessage.ToString()不是选项。

  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. 为了将任何对象存储在数据库或文件中,Class需要标记为Serializable,但是不幸的是,对于MailMessage,它未标记为Serializable属性。

  3. So you have to go for Second Option. 因此,您必须选择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. 如果您不想使用第二种方法,则可以使MailMessage类成为可序列化的版本,以帮助您将数据存储在数据库中。 Later you get that object back and convert to mailmessage and send email. 稍后,您将该对象找回并转换为mailmessage并发送电子邮件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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