user809355
user809355

Reputation: 55

About Java spring MimeMessageHelper send email with images

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?

Upvotes: 3

Views: 10322

Answers (2)

Jesfre
Jesfre

Reputation: 702

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...

Upvotes: 2

Ralph
Ralph

Reputation: 120781

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();
...

Upvotes: 1

Related Questions