public static Attachment ToAttachment(this string data, string name = "attachment.txt") { var memoryStream = new MemoryStream(); var streamWriter = new StreamWriter(memoryStream); streamWriter.Write(data); streamWriter.Flush(); memoryStream.Position = 0; return new Attachment(memoryStream, name); } }