使用Map发送内嵌图像javaMail Google App Engine

时间:2017-08-11 18:40:47

标签: java google-app-engine javamail

在向其添加内嵌图片时,使用默认的JavaMail服务收到通过Google App Engine项目发送的电子邮件很困难。无法理解为什么它不发送。当我没有imagePart时它工作正常。有谁知道我在这里做错了什么?或者我如何调试这个?非常感激。感谢...

 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) {
                  // ...
                }

        }

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

{
    "query":{
        "bool":{
            "must": [
                {
                    "terms":{
                        "city":[
                            "Contagem"
                        ]
                    }
                },
                {
                    "range":{
                        "price_adult":{
                            "lte":"300",
                            "gte":"150"
                        }
                    }
                }
            ]
        }
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

我认为/img/home-icon.png是本地可访问的文件。

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

Multipart multipart = new MimeMultipart("related");