attach a pdf in an email using SelectPdf .NET without saving PDF
<p>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</p>

<p>If I create an Email without attaching the PDF is always sent.</p>

<p>Here my code:</p>

<pre><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)
        {
        }
    }
</code></pre>

<p><strong>UPDATE</strong></p>

<p>I had two problems:</p>

<ul>
<li><p>First: gmail recognized the email as span, but...</p></li>
<li><p>Second:Even so I had to write <strong>doc.DetachStream()</strong> since the pdf was  corrupted.
This function detach the object memoryStream from PdfDocument and set it free.</p></li>
</ul>

<p>in the end the code is the next:</p>

<pre><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()); 
            **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)
        {
        }
    }
</code></pre>