본문 바로가기
Opensource

[apache-commons-email] 첨부파일 명에 한글 포함될 경우 깨지는 현상

by 무대포 개발자 2020. 7. 11.
728x90
반응형

apache-commons-email 첨부파일 명에 한글 포함될 경우 깨지는 현상

1. Issue

  • Apache-common-email API 를 사용 중 첨부 파일명에 한글 명이 들어갈 경우 첨부파일 명이 깨지는 현상이 발생했습니다.

2. Environment

  • apache-common-email.1.5.jar
  • javax.mail-api.1.6.1.jar

3. 원인 분석

  • 관련하여 Apache-email 소스를 분석한 결과 MultiPartEmail 쪽의 fileName 을 MimeUtility.encodeText(...) 를 사용하지 않을 경우 정상적으로 동작합니다.
    • 테스트 했을 경우, pdf, jpg, text, zip 등 사용하고 있는 파일 확장자에 대해 정상적으로 출력 됩니다.
    • 또한, javax.mail-api.1.6.1 jar 를 사용해서 첨부 파일을 보내면 깨지지 않고 정상적으로 보내 집니다.

    public MultiPartEmail attach(
        final DataSource ds,
        String name,
        final String description,
        final String disposition)
        throws EmailException
    {
        if (EmailUtils.isEmpty(name))
        {
            name = ds.getName();
        }
        final BodyPart bodyPart = createBodyPart();
        try
        {
            bodyPart.setDisposition(disposition);
            bodyPart.setFileName(MimeUtility.encodeText(name)); // 이 부분을 주석처리하거나 옵션 처리하면 파일명 정상 출력
            bodyPart.setDescription(description);
            bodyPart.setDataHandler(new DataHandler(ds));

            getContainer().addBodyPart(bodyPart);
        }
        catch (final UnsupportedEncodingException | MessagingException me)
        {
            // in case the file name could not be encoded
            throw new EmailException(me);
        }
        setBoolHasAttachments(true);

        return this;
    }

  • commons-email 에서 fileName 을 encode 해서 base64 로 filename 이 encoding 됐는데, javax.mail-api 에서 base64 로 인코딩된 fileName 을 한 번 더 handling 하는 과정에 첨부파일 명에 delimiter 가 추가 됩니다.
    • javax.mail 내용을 살펴보니 내부에서 encoding 을 하고 있었습니다. 즉, javax.mail, commons-email 둘다 파일명에 대해 Encoding 을 하고 있습니다.

4. 해결방안

  • apache-common-email 에서는 파일명에 대해 encoding 을 하지 않으면 됩니다. (javax.mail 에서 이미 하고 있기 때문에)
    • 테스트 시, 정상적으로 한글 출력이 됐습니다.
  • 또한, javax-mail 담당자가 답변을 해줬는데 apache-common-email 에서 파일명에 encoding 을 안하면 된다고 합니다.

5. 개선방안 제기

댓글