MailKit steam content to string variable C#

huangapple go评论57阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年2月24日 02:40:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定