Skip to content

Attachment names are not always parsed properly from MimeMessage #175

Closed

Description

@jkxyx205

Repo maintainer's note: Summarizing, the issue is that resource names that are wrapped inside <..> are not read back properly, resulting in double extensions.


Hi Benny Bottema,

  1. When I send email with attachment a.txt
EmailBuilder.startingBlank()
 .withAttachment("a.txt", new ByteArrayDataSource("Black Tie Optional", "text/plain"))
  1. but get attachent name a.txt.txt which is not expect.
Email email = EmailConverter.mimeMessageToEmail(message);
if (email.getAttachments().size() > 0) {
                System.out.println(email.getAttachments().get(0).getName());
            }

When I trace source code,I found

  • send email . "Content-ID" default <a.txt>
attachmentPart.setHeader("Content-ID", format("<%s>", resourceName));
  • get attachment name
        @Nonnull
	private static String parseResourceName(@Nullable final String contentID, @Nonnull final String fileName) {
		String extension = "";
		if (!valueNullOrEmpty(fileName) && fileName.contains(".")) {
			extension = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		}
		if (!valueNullOrEmpty(contentID)) { 
                        // execute here
			return (contentID.endsWith(extension)) ? contentID : contentID + extension;
		} else {
			return fileName;
		}
	}

How can i get the right file name 'a.txt'. Look forward to your relay, thank you.

Activity

  1. jkxyx205 commented on Oct 26, 2018

    @jkxyx205
    Author

    I can get real file name by the code below:

    email.getAttachments().get(0).getDataSource().getName();

    My original purpose is to get a mail from inbox, and then use it's attachment to create new mail to send, the file name not right.

    full code blow:

    @Test
    public void forward() throws MessagingException {
        Session session = ServerConfigUtils.getSession();
    
        Store store = session.getStore("imap");
    
        store.connect(Constants.imap_host, Constants.from, Constants.password);
    
        Folder folder = store.getFolder("INBOX");
    
        folder.open(Folder.READ_WRITE);
    
        Message message = folder.getMessage(2);
    
        // get mail from inbox
        Email oldEmail = EmailConverter.mimeMessageToEmail((MimeMessage) message);
    
        // get use old mail content
        Email newEmail = EmailBuilder.startingBlank()
                .from("Rick", Constants.from)
                .to("Ashley", Constants.email)
                .withSubject("转发:this is subject")
                // from old mail
                .withEmbeddedImages(oldEmail.getEmbeddedImages())
                .withAttachments(oldEmail.getAttachments())
                .withHTMLText(oldEmail.getHTMLText())
                .prependTextHTML("this is my own message")
                .buildEmail();
    
    
        Mailer mailer = MailerBuilder
                .withTransportStrategy(TransportStrategy.SMTP)
                .withSMTPServer(Constants.host, 25, Constants.from, Constants.password)
                .buildMailer();
    
    
        mailer.sendMail(newEmail);
    
    }
  2. self-assigned this
    on Oct 27, 2018
  3. bbottema commented on Oct 27, 2018

    @bbottema
    Owner

    This seems like a bug to me. Looking into it now.

    I do have a junit test that checks this functionality (MailerTest.testParser()), so let me check if I'm missing something there.

  4. bbottema commented on Oct 27, 2018

    @bbottema
    Owner

    Ahh, here's what I found in my test code:

    ByteArrayDataSource namedAttachment = new ByteArrayDataSource("Black Tie Optional", "text/plain");
    namedAttachment.setName("dresscode.txt"); // normally not needed, but otherwise the equals will fail
    

    If I remove that second line, the test fails and this is the comparison result:
    image
    So exactly the same as your situation.

    The reason I made this exception in the test is because when creating an attachment with Simple Java Mail, you can override the attachment name, but when parsing a MimeMessage, I cannot determine if this happened, this information is lost. To work around that, in the test I override the attachment name. However, this also hid a bug you discovered.

    Thanks for reporting this. I'm fixing it as we speak.

  5. added a commit that references this issue on Oct 27, 2018
  6. changed the title [-]Get wrong attachment name[/-] [+]Attachment names are not always parsed properly from MimeMessage[/+] on Oct 27, 2018
  7. added this to the 5.0.7 milestone on Oct 27, 2018
  8. bbottema commented on Oct 27, 2018

    @bbottema
    Owner

    Fix released under 5.0.7. Please verify, @jkxyx205.

  9. jkxyx205 commented on Oct 28, 2018

    @jkxyx205
    Author

    The bug has been fixed, thank you very much for high efficient modification. @bbottema

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions