2
votes

I am trying to send a pdf generated with Cystal:

    rd.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Server.MapPath("~\\Crystal\\TheExp.pdf"));
SendGridEmail em = new SendGridEmail();
var response = em.SendEmail("[email protected]", "[email protected]", "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");

The code that sends the email works until I include the 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;
    }

anyone any ideas. Enclosure is the path to the newly generated pdf. "~\Crystal\TheExp.pdf". It works fine without the line.

Jonathan

1

1 Answers

0
votes

I just had to do something similar.

I used

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

2 important points

  1. Be sure 'file' is a Base64 string
  2. 'fileName' displays the correct file format. Otherwise it'll show up as .dat file.

Good luck :)