javamail添加附件阻止邮件发送

Javamail adding attachment prevent mail to be sent

我正在努力使用 Javamail,我正在尝试发送带有 zip 文件附件的电子邮件。 当我尝试发送不带附件的邮件时,它工作正常,但当我添加 zip 时,邮件不再发送。我没有错误...

我的代码:

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);
    }

我已经尝试了很多东西,我可能已经厌倦了寻找错误xD

感谢帮助!!

Update :感谢 jmehrens 我发现了这个问题。我的邮件服务器不允许 .zip

确保您的邮件服务器没有阻止发送扩展名为 .zip 的电子邮件的策略。您应该能够仅使用邮件客户端(或 JavaMail)进行测试,并将扩展名重命名为 .txt 甚至 .piz.

阅读JavaMail FAQ。它充满了关于最佳实践、调试和故障排除步骤的有用信息。