使用SelectPdf .NET将电子邮件附加到电子邮件中,而不保存PDF

时间:2016-03-03 09:24:44

标签: c# html .net email pdf

我正在使用SelectPdf将HTML转换为PDF并在电子邮件中发送PDF而不保存并放入MemoryStream,但电子邮件永远不会发送

如果我在不附加的情况下创建电子邮件,则始终会发送PDF。

这是我的代码:

 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)
        {
        }
    }

更新

我有两个问题:

  • 首先:gmail将电子邮件识别为span,但是......

  • 第二:即便如此,我必须写 doc.DetachStream(),因为pdf已损坏。 此函数将对象memoryStream与PdfDocument分离并将其设置为空闲。

最后代码是下一个:

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 个答案:

答案 0 :(得分:4)

本规范对我有用..!

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 :(得分:1)

检查生成的内存流是否具有开头的当前位置。您可能需要设置如下内容:

streamPdf.Position = 0;