【问题标题】:Sending inline images javaMail Google App Engine with Map使用 Map 发送内联图像 javaMail Google App Engine
【发布时间】:2018-01-20 08:46:10
【问题描述】:

在使用默认 JavaMail 服务向 Google App Engine 项目添加内联图像时,难以收到要发送的电子邮件。无法理解为什么它不发送。当我没有 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) {
                  // ...
                }

        }

【问题讨论】:

    标签: java google-app-engine jakarta-mail


    【解决方案1】:

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

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

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

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

    Multipart multipart = new MimeMultipart("related");
    

    【讨论】:

    • 太棒了,谢谢伙计,终于到了某个地方!所以这都是关于我错过的“相关”部分。也感谢有关 URL 的信息。如果您不介意,一个简单的问题,我收到以下错误:“java.security.AccessControlException access denied ("java.io.FilePermission" "/img/home-icon.png" "read") sendImage" 任何想法在那?
    • 没关系,最后想通了。问题是我没有相对 URL。它必须是“inlineImages.put("ID1", "img/home-icon.png");"。而不是“inlineImages.put("ID1", "/img/home-icon.png");"。更多信息:stackoverflow.com/questions/16513260/…
    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 2012-03-24
    • 2015-11-08
    • 2011-06-20
    • 1970-01-01
    • 2015-02-25
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多