Hi Ulf,
Thank you?, I attached the pdf correctly, but there is one problem. When I open the e-mail at the recipient person, I don't see/download the attached file? Where is the error in my code?:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
try {
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
PdfPTable outerTable = new PdfPTable(1);
Font titleFont = new Font(helvetica, 21, Font.NORMAL);
PdfPCell cell = new PdfPCell(new Paragraph("Address\n\n\n", titleFont));
outerTable.addCell(cell);
cell = new PdfPCell(new Paragraph("Date: " + new java.util.Date()));
outerTable.addCell(cell);
document.add(outerTable);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
//resp.setContentType("application/pdf");
baos.flush();
//Generating an email with the attachment
String to = "bashar@ayyashelectra.com";
String from = "bashar@hotmail.com";
String host = "localhost";
boolean debug = true;
PrintWriter out = response.getWriter();
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
session.setDebug(debug);
try{
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
// msg.setRecipients(Message.RecipientType.CC, address);
msg.setSubject("JAVAMail APIs Multipart Test");
msg.setSentDate(new Date());
//Create and fill the first message
MimeBodyPart mpb1 = new MimeBodyPart();
mpb1.setText(msgText1);
//Create ByteArrayInputStream object
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
//Create and fill the second message part
MimeBodyPart mbp2 = new MimeBodyPart(bais);
//Create multipart
Multipart mp = new MimeMultipart();
mp.addBodyPart(mpb1);
mp.addBodyPart(mbp2);
//add the multipart to the message
msg.setContent(mp);
Transport.send(msg);
}catch (MessagingException mex){
mex.printStackTrace();
Exception ex = null;
if((ex = mex.getNextException()) != null){
ex.printStackTrace();
}
}
//---->
Regards,