有没有办法序列化 .Net MailMessage 对象
我正在尝试编写一个过程,它将接受 MailMessage 对象作为参数,并将其分开以将主题、正文、收件人地址、发件人地址和附件(困难部分)存储在数据库中,以便电子邮件可以将在将来的某个时间发送。
我对此的第一个想法是撕掉我需要的部分并将它们存储在数据库中,除了附件之外,这效果很好。我不知道如何循环遍历集合,然后实际对它们执行任何操作。
有没有一种简单的方法来序列化 MailMessage 对象,该对象实际上会携带附件的内容?
难道我做的这一切都是错的吗?以前有人这样做过吗?
I am trying to write a proc that will take in as a parameter a MailMessage object, and the split it apart to store the subject, body, to addresses, from address, and attachments (the hard part) in a database so the email can be sent at some point in the future.
My first take on this was to rip out the parts I need and store them in a database, and that works great except for attachments. I can't figure out how to loop through the collection and then actually do anything with them.
It there an easy way to serialize a MailMessage object that will actually take the content of the attachments with it?
Am I doing this all wrong? Has anyone done this before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我不得不猜测我将使用的策略是,对于每个附件,将其转换为字节数组,然后将这些字节数组和消息详细信息放入 xml 文档中,然后将该 xml 文档作为参数传递到数据库。
If I had to guess the strategy I would use would be for each attachment turn that into a byte array and then put those byte arrays and the message details into an xml document and then pass that xml document to the database as a parameter.
确实没有什么好的方法可以做到这一点。因此,我继续使用原来的方法,循环遍历 MailMessage 对象并获取我关心的所有信息。对于附件,这是最难的部分,每个附件都有一个 ContentStream,我只需读取该流并将其写入磁盘,存储文件名,然后当我想要实际发送它时可以重新创建它。
我还没有完全测试这个方法,所以我还没有向其他人推荐它,但它似乎是我们特定情况下的最佳解决方案。
There really isn't a good way to do this. So, I continued on with my original method of looping through the MailMessage object and getting all the info I cared about. For attachments, which was the hardest part, each attachment has a ContentStream, and I just read that stream in and wrote it out to disk, stored the filename, and then I can recreate it when I want to actually send it.
I haven't fully tested this method, so I don't recommend it to anyone else yet, but it seems like the best solution in our specific case.