有没有办法序列化 .Net MailMessage 对象

发布于 2024-08-29 21:49:29 字数 270 浏览 6 评论 0原文

我正在尝试编写一个过程,它将接受 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2024-09-05 21:50:05

如果我不得不猜测我将使用的策略是,对于每个附件,将其转换为字节数组,然后将这些字节数组和消息详细信息放入 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.

夏尔 2024-09-05 21:49:57

确实没有什么好的方法可以做到这一点。因此,我继续使用原来的方法,循环遍历 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文