itext sharp在使用c#asp.net發送電子郵件時創建了損壞的PDF,但在本地服務器上下載時正在使用localhost。在存儲流中附加PDF到郵件會導致PDF損壞
using (MemoryStream ms = new MemoryStream())
{
try
{
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, ms);
writer.CloseStream = false;
document.Open();
Font fnt = FontFactory.GetFont("Times New Roman", 12);
PdfPTable PdfTable = new PdfPTable(1);
var path = Server.MapPath(@"~/image.png");
iTextSharp.text.Image logopng =
iTextSharp.text.Image.GetInstance(path);
document.Add(logopng);
logopng.ScaleAbsolute(120f, 155.25f);
logopng.SpacingBefore = 10f;
logopng.SpacingAfter = 1f;
logopng.Alignment = Element.ALIGN_LEFT;
string dtt = todaydate.Text;
PdfPCell dtt1 = new PdfPCell(new Phrase(new Chunk(dat + dtt)));
PdfTable.AddCell(dtt1);
string buy;
buy = "Hello World";
string frst = first_name.Text;
PdfPCell frst1 = new PdfPCell(new Phrase(new Chunk(buy + frst)));
PdfTable.AddCell(frst1);
string city = city_add.Text;
PdfPCell city1 = new PdfPCell(new Phrase(new Chunk(city)));
PdfTable.AddCell(city1);
document.Add(PdfTable);
document.Close();
writer.Close();
sendmail(ms);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=payment agreement.pdf");
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
catch
{
ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('Mail not sent');", true);
}
}
}
private void sendmail(Stream ms)
{
string FromMail = "[email protected]";
string ToMail = email_id.Text;
string Subject = "Payment Agreement";
string Body = "Dear Customer ";
using (Attachment att = new Attachment(ms, "payment agreement.pdf", MediaTypeNames.Application.Pdf))
{
using (MailMessage mm = new MailMessage(
FromMail, ToMail, Subject, Body))
{
mm.Attachments.Add(att);
SmtpClient smtp = new SmtpClient();
smtp.Host = "server name";
smtp.Port = 25;
smtp.Send(mm);
}
}
}
謝謝你,這是工作,肯定會記得以後的帖子, –
這是自接受解決了這個問題的答案。點擊左側樂譜旁邊的複選標記即可完成接受。通過接受答案,您還向其他人顯示問題已正確回答。 –