【问题标题】:SendGrid C# sending a pdfSendGrid C# 发送 pdf
【发布时间】:2017-03-14 11:45:07
【问题描述】:

我正在尝试发送使用 Cystal 生成的 pdf:

    rd.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Server.MapPath("~\\Crystal\\TheExp.pdf"));
SendGridEmail em = new SendGridEmail();
var response = em.SendEmail("me@rer.co.uk", "me@rer.co.uk", "Memo", "This is it", Server.MapPath("~\\Crystal\\TheExp.pdf"),"TheExp.pdf");

Stream stream = rd.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);

stream.Seek(0, SeekOrigin.Begin);
return File(stream, "application/pdf", "PaidInvoice" + invNumber.ToString() + ".pdf");

在我包含 pdf 之前,发送电子邮件的代码一直有效

    public  string SendEmail(string to,string from, string subject, string content, string enclosure,string fileName)
    {

        var apiKey = "xx";// System.Environment.GetEnvironmentVariable("SendGridKey");
        var client = new SendGridClient(apiKey);
        var msg = new SendGridMessage();

        msg.AddTo (new EmailAddress(to));
        msg.SetSubject(subject);

        msg.PlainTextContent =content;
        msg.HtmlContent= content;
        msg.From=new EmailAddress(from, "Tyres");

       // this line breaks it
       msg.AddAttachment(fileName, enclosure);

        var response = client.SendEmailAsync(msg).ToString();
        return response;
    }

任何人都有任何想法。 Enclosure 是新生成的 pdf 的路径。 “~\Crystal\TheExp.pdf”。不用线也能正常工作。

乔纳森

【问题讨论】:

    标签: c# pdf sendgrid


    【解决方案1】:

    我只需要做类似的事情。

    我用过

    msg.AddAttachment(filename: fileName + ".pdf", content: file);
    

    两个重点

    1. 确保“文件”是 Base64 字符串
    2. 'fileName' 显示正确的文件格式。否则它将显示为 .dat 文件。

    祝你好运:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      • 2015-03-15
      • 2019-10-24
      • 2023-03-16
      • 2023-04-10
      相关资源
      最近更新 更多