发送内联图像 javaMail Google App Engine with Map

Sending inline images javaMail Google App Engine with Map

向 Google App Engine 项目添加内联图像时,使用默认 JavaMail 服务发送电子邮件时遇到困难。不明白为什么它不发送。当我没有 imagePart 时它工作正常。有谁知道我在这里做错了什么?或者我该如何调试呢?非常感激。谢谢...

    public void run(){

         try{
                //email body
                String htmlMessage = "<html>Hi there,<br>";
                htmlMessage += "See this cool pic: <img src=\"cid:ID1\" />";
                htmlMessage += "And this cool pic: <img src=\"cid:ID2\" />";
                htmlMessage += "</html>";

                //image part
                Map<String, String> inlineImages = new HashMap<String, String>();
                inlineImages.put("ID1", "/img/home-icon.png");
                inlineImages.put("ID2", "https://storage.googleapis.com/logophilia/default/Logophilia.png");

                //send to
                String useremail = "some.email@gmail.com";

                //email method below
                sendImage(useremail,htmlMessage,inlineImages);    
         }    
}

下面的方法是发送电子邮件的方式。

 public static void sendImage(String useremail, String htmlBody,
                Map<String, String> mapInlineImages){

            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);

            //app engine requires this for some reason
            String msgBody = "...";

            try {
              Message msg = new MimeMessage(session);
              msg.setFrom(new InternetAddress("someaddress.mail@gmail.com", "An Image test from website"));
              msg.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(useremail, "Admin"));
              msg.setSubject("Image test");
              msg.setText(msgBody);

            // creates message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setContent(htmlBody, "text/html");

            // creates multi-part
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);

            // adds inline image attachments
            if (mapInlineImages != null && mapInlineImages.size() > 0) {
                Set<String> setImageID = mapInlineImages.keySet();

                for (String contentId : setImageID) {
                    MimeBodyPart imagePart = new MimeBodyPart();
                    imagePart.setHeader("Content-ID", "<" + contentId + ">");
                    imagePart.setDisposition(MimeBodyPart.INLINE);

                    //commenting out the stuff below makes it work...
                    String imageFilePath = mapInlineImages.get(contentId);
                    try {
                        imagePart.attachFile(imageFilePath);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                    multipart.addBodyPart(imagePart);


                }
            }

            msg.setContent(multipart);

            Transport.send(msg);

            } catch (AddressException e) {
                  // ...
                } catch (MessagingException e) {
                  // ...
                } catch (UnsupportedEncodingException e) {
                  // ...
                }

        }

您正在使用 attachFile 方法附加不是文件 (ID2) 的内容,它是 URL。改用这个:

imagePart.setDataHandler(new DataHandler(new URL(imageFilePath)));

我假设 /img/home-icon.png 是一个本地可访问的文件。

另外,您可能想创建一个 multipart/related 而不是默认的 multipart/mixed:

Multipart multipart = new MimeMultipart("related");