![](/img/trans.png)
[英]trying to accept photo as Microsoft chat bot attachment and send it as an email attachment C#
[英]Trying to send csv as an email attachment in C#
当我使用StreamWriter.WriteLine
时,似乎没有向MemoryStream
写入任何内容。
-“数据”是先前填充的内容。 它是一个object [],其中包含具有Name,Date和PhoneNumber的对象。
StreamWriter sw = new StreamWriter(new MemoryStream());
CustomerReport[] customers = (CustomerReport[])data;
foreach (var d in customers)
{
sw.WriteLine($"{d.Name},{d.Date},{d.PhoneNumber}");
}
Attachment attachment = new Attachment(sw.BaseStream, "CustomerReport.csv", "text/csv");
但是, csv
附件始终是空的csv
。
更新:我明白了。 实际上是MemoryStream()
。 我只设置了我的MemoryStream()
,在这种情况下为sw.BaseStream.Postion = 0
下面是一个工作版本:
StreamWriter sw = new StreamWriter(new MemoryStream());
CustomerReport[] customers = (CustomerReport[])data;
foreach (var d in customers)
{
sw.WriteLine($"{d.Name},{d.Date},{d.PhoneNumber}");
sw.Flush();
}
sw.BaseStream.Position = 0;
Attachment attachment = new Attachment(sw.BaseStream, "CustomerReport.csv", "text/csv");
尝试在foreach
之后调用Flush
方法:
sw.Flush();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.