JavaMail带附件时出错

JavaMail requires an InputStreamSource that creates a fresh stream for every


// 添加附件的方法


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("attachment");
        mimeBodyPart.setFileName(MimeUtility.encodeText(attachmentFilename));
        mimeBodyPart.setDataHandler(new DataHandler(dataSource));
        this.getRootMimeMultipart().addBodyPart(mimeBodyPart);
    } catch (UnsupportedEncodingException var4) {
        throw new MessagingException("Failed to encode attachment filename", var4);
    }
}

public void addAttachment(String attachmentFilename, File file) throws MessagingException {
    Assert.notNull(file, "File must not be null");
    FileDataSource dataSource = new FileDataSource(file);
    dataSource.setFileTypeMap(this.getFileTypeMap());
    this.addAttachment(attachmentFilename, (DataSource)dataSource);
}

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource) throws MessagingException {
    String contentType = this.getFileTypeMap().getContentType(attachmentFilename);
    this.addAttachment(attachmentFilename, inputStreamSource, contentType);
}

public void addAttachment(String attachmentFilename, InputStreamSource inputStreamSource, String contentType) throws MessagingException {
    Assert.notNull(inputStreamSource, "InputStreamSource must not be null");
    if (inputStreamSource instanceof Resource && ((Resource)inputStreamSource).isOpen()) {
        throw new IllegalArgumentException("Passed-in Resource contains an open stream: invalid argument. JavaMail requires an InputStreamSource that creates a fresh stream for every call.");
    } else {
        DataSource dataSource = this.createDataSource(inputStreamSource, contentType, attachmentFilename);
        this.addAttachment(attachmentFilename, dataSource);
    }
}


InputStreamResource inputStreamResource
messageHelper.addAttachment(MimeUtility.encodeWord(fileName), new ByteArrayResource(IOUtils.toByteArray(inputStreamResource.getInputStream())));
流不匹配会导致报错。通过转换成对应的流即可解决



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值