在 c# EWS 中将 FileAttachment 附加到 message.Attachments

Attach FileAttachment to message.Attachments in c# EWS

我正在尝试向消息中添加 FileAttachment。不幸的是 message.Attachments.AddFileAttachment() 要求提供路径,但我没有。我有 FileAttachment 直接来自另一封邮件。

这里是附件:

EmailMessage oldEmailMessage = EmailMessage.Bind(service, olderSoxMail.Id);
// load attachments
FileAttachment fileAttachment = newEmailMessage.Attachments[0] as FileAttachment;
fileAttachment.Load();

这里我尝试将附件附加到邮件中

public static void ComposeEmail(string recipient, string subject, string body, FileAttachment[] attachment = null)
{
    var map = new Dictionary<char, string>() {
        { 'ä', "ae" },
        { 'ö', "oe" },
        { 'ü', "ue" },
        { 'Ä', "Ae" },
        { 'Ö', "Oe" },
        { 'Ü', "Ue" },
        { 'ß', "ss" }
    };
    recipient = recipient.Aggregate(
      new StringBuilder(),
      (sb, c) =>
      {
          string r;
          if (map.TryGetValue(c, out r))
              return sb.Append(r);
          else
              return sb.Append(c);
      }).ToString();

    EmailMessage message = new EmailMessage(service);
    message.Subject = subject;
    message.Body = body;
    message.ToRecipients.Add(recipient);

    foreach (FileAttachment attach in attachment)
    {
        // COMPILER ERROR HERE: Cannot Convert from FileAttachment to Path (string)
        message.Attachments.AddFileAttachment(attach);
        // END ERROR
    }

    message.SendAndSaveCopy();
}

你知道我如何添加 FileAttachment 吗?

现在您收到的文件附件存在于 Exchange 服务器上。您可以下载这些附件并存储路径并将这些路径附加到您的新电子邮件中。

更好的解决方案是将附件作为流读取并将它们直接附加到邮件,而不先将它们保存到磁盘。

foreach (var fileAttachment in fileAttachments)
{
    var stream = new System.IO.MemoryStream(fileAttachment.Content);
    message.Attachments.AddFileAttachment(fileAttachment.Name, stream);
}