简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM