2017-03-10 1 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); 

    } 

오류 : 대신 파일 경로의, 당신의 email_send 방법에 Doc 객체 참조를 전달하는 것이 오류의

Could not find file   

    {"Could not find file 'C:\\Admin\\iTextSharp.text.Document'.":"C: \\Admin\\iTextSharp.text.Document"} 
+0

_ "성공했지만 아무 것도 없습니다"_ - [ask]를 읽고 _does_이 어떻게되는지 설명하십시오. 왜 당신은'Response.End()'를 호출할까요, 그 메소드에 대한 문서를 읽으려고 했습니까? – CodeCaster

+0

내 코드를 편집했습니다. – focus

+0

을 확인하십시오. 이제 Response.End 이후 이메일을 보내지 않으므로 제목을 변경해야합니다. 그러나 문제는 무엇입니까? 이메일이 전송되지 않았습니까? 문서가 첨부되지 않았습니까? –

답변

0

이유입니다.
문서를 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 extention없이 noname 파일이 첨부되어 있습니다. 파일은 0kb입니다. – focus

+0

이 최신 버전을 사용하셨습니까? 10 분 전쯤에 오타를 편집했습니다. – Sami

+0

예, 첨부 파일로 0KB의 noname 파일을 받고 있습니다. – focus