2016-03-03 38 views
2

我正在使用SelectPdf將HTML轉換爲PDF並在電子郵件中發送PDF而不保存並放入MemoryStream中,但電子郵件從未發送過在使用SelectPdf .NET的電子郵件中附加PDF而不保存PDF

如果我創建的電子郵件沒有附加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) 
     { 
     } 
    } 

UPDATE

我有兩個問題:

  • 第一:Gmail的認識到電子郵件作爲跨度,但...

  • 第二:即使如此,我不得不寫doc.DetachStream()由於pdf已損壞。 該函數從PdfDocument中分離出對象的memoryStream並將其設置爲空閒。

到底

代碼是下一個:

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

我不確定,但是'doc'的擴展名對於電子郵件來說是未知的,並且它不會發送未知類型的附件,因爲它認爲它是一個「壞文件」。 –

+0

此外,如果附件存在,附件將工作,https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments(v=vs.110).aspx您是否遇到任何異常? –

+0

不,它沒有發現任何異常 – Fran

回答

1

檢查以查看生成存儲器流的在開頭的當前位置。您可能需要設置如下:

streamPdf.Position = 0; 
3

此代碼爲我工作..!

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); 
      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; 
         mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "Prueba.pdf")); 

         //Sending the email 
         ... 
        } 




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

     } 
} 
+0

請添加一些指向你的答案,而不是隻發佈代碼。 – surajsn

相關問題