3
votes

I am using SelectPdf to convert a HTML to PDF and sending the PDF in an email without saving it and putting into a MemoryStream, but the email is never sent

If I create an Email without attaching the PDF is always sent.

Here my code:

 public void SendEmail(string htmlBody, string email, string emailBody, string subject)
    {
        PdfDocument doc = null;
        try
        {
            //Reading the html and converting it to Pdf

            HtmlToPdf pdf = new HtmlToPdf();
            doc = pdf.ConvertHtmlString(htmlBodyReservaPasajeros);
            var streamPdf = new MemoryStream(doc.Save()); 

            //creating the message

            message.From = new MailAddress(ConfigurationManager.AppSettings[url + "Email"]);
            message.To.Add(new MailAddress(email));
            message.Subject = subject;
            message.Body = HtmlBody;
            message.IsBodyHtml = true;
            if (doc != null)
            {
                 message.Attachments.Add(new Attachment(streamPdf , "Prueba.pdf", "application/pdf"));
            }
            //Sending the email
            ...

            //Closing
            streamPdf.Close();
            doc.Close(); 
        }            
        catch (Exception e)
        {
        }
    }

UPDATE

I had two problems:

  • First: gmail recognized the email as span, but...

  • Second:Even so I had to write doc.DetachStream() since the pdf was corrupted. This function detach the object memoryStream from PdfDocument and set it free.

in the end the code is the next:

public void SendEmail(string htmlBody, string email, string emailBody, string subject)
    {
        PdfDocument doc = null;
        try
        {
            //Reading the html and converting it to Pdf

            HtmlToPdf pdf = new HtmlToPdf();
            doc = pdf.ConvertHtmlString(htmlBodyReservaPasajeros);
            var streamPdf = new MemoryStream(doc.Save()); 
            **doc.DetachStream();**
            //creating the message

            message.From = new MailAddress(ConfigurationManager.AppSettings[url + "Email"]);
            message.To.Add(new MailAddress(email));
            message.Subject = subject;
            message.Body = HtmlBody;
            message.IsBodyHtml = true;
            if (doc != null)
            {
                 message.Attachments.Add(new Attachment(streamPdf , "Prueba.pdf", "application/pdf"));
            }
            //Sending the email
            ...

            //Closing
            streamPdf.Close();
            doc.Close(); 
        }            
        catch (Exception e)
        {
        }
    }
2
I am not sure, but maybe the extension of doc is not known to the Email and it doesn't send the attachment with unknown type as it thinks it's a "bad file". - Abhishek Kumar
Also, attachment works if the Attachment exists, msdn.microsoft.com/en-us/library/… Do you get any exception? - Abhishek Kumar
No, it doesn't catch any exception - Fran
It is very weird, because you say that you can send it without attachment. Try only message.Attachments.Add(new Attachment(doc) and run it. - Abhishek Kumar
it couldn't be because Attachment(Stream streamObject) doesn't exit - Fran

2 Answers

5
votes

This Code worked for me.. !

public void SendEmail(string htmlBody, string email, string emailBody, string subject)
{
  try
  {

    //Reading the html and converting it to Pdf
    HtmlToPdf pdf = new HtmlToPdf();
    PdfDocument doc = pdf.ConvertHtmlString(htmlBodyReservaPasajeros);

    using (MemoryStream memoryStream = new MemoryStream())
    { 
      doc.Save(memoryStream);

      byte[] bytes = memoryStream.ToArray();

      memoryStream.Close();

      MailMessage message= new MailMessage();
      message.From = new MailAddress(
        ConfigurationManager.AppSettings[url + "Email"]
      );
      message.To.Add(new MailAddress(email));
      message.Subject = subject;
      message.Body = htmlBody;
      message.IsBodyHtml = true;
      message.Attachments.Add(new Attachment(
        new MemoryStream(bytes),
        "Prueba.pdf"
      ));

      //Sending the email
      . . .
    }

    doc.Close(); 
  }            
  catch (Exception e)
  {
    // handle Exception
    . . .
  }

}
1
votes

Check to see of the generated memory stream has the current position at the beginning. You might need to set something like this:

streamPdf.Position = 0;