Reputation: 11
I am trying to send an attachment containing a zip file through javamail. However, while sending it throws an exception as
com.sun.mail.smtp.SMTPSendFailedException: 552-5.7.0 This message was blocked because its content presents a potential 552-5.7.0 security issue.
I added MIME content type as application/zip
but facing
javax.mail.MessagingException: IOException while sending message;
nested exception is:
javax.activation.UnsupportedDataTypeException: no object DCH for MIME type
application/zip at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1167)
Below is my code snippet:
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "application/zip");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
File srcFile = new File(System.getProperty("user.dir")+ "/Reports/");
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(srcFile.getPath()+"/Report.zip");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("Report.zip");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
When I remove the .zip extension it works fine but not with .zip.
Upvotes: 1
Views: 2695
Reputation: 29971
Your code is adding the attachment twice, which I'm sure is not what you want. Replace your code with this:
MimeBodyPart messageBodyPart = new MimeBodyPart();
String srcFile = System.getProperty("user.dir") + "/Reports/Report.zip";
messageBodyPart.attachFile(srcFile, "application/zip", "base64");
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
Upvotes: 1