【问题标题】:About Java spring MimeMessageHelper send email with images关于 Java spring MimeMessageHelper 发送带有图像的电子邮件
【发布时间】:2011-09-19 22:33:57
【问题描述】:

我的文件服务器上有图像,我想嵌入到我的电子邮件中。

我可以像这样发送带有本地图像的电子邮件

message.addInline("image1", new ClassPathResource("../../static/images/123.jpg"));

但如果我想用我的文件服务器图像发送电子邮件,将无法正常工作。

message.addInline("image1", new ClassPathResource("http://fileserver.com/images/123.jpg"));

有人知道有办法吗?

【问题讨论】:

    标签: java spring mime-message


    【解决方案1】:

    问题是http://fileserver.com/images/123.jpg 不是类路径资源。

    如果您从文件系统访问图像,则从java.io 包中访问文件类。 如果您确实需要通过http访问文件,那么您需要先下载文件。

    Url url = new URL("http://fileserver.com/images/123.jpg");
    InputStream is = u.openStream();
    ...
    

    【讨论】:

      【解决方案2】:

      这个问题已经有一年了,但我想为帮助其他人做出贡献......

      Spring 有办法完成您想要的工作。看看这个chapter of the Spring 3x referenceOr Spring2x.

      在简历中: 您可以从服务器的文件文件系统中获取您的图像文件。正如您在参考资料中看到的那样:

      Resource res = new FileSystemResource(new File("c:/Sample.jpg"));
      helper.addInline("identifier1234", res);
      

      或来自应用程序类路径的相对路径:

      Resource res = new ClassPathResource("mx/blogspot/jesfre/test/image.png");
      

      但是,如果您想通过 URL 从文件服务器发送资源,您可以执行 @Ralph 所说的类似操作:

      Url url = new URL("http://fileserver.com/images/123.jpg");
      Resource res = new InputStreamResource(u.openStream());
      

      然后,只需将资源添加到您的消息中:

      helper.addInline("myIdentifier", res);
      

      希望这对某人有所帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-08
        • 2013-06-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        • 2015-07-29
        • 2013-11-11
        • 2012-04-07
        相关资源
        最近更新 更多