如何添加从一个字节数组电子邮件附件? [英] How to add an email attachment from a byte array?

查看:140
本文介绍了如何添加从一个字节数组电子邮件附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节[] 与文件的内容。我想送它作为使用 System.Net.Mail 附件。

I have a byte[] with the contents of file. I would like to send it as an attachment using System.Net.Mail.

我注意到附件类有1过载,它接受一个流。

I noticed the attachment class has 1 overload which accepts a stream.

Attachment att = new Attachment(Stream contentStream,string name);

是否有可能通过字节[] 通过这个过载

Is it possible to pass the byte[] through this overload?

推荐答案

最简单的方式:

Attachment att = new Attachment(new MemoryStream(bytes), name);

请注意,除非你做时髦的东西与异步操作,的MemoryStream 安全后再离开未予处置,这可能使生活变得更容易。诚然,没有任何的保证的,这将在未来的是事实,但我认为这是不可能的,它会改变。我看不到任何迹象显示其流是否附着废弃处置:(

Note that unless you do funky stuff with asynchronous operations, MemoryStream is safe to leave undisposed, which may make life easier for you. Admittedly there's no guarantee that that will be true in the future, but I think it's unlikely that it will change. I can't see any indication as to whether disposing of the attachment disposes of its stream :(

这篇关于如何添加从一个字节数组电子邮件附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆