关于Java spring MimeMessageHelper发送带有图像的电子邮件

发布于 2024-11-16 07:53:39 字数 351 浏览 11 评论 0原文

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

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

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

,但如果我想发送带有我的文件服务器图像的电子邮件,则行不通。

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

有人知道有办法做到这一点吗?

I have images on my file server which I want to embed into my email.

I can send email with local images like this way

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

but if i want to send email with my file server images, won't work.

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

Anybody knows there is a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舞袖。长 2024-11-23 07:53:39

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

Spring 有办法完成你想要的工作。查看 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 所说的操作:< /strong>

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

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

helper.addInline("myIdentifier", res);

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

This question is a year old, but I want to contribute for help other people...

Spring have ways to do the job that you want. Take a look in this chapter of the Spring 3x reference. Or Spring2x.

In resume:
You can obtain your image file from file file system of the server. As you can see in the reference:

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

Or from a relative path of the classpath of your application:

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

But, if you want to send a resource from a file server with a URL, you can do something like @Ralph said:

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

And then, simply add the resource to your message:

helper.addInline("myIdentifier", res);

Hope this help somebody...

小忆控 2024-11-23 07:53:39

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

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

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

The problem is that http://fileserver.com/images/123.jpg is no Class Path Resource.

If you access the image from the file system then file access classes from java.io package.
If you really need to to access the files over http, then you need to download the file first.

Url url = new URL("http://fileserver.com/images/123.jpg");
InputStream is = u.openStream();
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文