简体   繁体   中英

Commons email wrong attachment name

I'm using commons-email-1.2.jar and I have the issue with the attachment name.

private static final String XML_ATTACHMENT_FILE_NAME = "Data.xml";
...
email.attach(ds, XML_ATTACHMENT_FILE_NAME, description, EmailAttachment.ATTACHMENT);

The problem is that the attachment name is not "Data.xml" but something else related to the media name stored on the database, somehow related to the description + file extension.

Did somebody else face this issue?

UPDATE: On Windows (Outlook) it's working properly but the problem is on Mac.

I'm not sure about the problem here without having logs in place, but you could try the verbose approach:

// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("foo/foo.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture");
attachment.setName("Foo");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("foo.myserver.com");
email.addTo("foo@bar.com", "Foo Bar");
email.setFrom("example@example.com", "Example");
email.setSubject("Picture");
email.setMsg("Message body example");

// add the attachment
email.attach(attachment);

// send the email
email.send();

问题是在Mac和其他系统上,使用描述而不是附件名称,因此解决方案是将名称放在两个字段中:

email.attach(ds, XML_ATTACHMENT_FILE_NAME, XML_ATTACHMENT_FILE_NAME, EmailAttachment.ATTACHMENT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM