Search code examples
c#steammailkit

MailKit steam content to string variable C#


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?


Solution

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