JavaMail - 内联图像也附加到电子邮件

时间:2016-01-07 06:05:26

标签: spring javamail

我在邮件中添加了内联图片,但它也附带了邮件。我的代码:

public Map<String, String> sendMail(final String campaignId, final String emailTo[], final String emailSubject, final String emailBody, final MultipartFile[] mailAttachment) {

    Map<String, String> messageMailIdMap = new HashMap<String, String>();
    SMTPMessage SMTPMessageArray[] = new SMTPMessage[emailTo.length];
    try {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        messageHelper.getMimeMessage().setHeader("X-Priority", "High");
        messageHelper.getMimeMessage().setHeader("Return-Receipt-To", AppProperties.getInstance().getEmailId());
        messageHelper.getMimeMessage().setHeader("Disposition-Notification-To", AppProperties.getInstance().getEmailId());
        messageHelper.setFrom(AppProperties.getInstance().getEmailId(), AppProperties.getInstance().getUserNameAlias());
        messageHelper.setSubject(emailSubject);

        if (mailAttachment != null)
            for (final MultipartFile multipartFile : mailAttachment) {
                if (multipartFile.getSize() != 0) {
                    String attachName = multipartFile.getOriginalFilename();

                    messageHelper.addAttachment(attachName, new InputStreamSource() {

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return multipartFile.getInputStream();
                        }
                    });
                }
            }

        for (int i = 0; i < emailTo.length; i++) {
            messageHelper.setTo(emailTo[i]);

            if (emailBody != null && emailBody.contains("<img")) {
                UtilityClass.saveEmailInlineImagesInTemp(emailBody);
                messageHelper.setText(UtilityClass.appendUnsubcribeTextUrl(UtilityClass.setInlineTextWithImages(emailBody),unsubcribeTextUrl(emailTo[i],campaignId)), true);

                int occurance = StringUtils.countOccurrencesOf(emailBody, "<img");
                for (int j = 0; j < occurance; j++) {
                    File inlineImage = new File(UtilityClass.mailTempFolderPath() + System.getProperty("file.separator") + j + "mailInlineImage.png");
                    messageHelper.addInline(j + "mailInlineImage", inlineImage);
                }
            } else
                messageHelper.setText(UtilityClass.appendUnsubcribeTextUrl(emailBody.toString(),unsubcribeTextUrl(emailTo[i],campaignId)), true);// //Mail
            messageHelper.getMimeMessage().saveChanges();

            messageMailIdMap.put(emailTo[i], messageHelper.getMimeMessage().getMessageID());

            SMTPMessage smtpMessage = new SMTPMessage(messageHelper.getMimeMessage());
            smtpMessage.setReturnOption(SMTPMessage.RETURN_FULL);
            smtpMessage.setNotifyOptions(SMTPMessage.NOTIFY_DELAY | SMTPMessage.NOTIFY_FAILURE | SMTPMessage.NOTIFY_SUCCESS);
            SMTPMessageArray[i] = smtpMessage;
        }

        mailSender.send(SMTPMessageArray);
    } catch (Exception ex) {
        System.out.println("Error Sending!!!");
        ex.printStackTrace();
        return null;
    } finally {
        File mailTempFolder = new File(UtilityClass.mailTempFolderPath());
        if (mailTempFolder.exists()) {
            for (File file : mailTempFolder.listFiles())
                file.delete();
            System.out.println("tempFilesDeleted........");
        }
    }

    return messageMailIdMap;
}

UtilityClass.saveEmailInlineImagesInTemp(emailBody)从html img标签收集图片信息并创建图片文件。

UtilityClass.setInlineTextWithImages(emailBody)使用img标记创建邮件正文,例如<img src="cid:0mailInlineImage">

messageHelper.addInline(j + "mailInlineImage", inlineImage);添加内联图片

SMTPMessageArray[i] = smtpMessage;创建一个邮件数组,以便发送给具有相同会话的多个收件人。我正在使用Spring JavaMailSender来创建MimeMessage。

System.out

登录
From: XXXXXXXXXXXXXXXXXXXXXXXXXXX
To: XXXXXXXXXXXXXXXXXXXXXXXXXXX
Message-ID: XXXXXXXXXXXXXXXXXXXXXXXXXXX
Subject: bellaward
MIME-Version: 1.0
Content-Type: multipart/mixed; 
    boundary="----=_Part_16_711673112.1452149784499"
X-Priority: High
Return-Receipt-To: XXXXXXXXXXXXXXXXXXXXXXXXXXX
Disposition-Notification-To: XXXXXXXXXXXXXXXXXXXXXXXXXXX

------=_Part_16_711673112.1452149784499
Content-Type: multipart/related; 
    boundary="----=_Part_17_1410900889.1452149784499"

------=_Part_17_1410900889.1452149784499
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: 7bit

<p>Hi </p><p>How Are you</p><p><br></p><p><img src='cid:0mailInlineImage'></p><p><br></p><p><br></p><p>Thanks<br></p>
------=_Part_17_1410900889.1452149784499
Content-Type: image/x-png
Content-Transfer-Encoding: base64
Content-Disposition: inline
Content-ID: <0mailInlineImage>

iVBORw0KGgoAAAANSUhEUgAAAUAAAACvCAIAAAADl5AeAAB0yUlEQVR42uy9h39c1bX3/fwnz+d9
npvgIttyg5Qnt+Tm3hBIw8aqM2ouQAgBLsm9SSCh2OqSMSWFjrFVpo96l1yxZbXpfdR718yc3t61
zpEnwtYYy0iWDDP8PkIanzlzzj77u9dae6+99//aU9QUV1xxPaD6X/EiiCuuOMBNuwsb44orrvus9++rAvgbIf8f/DPHTHrGDe4AAAAASUVORK5CYII=
------=_Part_17_1410900889.1452149784499--

------=_Part_16_711673112.1452149784499--

0 个答案:

没有答案