itextsharpを使用してpdfを作成しています。 pdfは正常に作成され、ブラウザにポストするので、ユーザはファイルを自分のコンピュータに簡単に保存できます。今私はこの生成されたPDFを自動的に電子メールで送信します。私は文章を文章に転記する前にその文書を変換し、電子メールに添付しますが、成功することはありません。私のコードは次のとおりです。HttpContext.Current.Responseで作成された電子メールの添付ファイルを送信
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=mypdf.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
string imagepath = Server.MapPath(".") + "/assets/myimages/myimage.png";
Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(Doc);
PdfWriter pdfwriter= PdfWriter.GetInstance(Doc, HttpContext.Current.Response.OutputStream);
Doc.Open();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imagepath);
image.ScalePercent(106f,90f);
Doc.Add(image);
//adding elements using itextshart pdf
AddPDf(pdfwriter,Doc);
//to add html in pdf
// htmlparser.Parse(stringReader);
OnEndPage(pdfwriter, Doc);
Doc.Close();
email_send(Doc.ToString());
HttpContext.Current.Response.End();
public void email_send(string filename)
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment((Server.MapPath(filename.ToString())));
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "mypass");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
}
私が手にエラーがある:
Could not find file
{"Could not find file 'C:\\Admin\\iTextSharp.text.Document'.":"C: \\Admin\\iTextSharp.text.Document"}
_「成功していない」_ - [質問]を読んで、何が起こったのかを説明してください。なぜあなたは 'Response.End()'を呼びますか?そのメソッドのドキュメントを読もうとしましたか? – CodeCaster
私のコードを編集しました。 – focus
を確認してください。もうResponse.Endの後でメールを送信しないので、タイトルを変更する必要があります。しかし、何が問題なのですか?メールは送信されませんか?ドキュメントは添付されていませんか? –