Get image path in non-servlet
I have a scheduled task running in JBoss 5.1 on a daily basis for sending birthday wishes.
The mail content is HTML and I embed images in the mail.
Now I would like to get the path of that image for embedding, how would it be possible to get path of image in a non-servelt environment.
Ofcourse I could have placed the im开发者_StackOverflow中文版ages at a static location and accessed them, for which I don't want to hardcode the path.
The image is at "WebContent/images/birthday.jpg" location.
How are you generating the email content? Are these also static html files?
If you are going to use simple static html files, you will have to hard code the image paths. There is no other way around it.
You could write a simple Java application, which runs as a standalone application (without any servers,servlets etc), which will create the email content.
The java code can send out the emails for you too if you want.
These are some of the things you can do, if you use java
- Use property files to specify the location of images. These are files which hold simple key/value pairs.
- You can easily create multiple email content to different users, with the same template.
You will be able to easily redesign the html content for multiple users.
An example of using property files.Create a file ex: "email_template.properties"
Enter the following into the file and save it.image_server=http://www.mywebsite.com
image_folder=/WebContent/images/Create a jave program to create your html email, and use the property file to generate the image locations.
Properties properties = new Properties(); try { properties.load(new FileInputStream("C://email_template.properties")); //specify path here String sServerLocation = properties.getProperty("image_server"); String sImageFolder = properties.getProperty("image_folder"); StringBuilder strEmail = new StringBuilder(); strEmail.append("<html><body> <img src=\"" + sServerLocation + sImageFolder +"birthday.jsp\""> </body> </html>" ); // Write code to generate complete email dynamically // write code to send out the email or to save as html file to you machine, where you can send it manually.
} catch (IOException e) { // }
You get the idea. using plain html you will have to hard code.
However if you use a simple java file you can get more flexibility.
If you need code to send out email from java, check this link out.
How can I send an email by Java application using GMail, Yahoo, or Hotmail?
精彩评论