Hi everybody
I am having issues sending email with an attachment using google app engine javamail api. Without the attachment it works fine, but with the attachment the mail is not delivered, and also there are no exceptions. Please let me no if there are any changes that need to be done to the below code.
try {
Multipart mp = new MimeMultipart();
File file= new File("example.pdf");
FileInputStream fin = new FileInputStream(file);
attachmentData = new byte[(int)file.length()];
fin.read(attachmentData);
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(emailHtml, "text/html");
mp.addBodyPart(htmlPart);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("example.pdf");
// attachment.setContent(attachmentData, "application/html");
DataSource src = new ByteArrayDataSource(attachmentData, "application/pdf");
attachment.setDataHandler(new DataHandler(src));
mp.addBodyPart(attachment);
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("
[email protected]"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(req.getParameter("email"), "Mr. User"));
msg.setSubject("Email Testing By example");
msg.setText(msgBody);
msg.setContent(mp);
System.out.println("I am above Transport.send(msg)");
msg.saveChanges();
Transport.send(msg);
} catch (Exception e) {
req.setAttribute("ERRORMESSAGE", "Error while sending email");
e.printStackTrace();
}
Any help is appreciated.