2016-12-02 3 views
1

Java에서 이메일을 보내는 동안 런타임 예외 UnsupportedDataTypeException이 발생합니다. 여기에 예외가 있습니다.Java : 첨부 파일 전자 메일을 보내는 동안 예외 발생

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message; 
    nested exception is: 
    javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; 
    boundary="----=_Part_0_764977973.1480687764115" 

어떻게이 예외를 처리 할 수 ​​있습니까?

나는이 코드 조각을 사용하고 있습니다 : 전체 코드를

public static void main(String[] args) { 

    String senderMail = "[email protected]"; 
    String recepMail = "[email protected]"; 
    String pass = "*********"; 
    String host = "smtp.gmail.com"; 
    String filePath = "C:\\Users\\Inzimam\\Desktop\\helicopter_final.png"; 

    sendJavaMail(senderMail, pass, recepMail, host, filePath); 
} 

private static void sendJavaMail(String senderMail, String pass, String recepMail, String host, String filePath) { 
    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"); 
    // Get the Session object. 
    Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
       @Override 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(senderMail, pass); 
       } 
      }); 
    session.setDebug(true); 
    try { 

     Message message = new MimeMessage(session);    
     message.setFrom(new InternetAddress(senderMail));    
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recepMail));    
     message.setSubject("Subject here");    
     BodyPart messageBodyPart = new MimeBodyPart();    
     messageBodyPart.setText("This is message body");    
     Multipart multipart = new MimeMultipart();    
     multipart.addBodyPart(messageBodyPart);    
     messageBodyPart = new MimeBodyPart(); 


     DataSource source = new FileDataSource(filePath); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     multipart.addBodyPart(messageBodyPart);    
     message.setContent(multipart); 
     SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); 
     t.connect("smtp.gmail.com", senderMail, pass); 
     t.sendMessage(message, message.getAllRecipients()); 
     t.close(); 
//   Transport.send(message); 

     JOptionPane.showMessageDialog(null, "Message has been sent successfully!."); 

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

내가 틀렸다 경우 지적 바랍니다. 감사합니다

+0

정말로 변수 messageBodyPart를 덮어 쓸 필요가 있습니까? – jay

답변

0

처음으로 Javamail API 1.4.6하지만 이제 Javamail API Version 1.5.0 이상으로 위의 동일한 코드가 올바르게 작동합니다. 이제 API 1.5.0을 사용하여 첨부 파일을 성공적으로 보낼 수 있습니다.

편집 : 나는 그것이 작동하지만 API 1.5.0와 이상 우리는 또한

Transport.send(message); 

대신

사용하지 않았다

Transport.send(message); 

을 사용하는 API 1.4.6와

SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); 
      t.connect("smtp.gmail.com", senderMail, pass); 
      t.sendMessage(message, message.getAllRecipients()); 
      t.close(); 
+1

[현재 버전은 JavaMail 1.5.6]입니다 (https://java.net/projects/javamail/pages/Home). –

+0

@BillShannon 예 지금 사용하고 있습니다. –

관련 문제