JavaMail附件打不开

System.getProperties().setProperty("mail.mime.splitlongparameters", "false");

客户邮箱用的是outlook邮箱
在这里插入图片描述

原因:附件名过长
参数mail.mime.splitlongparameters 在linux下 会默认为 true,附件名过长,就会被截断

helper.addAttachment(MimeUtility.encodeWord(fileName,"utf-8","B"), source);

public void addAttachment(String attachmentFilename, DataSource dataSource) throws MessagingException {
	Assert.notNull(attachmentFilename, "Attachment filename must not be null");
	Assert.notNull(dataSource, "DataSource must not be null");
	try {
		MimeBodyPart mimeBodyPart = new MimeBodyPart();
		mimeBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
		mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
		mimeBodyPart.setDataHandler(new DataHandler(dataSource));
		getRootMimeMultipart().addBodyPart(mimeBodyPart);
	}
	catch (UnsupportedEncodingException ex) {
		throw new MessagingException("Failed to encode attachment filename", ex);
	}
}

public void setFileName(String filename) throws MessagingException {
    setFileName(this, filename);
}

static void setFileName(MimePart part, String name) throws MessagingException {
    ...
    ParameterList p = cd.getParameterList();
    ...
    
if (value.length() > 60 && splitLongParameters && encodeParameters) {

private static final boolean splitLongParameters = PropUtil.getBooleanSystemProperty("mail.mime.splitlongparameters", true);

这次遇到的是在方法中已经设置的false在生产环境中依然有问题,最后发现是其他方法有发送附件且没有设置为false,并且这个环境变量是只能配置一次。
所以最好是放到启动类,我的代码是写在Listener中的,我就直接放到了static代码块中

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用JavaMail库来下载电子邮件附件。以下是一个简单的示例代码,演示了如何从电子邮件中下载附件: ```java import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Part; import javax.mail.Session; import javax.mail.Store; public class EmailAttachmentDownloader { public static void main(String[] args) { String host = "your_email_host"; String username = "your_email_username"; String password = "your_email_password"; int messageNumber = 1; // 要下载附件的电子邮件的序号 String saveDirectory = "path_to_save_attachments"; // 附件保存的目录 Properties properties = new Properties(); properties.put("mail.store.protocol", "pop3"); properties.put("mail.pop3.host", host); properties.put("mail.pop3.port", "995"); properties.put("mail.pop3.ssl.enable", "true"); Session session = Session.getDefaultInstance(properties); try { Store store = session.getStore("pop3s"); store.connect(host, username, password); Folder emailFolder = store.getFolder("INBOX"); emailFolder.open(Folder.READ_ONLY); Message message = emailFolder.getMessage(messageNumber); Multipart multipart = (Multipart) message.getContent(); for (int i = 0; i < multipart.getCount(); i++) { BodyPart bodyPart = multipart.getBodyPart(i); if (Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) { String fileName = bodyPart.getFileName(); InputStream inputStream = bodyPart.getInputStream(); FileOutputStream outputStream = new FileOutputStream(new File(saveDirectory + File.separator + fileName)); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); } } emailFolder.close(false); store.close(); } catch (MessagingException | IOException e) { e.printStackTrace(); } } } ``` 请确保将`your_email_host`,`your_email_username`,`your_email_password`和`path_to_save_attachments`替换为实际的值。此示例代码假设您正在使用POP3协议。 这段代码将连接到指定的电子邮件服务器,打开收件箱文件夹,获取指定序号的电子邮件,并将其中的附件保存到指定目录中。 请注意,该示例代码只适用于下载单个附件。如果电子邮件中有多个附件,您可能需要根据需要进行修改,以处理所有附件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值