java - 在 javamail 中附加 zip 文件时出现问题

标签 java jakarta-mail

我尝试使用 javamail 附加 zip 文件,但收到以下错误:

“com.sun.mail.smtp.SMTPSendFailedException:552-5.7.0 此邮件已被阻止,因为其内容呈现出潜在的威胁 552-5.7.0 安全问题。请访问http://support.google.com/mail/bin/answe 552-5.7.0 r.py?answer=6590 查看我们的邮件内容和附件内容 552 5.7.0 指南。 vb7sm60966875pbc.13 - gsmtp“

附加文档或 xls 没有任何问题。我什至相信附加 zip 文件与任何其他文件没有什么不同。请让我知道这里有什么问题。

如果需要,我还提供了代码。

public class SendMail {

    @Test
    public static void sendFileEmail()
    {
        // Recipient's email ID needs to be mentioned.
        String to = "*****@gmail.com";

        // Sender's email ID needs to be mentioned
        String from = "****@gmail.com";

        // Get system properties
        Properties properties = System.getProperties();

        // Setup mail server
        properties.put("mail.smtp.host", "smtp.gmail.com");
        properties.put("mail.smtp.socketFactory.port", "465");
        properties.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.port", "465");
        properties.put("mail.debug", "false");

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties,
                new javax.mail.Authenticator() 
                {
                    protected PasswordAuthentication getPasswordAuthentication()
                    {
                        return new PasswordAuthentication("*****@gmail.com","****");
                    }
                });

        try {
            // Create a default MimeMessage object.
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(
                    to));

            // Set Subject: header field
            message.setSubject("This is the Subject Line!");

            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();

            // Fill the message
            messageBodyPart.setText("This is message body");

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();
            String filename = "XSLTReports.zip";
            DataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            // Send the complete message parts
            message.setContent(multipart);

            // Send message
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

最佳答案

我猜您尚未为发送的多部分附件设置 MIME 类型。尝试设置一下看看

ZIP 文件的标准 MIME 类型是 application/zip。

如果不起作用,还可以尝试 application/octet-stream

关于java - 在 javamail 中附加 zip 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22503893/

相关文章:

javamail java.io.IOException : Invalid keystore format

Java 多 keystore 检查顺序

java - 使用 javax 在 RCP Java 应用程序中发送带有附件的电子邮件

java - 如何分享listiview的内容(图片+文字)?

Java Key Store 总是以空别名结束

nullpointerexception - JavaMail : Null pointer exeception in BODYSTRUCTURE. parseParameters。是 bug 吗?

java - 即使条件包含指定数据,条件也会变为 false

java - 如何返回结果集?

java - 如何将具有 2 个实体名称的相同类迁移到 Spring Data JPA?

java - 从两个数组中获取非重复项的最佳算法