2017-03-10 6 views
0

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"} 
+0

_「成功していない」_ - [質問]を読んで、何が起こったのかを説明してください。なぜあなたは 'Response.End()'を呼びますか?そのメソッドのドキュメントを読もうとしましたか? – CodeCaster

+0

私のコードを編集しました。 – focus

+0

を確認してください。もうResponse.Endの後でメールを送信しないので、タイトルを変更する必要があります。しかし、何が問題なのですか?メールは送信されませんか?ドキュメントは添付されていませんか? –

答えて

0

エラーの理由は、あなたの代わりにファイルパスのごemail_send方法にDocオブジェクト参照を、渡しているということです。
ドキュメントをMemoryStreamに読んで添付ファイルとして渡すことで、やや似た電子メールソリューションを作成しました。

public void email_send(Document d) 
{ 
    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.IO.MemoryStream ms = new System.IO.MemoryStream(); 
    iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(d, ms); 
    System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf); 
    Attachment attach = new Attachment(ms, ct); 

    mail.Attachments.Add(attach); 

    SmtpServer.Port = 587; 
    SmtpServer.Credentials = new System.Net.NetworkCredential("[email protected]", "mypass"); 
    SmtpServer.EnableSsl = true; 

    SmtpServer.Send(mail); 

    writer.Flush(); 
    writer.Dispose(); 
    ms.Dispose(); 

} 

email_send(Doc)と呼んでください。

+0

こんにちは、電子メールを送信しますが、pdf拡張子を持たないnonameファイルが添付されています。ファイルは0kbです – focus

+0

あなたはこの最新バージョンを使用しましたか?10分ほど前に編集しました。 – Sami

+0

はい、私は添付ファイルとして0KBのnonameファイルを取得しています。 – focus

関連する問題