我尝试使用 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/