2016-05-06 11 views
0

このメール機能は、添付ファイルとして.html、.txtなどのすべての種類のファイルに対して機能します。しかし、添付ファイルとして.zipフォルダを送信しようとすると、終了します。javaでメール機能を送信しません。添付ファイルとして.zipを送信します。

public void sendmail(){ 
     System.out.println("started function"); 
     String from = "[email protected]"; 
      // Sender's email ID needs to be mentioned 
      String to = "[email protected]"; 

      final String username = "amritharajeevan.77";//change accordingly 
      final String password = "vivekrajeevan";//change accordingly 

      // Assuming you are sending email through gmail.com 
      String host = "smtp.gmail.com"; 

      Properties props = new Properties(); 
      props.put("mail.smtp.auth", "true"); 
      props.put("mail.smtp.starttls.enable", "true"); 
      props.put("mail.smtp.host", host); 
      props.put("mail.smtp.port", "25"); 
      Session session = Session.getInstance(props, 
       new javax.mail.Authenticator() { 
        protected javax.mail.PasswordAuthentication getPasswordAuthentication() { 
        return new javax.mail.PasswordAuthentication(username, password); 
        } 
       }); 

      try { 
       Message message = new MimeMessage(session); 
       message.setFrom(new InternetAddress(from)); 
       message.setRecipients(Message.RecipientType.TO, 
        InternetAddress.parse(to)); 
       message.setSubject("Server Error"); 
       BodyPart messageBodyPart = new MimeBodyPart(); 
       messageBodyPart.setText("Server"+" " +servererror+ " " +" is unreachable. Please check the logs."); 
       Multipart multipart = new MimeMultipart(); 
       multipart.addBodyPart(messageBodyPart); 
       messageBodyPart = new MimeBodyPart(); 
       String filename = "/Users/aricent/Desktop/amritha/seetest/May6.zip"; 
       DataSource source = new FileDataSource(filename); 
       messageBodyPart.setDataHandler(new DataHandler(source)); 
       messageBodyPart.setFileName(filename); 
       multipart.addBodyPart(messageBodyPart); 
       message.setContent(multipart); 
       System.out.println("sent start function"); 
       Transport.send(message); 

       System.out.println("Sent message successfully...."); 

      } catch (MessagingException e) { 
       throw new RuntimeException(e); 
      } 
     } 

だから私は何をすべきか分かりません。私のコードのどの部分を変更すべきか教えてください。私は.txtと.htmlファイルを試しました。それはうまく動作します。

+0

例外はありますか?その場合は、stacktrace情報を追加してください。 – Fildor

+0

添付ファイルのMIMEタイプを設定する必要があると思います。 zipファイルには適切である必要があります。正確なフォーマットを覚えておいてください。しかし、どこかでウェブ上で動くでしょう。 – jr593

答えて

0

このコードを多く整理して添付ファイルを簡単に作成できるApache Commons Emailプロジェクトをチェックアウトします。

// Create the attachment 
    EmailAttachment attachment = new EmailAttachment(); 
    attachment.setPath(zipFile); 
    attachment.setDisposition(EmailAttachment.ATTACHMENT); 
    attachment.setDescription("Zip File"); 
    attachment.setName("myfiles.zip"); 

    MultiPartEmail email = new MultiPartEmail(); 
    email.attach(attachment); 
関連する問題