英文:
MailKit steam content to string variable C#
问题
我正在使用C#中的MailKit库来读取包含附件(xml文件)的某些电子邮件;到目前为止,我能够获取带有附件的电子邮件并将其写入磁盘。
using (var stream = File.Create(part.FileName))
part.Content.DecodeTo(stream);
然后,我要读取文件并将其存储到表中。但是,程序将在没有磁盘写入/读取权限的地方执行。
在对内容进行解码后,我应该如何将流内容重定向到字符串变量中?
英文:
I'm using MailKit library with C# to read some emails that contain attachment (xml files); so far i'm able to get the email with the attachement and write it to disk.
using (var stream = File.Create(part.FileName))
part.Content.DecodeTo(stream);
then i was going to read the file and store it into a table. but where the program will be execute will not have grant access to write/read to disk.
how can i redirect the stream content to a string variable after it is Decode?
答案1
得分: 1
你可以将内容写入MemoryStream
,然后在之后解码字符串:
using var stream = new MemoryStream();
part.Content.DecodeTo(stream);
// 假设是UTF-8编码...
string text = Encoding.UTF8.GetString(stream.GetBuffer(), 0, stream.Length);
英文:
You can write to a MemoryStream
, then decode the string afterwards:
using var stream = new MemoryStream();
part.Content.DecodeTo(stream);
// Assuming it's UTF-8...
string text = Encoding.UTF8.GetString(stream.GetBuffer(), 0, stream.Length);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论