2013-09-21 3 views
0

자바 메일 API를 사용하여 이메일에 첨부 파일을 첨부하고 있지만 프로그램을 실행하면 텍스트 파일 내용이 복사되어 메시지 본문에 삽입됩니다. 첨부 파일 대신자바 메일을 사용하여 텍스트 파일이 첨부 파일로 첨부되지 않음

public class EmailCMSUsers { 

    public static void main(String args[]) 
    { 

     Properties props = new Properties(); 
     props.put("mail.smtp.host", "10.10.55.11"); 
     props.put("mail.smtp.port", "25"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     // To see what is going on behind the scene 
     //props.put("mail.debug", "true"); 
     props.put("mail.from", "[email protected]"); 
     Session session = Session.getInstance(props, null); 

     try { 
      MimeMessage msg = new MimeMessage(session); 
      msg.setFrom(); 
      msg.setRecipients(Message.RecipientType.TO,"[email protected]"); 
      msg.setSubject("JavaMail hello world example"); 
      msg.setSentDate(new Date()); 
      msg.setText("Hello, world!\n"); 

      //Transport.send(msg); 
      msg.saveChanges(); 



      /** 
      * for attaching the documents 
      */ 

     // 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 = "D:\\Deployment Docs\\Document.txt"; 
      DataSource source = new FileDataSource(filename); 
      messageBodyPart.setDataHandler(new DataHandler(source)); 
      messageBodyPart.setDisposition(Part.ATTACHMENT); 
      messageBodyPart.setFileName(filename); 
      multipart.addBodyPart(messageBodyPart); 

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





      Transport transport = session.getTransport("smtp"); 
      transport.connect("10.10.55.11", "test123", "test123"); 
      transport.sendMessage(msg, msg.getAllRecipients()); 
      transport.close(); 


      System.out.println(" email sucessfully sent"); 
     } catch (MessagingException mex) { 
      System.out.println("send failed, exception: " + mex); 
     } 
    } 

} 
+0

setDataHandler, setDisposition 및 setFileName을 호출하는 대신 [[attachFile'] (http://docs.oracle.com/javaee/6/api/javax/mail/internet/MimeBodyPart.html#attachFile%28java)를 호출 해보십시오. .lang.String % 29). – VGR

답변

0

첨부 파일이 메시지 본문에 삽입되는 이유는 무엇이라고 생각하십니까? 아마도 메일 본문이 메시지 본문에있는 것처럼 첨부 파일 으로 표시합니까?

0

난 당신의 코드를 실행 한 당신이이 줄을 제거하면 잘 작동 : "D:\\Deployment Docs\\Document.txt" 그러나

msg.saveChanges(); 

를이 코드로, 메일로받은 첨부 파일의 이름은 경로를 포함 할 것이다. 당신이 피하기 만 (경로 제외 : "Document.txt") 정식 이름으로 파일을 전송하려면 다음과 같이 당신은 그것을 할 수 있습니다 :

변경이 : 이것에 대한

String filename = "D:\\Deployment Docs\\Document.txt"; 
DataSource source = new FileDataSource(filename); 
//... 
messageBodyPart.setFileName(filename); 

를 :

File file = new File("D:\\Deployment Docs\\Document.txt"); 
DataSource source = new FileDataSource(file); 
//... 
messageBodyPart.setFileName(file.getName()); 

희망이 있습니다.

관련 문제