2013-02-25 2 views
1

정확히 this 과 동일한 문제가 발생하여 솔루션을 게시하여 문제를 해결할 수있었습니다. 하지만 문제는 첨부 파일을 받았을 때 이름이 없다는 것입니다. 내 방법에서는, 나는 파일에 대한 수신자의 전자 메일 id, 제목, 내용, 파일 이름 및 바이트 []를 요청했습니다. 전달하는 파일의 형식에는 문제가 없지만 문제는 이름과 관련이 있습니다. 수신자는 파일 이름으로 "noname"을 얻습니다. 우리가 선택한 파일 이름을 어떻게 지정해야합니까? 매개 변수로 지나가는 파일 이름이 반영되지 않습니다. 제발 제안 해주세요. 내가 사용하고바이트 배열에서 파일 객체를 생성하는 동안 파일 이름 지정 (실제 파일을 만들지 않음)

코드는

가 이
File file = new File("D:/my docs/Jetty.pdf"); 
int len1 = (int)(file.length()); 
FileInputStream fis1 = null; 
try { 
    fis1 = new FileInputStream(file); 
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} 
byte buf1[] = new byte[len1]; 
try { 
    fis1.read(buf1); 
    EmailServiceClient.sendEmailWithAttachment("[email protected]", "[email protected]", "Hi", "PFA", "Jetty.pdf", buf1); 

    System.out.println("SENT"); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

이에 대한 나의 이메일 서비스 구현 여기

public void sendEmailWithAttachment(String emailIdTo, String emailIdFrom, String subject, String content, 
    final String fileName, byte[] file) { 
MimeMessage message = mailSender.createMimeMessage(); 
try { 
    MimeMessageHelper helper = new MimeMessageHelper(message, true); 
    helper.setTo(emailIdTo); 
    helper.setFrom(emailIdFrom); 
    helper.setSubject(subject); 
    helper.setText(content, true); 
    helper.addInline("attachment", new ByteArrayResource(file) { 
     @Override 
     public String getFilename() { 
      return fileName; 
     } 
    }); 
    mailSender.send(message); 
} catch (MessagingException e) { 
    throw new MailParseException(e); 
}} 

간다 제발 내가 할 수있는 봄 문서에서

답변

1

이것을 알아내는 누군가의 도움 인라인 된 요소에는 contentId 외에 특별한 이름이 없다고 말하십시오. 어쩌면 addAttachment 메서드를 사용하는 대신 첨부 파일을 추가하려고 할 것입니다. 그런 다음 파일 이름을 지정할 수 있습니다. 회신 @toomasr하지만 내 이전 문제에 대한

http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/mail/javamail/MimeMessageHelper.html

+0

덕분에 내가 언급 한 링크를했다. 내 문제는 실제 파일을 만들지 않고 전자 메일에 첨부 파일을 추가하는 것이 었으며 그곳의 솔루션은 그 트릭을 수행했습니다. 하지만 지금받은 전자 메일 첨부 파일에는 이름이 없으며이를 수정하는 방법을 알지 못합니다. –

+0

감사합니다. @toomasr, 모든 것이 작동했습니다! :) –

관련 문제