使用流在邮件中写入 html 内容

发布于 2024-08-02 21:25:16 字数 191 浏览 2 评论 0原文

我有一个加载了从导出中获得的 HTML 的流。我可以从字节[]中的流中获取这个html(还有其他方法吗?),并且我想将此字节[]写入html邮件正文。

这样做的原因是我想将报告导出为 html,并在邮件正文中使用此数据,而不是将其导出为 pdf 并作为附件发送。

希望它足够清楚,如果我需要添加一些内容,请告诉我。

谢谢

I have a stream loaded with HTML that I got from an export. I can take this html from the stream in a byte[] (is there any other way?), and I want to write this byte[] to a html mail body.

The reason to do this is that I want to export a report to html, and use this data in the body of the mail, instead of exporting it to pdf and send it as an attachment.

Hope it's clear enough, let me know if I need to add something.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

风蛊 2024-08-09 21:25:16

我找到了。很简单,虽然我没有找到我想要的结果,直接有一个格式化的输出到邮件,问题更多的是如何使用存储在流中的字节来输出html。

我获取流并创建一个 StreamReader:

            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);

然后,在邮件对象的正文中,我使用 StreamReader 读取流的内容:

mail.Body = reader.ReadToEnd();

仅此而已。你怎么认为?

I found it. It's very simple, although I didn't find the result I would like, having a formatted output directly to the mail, the question was more about how to use the bytes stored in a stream to output html.

I take the stream and create a StreamReader:

            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);

Then, in the body of the mail object, I read the contents of the stream using the StreamReader:

mail.Body = reader.ReadToEnd();

That's all. What do you think?

落叶缤纷 2024-08-09 21:25:16

您可以使用 Multipart Mime 消息并将流传递给 AlternateView 构造函数,只是在发送消息之前不要关闭流或清理它(如果满足以下条件,请调用 MailMessage.Dispose() 来为您处理清理工作)你喜欢)。如果您采用您的解决方案,请务必将 mail.IsBodyHtml 设置为 true,以便正确设置内容类型,否则接收邮件客户端可能不会将 HTML 显示为 HTML,因为它会认为它是纯文本。如果邮件的输出在您的邮件客户端中看起来很有趣,那么这可能就是原因。

代码看起来像这样(仅截断相关部分):

MailMessage message = new MailMessage();
message.AlternateViews.Add(new AlternateView(stream, new ContentType("text/HTML"));
// do NOT set Body, IsBodyHtml, or ContentType on MailMessage or 
// you'll mess up the mime types

smtpClient.Send(message);

You can use a Multipart Mime message and pass the stream to the AlternateView constructor as well, just don't close the stream or clean it up until after you've sent the message (call MailMessage.Dispose() to handle cleanup for you if you like). If you go with your solution, be sure to set mail.IsBodyHtml to true in order for the Content Type to get set correctly otherwise the receiving mail client may not display the HTML as HTML since it will think it's plain text. If the output of the mail looks funny in your mail client then that's probably why.

The code would look something like this (truncated to only the relevant parts):

MailMessage message = new MailMessage();
message.AlternateViews.Add(new AlternateView(stream, new ContentType("text/HTML"));
// do NOT set Body, IsBodyHtml, or ContentType on MailMessage or 
// you'll mess up the mime types

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