C#:将内存中的 iTextSharp pdf 作为 Outlook 附件附加

C#: Attach an in-memory iTextSharp pdf as an Outlook attachment

我试图将 iTextSharp pdf 附加到 Outlook MailItem,但实际上添加附件总是导致 ArgumentException,附加信息只是说,"Sorry, something went wrong. You may want to try again."

相关代码如下:

public void SendPDF(string subject, string body, string To)
{
    var pdf = GeneratePDF();

    Outlook.Application mailApp = new Outlook.Application();
    Outlook.MailItem mail = mailApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
    mail.Subject = subject;            
    mail.Body = body;            

    var addresses = To.Split(',');            

    foreach (var address in addresses)
    {
        mail.Recipients.Add(address);
    }

    //error happens here:
    mail.Attachments.Add(pdf, Type.Missing, Type.Missing, EquipmentName + ".pdf");
    mail.Recipients.ResolveAll();

    mail.Send();
}

不附加 pdf 会导致电子邮件发送成功,但显然这没有意义。创建 pdf 并将其存储在某处不是一种选择。任何帮助将不胜感激!

正如 Paul-Jan 在评论中发布的那样,Outlook 无法附加内存中的 pdf。我已切换到 MailMessage。