dotnetnuke2sxc

2sxc send e-mail with the pdf file generated in the app-tutorial-pdf


I need to improve the pdf demo app to send the pdf as an attachment.

The app-tutorial-pdf generates the file with:

[AllowAnonymous]
public class PdfController : Custom.Hybrid.Api14
{
  public static string generatorUrlPrefix = "/pdf/generator";

  [HttpGet]
  public async Task<dynamic> Document(int episode)
  {
    var cultureCode = CmsContext.Culture.CurrentCode.Substring(0, 2);

    var pdfService = CreateInstance("../Services/PdfService.cs");
    var printUrl = pdfService.AddPrintToken(CmsContext.Site.Url + generatorUrlPrefix + "/document?" + "episode=" + episode);

    byte[] pdfStream = await pdfService.GetWebPageAsPdf(printUrl);

    HttpContext.Current.Response.ContentType = "application/pdf";
    HttpContext.Current.Response.AddHeader("Content-Disposition", "filename=" + episode + ".pdf");
    HttpContext.Current.Response.BinaryWrite(pdfStream);
    HttpContext.Current.Response.End();

    return pdfStream;
  }
}

Let's say I need to pick that file and use DNN Mail.SendMail feature to send it. How can this be achieved?

string xmailFrom = "something@test.me";
string xmailTo = "something@test.me";
string xcc = "";
string xbcc = "";
string xreplyTo = "";
string xsubject = "TestSubject";
string xbody = "TestBody";
string[] xattachments = [I need to place the pdfStream result here];
string xsmtpServer = "";
string xsmtpAuthentication = "";
string xsmtpUsername = "";
string xsmtpPassword = "";


Mail.SendMail(xmailFrom, xmailTo, xcc, xbcc, xreplyTo, MailPriority.Normal, xsubject, MailFormat.Html, System.Text.Encoding.UTF8, xbody, xattachments, xsmtpServer, xsmtpAuthentication, xsmtpUsername, xsmtpPassword, DotNetNuke.Entities.Host.Host.EnableSMTPSSL);

Storing the pdf in the server would also work.


Solution

  • This will need some experimentation, but the gist is this:

    byte[] pdfStream = await pdfService.GetWebPageAsPdf(printUrl);
    

    this puts all the bytes into pdfStream. You should now either save it to file, or convert this into the format needed by the Mail.SendMail(...) (not sure what this is).

    To save it, google something like c# save byte array to file and you should be good to go.

    To convert it to the format your Mail.SendMail uses, you'll have check what that API expects.