3

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

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

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

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

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

有谁知道有办法做到这一点?

4

2 回答 2

2

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

春天有办法做你想做的工作。查看 Spring 3x 参考的这一或 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);

希望这可以帮助某人...

于 2012-10-11T19:28:28.297 回答
1

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

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

Url url = new URL("http://fileserver.com/images/123.jpg");
InputStream is = u.openStream();
...
于 2011-06-22T07:37:47.803 回答