2014-03-19 38 views
1

我試圖使用JavaMail附加一個zip文件附加zip文件,並提示以下錯誤:問題在JavaMail的

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

回答

0

我相信這個問題是一些如何與zip文件的內容。我更改了zip文件並且工作正常

0

我想你沒有爲你發送的multipart附件設置MIME類型。嘗試設置它並參見

ZIP文件的標準MIME類型是application/zip。

也可嘗試申請/八位字節流,如果這麼想的工作

+0

我相信問題是一些如何與zip文件的內容有關。我改變了zip文件,它工作正常。感謝您的答覆 ! – Siv

0

請確保網絡訪問正常,問題似乎是網絡訪問權限 首先:嘗試從您的計算機ping通郵件服務器,如果沒問題,您可以看到 其次:嘗試發送簡單的郵件(主題/內容) 第三:嘗試附加簡單的文檔(txt文件)