我已经制作了一个jsp程序,当提交表单时,使用itext库创建一个pdf,并以该pdf作为附件发送一封电子邮件,它会创建pdf,但不会附加这个文件,我可以让它发送电子邮件并创建pdf而不附加它,当我摆脱多部分代码,但我不能让它同时做两件事。
现在我已经尝试附上电子邮件根本无法发送的文件,但我猜测可能因为无法找到我想要附加的文件而感到错误我没有发送电子邮件,因为当我删除所有附加文件的代码时,电子邮件发送没有任何问题。有没有人以前经历过这个问题,或者任何人可以提供任何建议,因为我已经搜索了高低的答案,我一遍又一遍地找到相同的帖子,事情已经让我绝望了我已经超越了谷歌搜索的前2页。
<%@page import="javax.activation.DataHandler"%>
<%@page import="javax.activation.FileDataSource"%>
<%@page import="javax.mail.Multipart"%>
<%@page import="javax.mail.internet.MimeMultipart"%>
<%@page import="javax.mail.internet.MimeBodyPart"%>
<%@page import="javax.mail.Address"%>
<%@page import="com.sun.mail.smtp.SMTPTransport"%>
<%@page import="javax.mail.URLName"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.Transport"%>
<%@page import="javax.mail.Session"%>
<%@ page import="java.io.*, com.itextpdf.text.*, com.itextpdf.text.pdf.*" %>
<%
String fileStr = request.getParameter("employeeid") + " Daily Briefing Sheet " + request.getParameter("date") + ".pdf";
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileStr + "\"");
Document document=new Document();
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
document.add(new Paragraph("Daily Briefing Sheet"));
// Employee Data the ID and the dates they entered
document.add(new Paragraph(request.getParameter("employeeid")));
document.add(new Paragraph(request.getParameter("date")));
document.add(new Paragraph(request.getParameter("date2")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Employee Overview of Yesterdays Performance"));
// The Overview of yesterday and today
document.add(new Paragraph(request.getParameter("overview")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Todays Work Plan"));
document.add(new Paragraph(request.getParameter("overviewtoday")));
document.add( Chunk.NEWLINE );
// Measures Table Data
document.add(new Paragraph("Daily Briefing Sheet"));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure1")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay1")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt1")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt1")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma1")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg1")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt1")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya1")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg1")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure2")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay2")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt2")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt2")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma2")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg2")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt2")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya2")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg2")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure3")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay3")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt3")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt3")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma3")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg3")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt3")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya3")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg3")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure4")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay4")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt4")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt4")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma4")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg4")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt4")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya4")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg4")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure5")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay5")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt5")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt5")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma5")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg5")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt5")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya5")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg5")));
// Narratives
document.add( Chunk.NEWLINE );
document.add(new Paragraph("What got left behind yesterday which needs to be rescheduled? When is it to be planned?"));
document.add(new Paragraph(request.getParameter("leftbehind")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any critical issues which need to be discussed or passed on?"));
document.add(new Paragraph(request.getParameter("criticalissues")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("What progress has been made on Projects/Tasks?"));
document.add(new Paragraph(request.getParameter("progress")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any improvement suggestions to be tabled or action sheets to be handed over?"));
document.add(new Paragraph(request.getParameter("improvements")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any quality matters which needs discussed or passed on?"));
document.add(new Paragraph(request.getParameter("qualitymatters")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any internal or external customer complaints which need to be discussed or handed over?"));
document.add(new Paragraph(request.getParameter("complaints")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Is there any information which needs to be shared or handed over?"));
document.add(new Paragraph(request.getParameter("information")));
document.add( Chunk.NEWLINE );
try{
Session mailSession = Session.getInstance(System.getProperties());
Transport transport = new SMTPTransport(mailSession,new URLName("smtp.gmail.com"));
transport = mailSession.getTransport("smtps");
transport.connect("smtp.gmail.com", 465 ,"foobar@gmail.com","mypass");
MimeMessage m = new MimeMessage(mailSession);
m.setFrom(new InternetAddress("foobar1@gmail.com"));
Address[] toAddr = new InternetAddress[] {
new InternetAddress("foobar2@gmail.com")
};
m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
m.setHeader("Content-Type", "multipart/mixed");
m.setSubject("Performance Portal Figures");
m.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("See attached");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
javax.activation.DataSource source = new FileDataSource(fileStr);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileStr);
multipart.addBodyPart(messageBodyPart);
m.setContent(multipart);
transport.sendMessage(m,m.getAllRecipients());
transport.close();
out.println("Thanks for sending mail!");
}
catch(Exception e){
out.println(e.getMessage());
e.printStackTrace();
}
document.close();
%>
答案 0 :(得分:1)
您正在创建发送到浏览器的PDF:
PdfWriter.getInstance(document, response.getOutputStream());
然后您抱怨此PDF没有附加到邮件中。
那......很奇怪。与基本逻辑相反,发送到ServletOutputStream
的字节可以被恢复以将它们发送到邮件。
在你的邮件中,你试图附上fileStr
,这似乎只是一个带有文件名的String
:
String fileStr = request.getParameter("employeeid") + " Daily Briefing Sheet " + request.getParameter("date") + ".pdf";
如果要将PDF发送到浏览器并使用它将PDF附加到邮件中,则需要在内存中创建PDF:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
完全创建PDF后,您可以将PDF发送到ServletOutputStream
,如下所示:
baos.writeTo(response.getOutputStream());
您还可以使用PDF获取byte[]
:
byte[] pdf = baos.toByteArray();
您可以根据需要多次使用这些pdf
,例如:将PDF添加为邮件的附件。
额外评论:我同意san krish的评论,即为此目的使用JSP并不是最理想的。您应该使用Servlet。我很惊讶您的JSP文件实际上是有效的。我很惊讶你没有得到以下错误:
java.lang.IllegalStateException: Already using output stream
答案 1 :(得分:0)
使用你提到的所有工作都可以使用的例子。
感谢下面的代码,我曾经对任何对如何操作感兴趣的人使用过,抱歉我再次使用了jsp,但是这个项目非常紧急,我需要它并运行才能优化它,因为它有对它的要求很高。
<%@page import="javax.activation.DataSource"%>
<%@page import="javax.mail.util.ByteArrayDataSource"%>
<%@page import="javax.activation.DataHandler"%>
<%@page import="javax.activation.FileDataSource"%>
<%@page import="javax.mail.Multipart"%>
<%@page import="javax.mail.internet.MimeMultipart"%>
<%@page import="javax.mail.internet.MimeBodyPart"%>
<%@page import="javax.mail.Address"%>
<%@page import="com.sun.mail.smtp.SMTPTransport"%>
<%@page import="javax.mail.URLName"%>
<%@page import="javax.mail.internet.InternetAddress"%>
<%@page import="javax.mail.internet.MimeMessage"%>
<%@page import="javax.mail.Transport"%>
<%@page import="javax.mail.Session"%>
<%@ page import="java.io.*, com.itextpdf.text.*, com.itextpdf.text.pdf.*" %>
<%
/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
// step 1
Document document = new Document();
// step 2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
// step 3
document.open();
// step 4
document.add(new Paragraph("Daily Briefing Sheet"));
// Employee Data the ID and the dates they entered
document.add(new Paragraph(request.getParameter("employeeid")));
document.add(new Paragraph(request.getParameter("date")));
document.add(new Paragraph(request.getParameter("date2")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Employee Overview of Yesterdays Performance"));
// The Overview of yesterday and today
document.add(new Paragraph(request.getParameter("overview")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Todays Work Plan"));
document.add(new Paragraph(request.getParameter("overviewtoday")));
document.add( Chunk.NEWLINE );
// Measures Table Data
document.add(new Paragraph("Daily Briefing Sheet"));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure1")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay1")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt1")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt1")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma1")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg1")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt1")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya1")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg1")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure2")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay2")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt2")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt2")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma2")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg2")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt2")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya2")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg2")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure3")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay3")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt3")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt3")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma3")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg3")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt3")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya3")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg3")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure4")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay4")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt4")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt4")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma4")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg4")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt4")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya4")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg4")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph(request.getParameter("measure5")));
document.add(new Paragraph("Achieved Yesterday: " + request.getParameter("ay5")));
document.add(new Paragraph("Plan Today: " + request.getParameter("pt5")));
document.add(new Paragraph("Monthly Target: " + request.getParameter("mmt5")));
document.add(new Paragraph("Monthly Achieved: " + request.getParameter("mma5")));
document.add(new Paragraph("Monthly Gap/Over: " + request.getParameter("mmg5")));
document.add(new Paragraph("Yearly Target: " + request.getParameter("myt5")));
document.add(new Paragraph("Yearly Achieved: " + request.getParameter("mya5")));
document.add(new Paragraph("Yearly Gap/Over: " + request.getParameter("myg5")));
// Narratives
document.add( Chunk.NEWLINE );
document.add(new Paragraph("What got left behind yesterday which needs to be rescheduled? When is it to be planned?"));
document.add(new Paragraph(request.getParameter("leftbehind")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any critical issues which need to be discussed or passed on?"));
document.add(new Paragraph(request.getParameter("criticalissues")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("What progress has been made on Projects/Tasks?"));
document.add(new Paragraph(request.getParameter("progress")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any improvement suggestions to be tabled or action sheets to be handed over?"));
document.add(new Paragraph(request.getParameter("improvements")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any quality matters which needs discussed or passed on?"));
document.add(new Paragraph(request.getParameter("qualitymatters")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Are there any internal or external customer complaints which need to be discussed or handed over?"));
document.add(new Paragraph(request.getParameter("complaints")));
document.add( Chunk.NEWLINE );
document.add(new Paragraph("Is there any information which needs to be shared or handed over?"));
document.add(new Paragraph(request.getParameter("information")));
document.add( Chunk.NEWLINE );
// step 5
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
try{
Session mailSession = Session.getInstance(System.getProperties());
Transport transport = new SMTPTransport(mailSession,new URLName("smtp.gmail.com"));
transport = mailSession.getTransport("smtps");
transport.connect("smtp.gmail.com", 465 ,"*******@gmail.com","*********");
MimeMessage m = new MimeMessage(mailSession);
m.setFrom(new InternetAddress("agilegroupforum@gmail.com"));
Address[] toAddr = new InternetAddress[] {
new InternetAddress("foobar@gmail.com")
};
m.setRecipients(javax.mail.Message.RecipientType.TO, toAddr );
m.setHeader("Content-Type", "multipart/mixed");
m.setSubject("Performance Portal Figures");
m.setSentDate(new java.util.Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("See attached");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new ByteArrayDataSource(baos.toByteArray(), "application/pdf");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("test.pdf");
multipart.addBodyPart(messageBodyPart);
m.setContent(multipart);
transport.sendMessage(m,m.getAllRecipients());
transport.close();
out.println("Thanks for sending mail!");
}
catch(Exception e){
out.println(e.getMessage());
e.printStackTrace();
}
%>
感谢Bruno Lowagie