spring javamail图片显示问题

mac下图片无法直接显示,显示为附件

  1. 修改前代码摘要
 //设置utf-8或GBK编码,否则邮件会有乱码
  MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, 3,
					mailEncoding);
  ClassPathResource resource = new ClassPathResource("img/logo.v3.png");
  messageHelper.addInline(file.getContentId(),file.getResource());

此是图片在mac emal中显示为附件,在outlook中正常

  1. 修改后代码摘要
					//附件
					DataSource dataSource = new DataSource() {
						@Override
						public InputStream getInputStream() throws IOException {
							return file.getResource().getInputStream();
						}
						@Override
						public OutputStream getOutputStream() {
							throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
						}
						@Override
						public String getContentType() {
							return contentType;
						}
						@Override
						public String getName() {
							return "inline";
						}
					};

					MimeBodyPart mimeBodyPart = new MimeBodyPart();
					mimeBodyPart.setHeader("Content-Type", contentType);
					mimeBodyPart.setDisposition("inline");
					mimeBodyPart.setHeader("Content-ID", "<" + file.getContentId() + ">");
					mimeBodyPart.setFileName(file.getResource().getFilename());
					mimeBodyPart.setDataHandler(new DataHandler(dataSource));
					messageHelper.getMimeMultipart().addBodyPart(mimeBodyPart);

此时mac email中图片显示正常
3. 原因分析
MimeMessageHelper 的addInline没有指定文件名
mimeBodyPart.setFileName(file.getResource().getFilename());

public void addInline(String contentId, DataSource dataSource) throws MessagingException {
        Assert.notNull(contentId, "Content ID must not be null");
        Assert.notNull(dataSource, "DataSource must not be null");
        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setDisposition("inline");
        mimeBodyPart.setHeader("Content-ID", "<" + contentId + ">");
        mimeBodyPart.setDataHandler(new DataHandler(dataSource));
        this.getMimeMultipart().addBodyPart(mimeBodyPart);
    }```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值