Nicolas Ould Bouamama
Nicolas Ould Bouamama

Reputation: 273

Javamail adding attachment prevent mail to be sent

I'm struggling with Javamail, I'm trying to send emails with a zip file attached. When I try to send a mail without attachment, it works fine but when I add the zip the mail is no longer sent. I have no errors...

My code :

LOGGER.info("########################### Send Email with attachement to " + destination + "  Start ######################");
    //Config smtp mail
    Properties props = new Properties();
    props.put("mail.smtp.host", getSmtpHost());
    props.put("mail.smtp.socketFactory.port", getSmtpsocketFactoryPort());
    props.put("mail.smtp.socketFactory.class", getSmtpsocketFactoryClass());
    props.put("mail.smtp.auth", getSmtpAuth());
    props.put("mail.smtp.port", getSmtpPort());

    //instance Session
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(getUsername(), getPassword());
        }
    });

    try {
        //construction objet mail
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(getFromAddress()));

        //Send Email to Addresse
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(destination));
        message.setSubject(objet);
        message.setSentDate(new Date());

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(contenu);

        MimeBodyPart  attachmentBodyPart = new MimeBodyPart();
        String fileName = attachementPath + attachementName;
        File file = new File (fileName);
        attachmentBodyPart.attachFile(file); 

        MimeMultipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);
        multipart.addBodyPart(attachmentBodyPart);

        message.setContent(multipart);
        //send Email
        Transport.send(message);
        LOGGER.info("########################### Send email with attachement to " + destination + " End ########################### ");
    } catch (MessagingException e) {
        LOGGER.error("Error when send email to " + destination);
        throw new RuntimeException(e);
    }

I've tryied a lot of things, I may be to tired to find the mistake xD

Thanks for the help !!

Update : Thanks to jmehrens I've found the issue. My mail server doesn't allow .zip

Upvotes: 0

Views: 493

Answers (1)

jmehrens
jmehrens

Reputation: 11045

Make sure your mail server doesn't have a policy in place that prevents the delivery of emails with the extension of .zip. You should be able to test that with just a mail client (or JavaMail) and rename the extension to either .txt or even .piz.

Read the JavaMail FAQ. It is full of good information on best practices, debugging and troubleshooting steps.

Upvotes: 1

Related Questions