首页 > 解决方案 > 防止附加内联图像

问题描述

我有一个发送电子邮件的方法,签名是:

public void sendEmail(String to, File[] fileAttachments, String subject, 
                        String body, boolean inlineImages) throws Exception {

我最近对其进行了修改,使其能够内联发送图像(在 HTML 电子邮件中)而不是附件。处理附件的方法体是:

    MimeBodyPart messageBodyPart = new MimeBodyPart();

    // messageBodyPart.setText(body);
    messageBodyPart.setContent(body, "text/html; charset=utf-8");

    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);

    if (fileAttachments != null) {
        for( File f : fileAttachments ) {
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource(f);

            messageBodyPart.setDataHandler(new DataHandler(source));
            
            if( inlineImages ) {
                messageBodyPart.setHeader("Content-ID", "<image>");
            } else
                messageBodyPart.setFileName(f.getName());
            
            multipart.addBodyPart(messageBodyPart);
        }
    }

    // Put parts in message
    message.setContent(multipart);

    // Send the message
    Transport.send(message);

不幸的是,它现在以内附件的形式发送图像。如何修改代码,使其不附加图像,如果它是内联的?

标签: javaemail

解决方案


推荐阅读