【问题标题】:Create attachment from the stream of the data从数据流创建附件
【发布时间】:2015-01-28 20:50:47
【问题描述】:

我正在尝试在 Azure 上的 C# 中即时创建 SendGrid 电子邮件附件。如何更正以下代码,其中someString 是一个变量,其中包含直接从数据库中检索到的附件 xml 内容?

using (var attachmentFileStream = new FileStream(@someString, FileMode.Open))
{
    message.AddAttachment(attachmentFileStream, "someattachmentname.xml");
}

它似乎不喜欢它。我收到以下错误:

  • 路径中有非法字符。访问路径
  • “D:\Windows\system32-1”被拒绝。

【问题讨论】:

  • message的类型是什么?
  • var message = new SendGridMessage();
  • 嗯。那么someString 里面是什么?
  • 转换成MemoryStream的字符串

标签: c# sendgrid


【解决方案1】:

好的...我想我想通了。当我将流类型传递给方法时,我需要剥离“使用(var attachmentFileStream ...”包装并将其直接传递到 myMessage.AddAttachment 即:

// convert to Stream type not MemoryStream!
byte[] byteArray = Encoding.UTF8.GetBytes(someString);

Stream theStream = new MemoryStream(byteArray);

myMessage.AddAttachment(theStream, "someattachmentname.xml"); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2012-09-10
    • 1970-01-01
    • 2018-12-05
    • 2020-04-25
    • 1970-01-01
    相关资源
    最近更新 更多