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

时间:2015-08-18 21:22:46

标签: c# pdf outlook itextsharp

我试图将iTextSharp pdf附加到Outlook MailItem,但实际添加附件始终会导致ArgumentException,附加信息只是说,"抱歉,出了点问题。您可能想再试一次。"

以下是相关代码:

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并将其存储在某处不是一种选择。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

由Paul-Jan在评论中发布,使用Outlook无法附加内存中的pdf。我已切换到MailMessage。