4

メールに複数の画像を追加したい

String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "email-template.vm", module);
            message.setText(text, true);

            message.addInline("img001", new ClassPathResource("/myPath/image001.jpg"));
            message.addInline("img002", new ClassPathResource("/myPath/image002.jpg"));

しかし、私は NullPointerException を持っています!

org.springframework.mail.MailPreparationException: Could not prepare mail; nested exception is java.lang.NullPointerException
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:369) [spring-context-support-2.5.6.SEC01.jar:2.5.6.SEC01]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:346) [spring-context-support-2.5.6.SEC01.jar:2.5.6.SEC01]

画像を1つだけ追加すると(addInline())、すべてがうまくいき、メールが送信されます

誰にもアイデアがありますか?ありがとう

編集:

複数の画像を含むメールを送信する方法を見つけました

    MimeMessagePreparator preparator = new MimeMessagePreparator() {
         public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,MimeMessageHelper.MULTIPART_MODE_RELATED,"UTF-8");
                    message.setTo(TO);
                    message.setFrom(FROM);
    MimeBodyPart part = new MimeBodyPart();
    String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,"template.vm","UTF-8",module);
    part.setContent(body,"text/html");
// Create a new part for the attached image and set the CID image identifier
    MimeBodyPart imagePart = new MimeBodyPart();
    FileDataSource fds = new FileDataSource("C:/path/image001.jpg");
    imagePart.setDataHandler(new DataHandler(fds));
    imagePart.setHeader("Content-ID", "resImg1");

    MimeBodyPart imagePart2 = new MimeBodyPart();
    FileDataSource fds2 = new FileDataSource("C:/image002.jpg");
    imagePart2.setDataHandler(new DataHandler(fds2));
    imagePart2.setHeader("Content-ID", "resImg2");

    Multipart mp = new MimeMultipart();

    mp.addBodyPart(part);
    mp.addBodyPart(imagePart);
    mp.addBodyPart(imagePart2);

    // Set the message's content
    mimeMessage.setContent(mp);
    }
};
this.mailSender.send(preparator);

画像を挿入するための template.vm :<img width=172 height=40 src="cid:resImg1"alt="Image1"/>

誰かを助けてほしい

4

0 に答える 0