我正在使用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)
{
}
}
我不確定,但是'doc'的擴展名對於電子郵件來說是未知的,並且它不會發送未知類型的附件,因爲它認爲它是一個「壞文件」。 –
此外,如果附件存在,附件將工作,https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage.attachments(v=vs.110).aspx您是否遇到任何異常? –
不,它沒有發現任何異常 – Fran