我有一個計劃任務,每天在JBoss 5.1中運行,用於發送生日祝福。獲取非servlet中的圖像路徑
郵件內容是HTML,我在郵件中嵌入圖像。
現在我想要獲取嵌入圖像的路徑,如何在非servelt環境中獲取圖像路徑。
當然,我可以將圖像放在一個靜態位置並訪問它們,爲此我不想硬編碼路徑。
圖像位於「WebContent/images/birthday.jpg」位置。
我有一個計劃任務,每天在JBoss 5.1中運行,用於發送生日祝福。獲取非servlet中的圖像路徑
郵件內容是HTML,我在郵件中嵌入圖像。
現在我想要獲取嵌入圖像的路徑,如何在非servelt環境中獲取圖像路徑。
當然,我可以將圖像放在一個靜態位置並訪問它們,爲此我不想硬編碼路徑。
圖像位於「WebContent/images/birthday.jpg」位置。
您是如何生成電子郵件內容的?這些也是靜態html文件嗎?
如果您打算使用簡單的靜態html文件,您將不得不硬編碼圖像路徑。沒有其他辦法。
你可以編寫一個簡單的Java應用程序,它作爲一個獨立的應用程序運行(沒有任何服務器,servlet等),它將創建郵件內容。
如果需要,Java代碼也可以爲您發送電子郵件。
這些都是你可以做的事情,如果你用java
您將可以輕鬆地爲多個用戶重新設計html內容。
使用屬性文件的示例。
創建文件例如:「email_template.properties」
在文件中輸入以下內容並保存。
image_server = http://www.mywebsite.com
image_folder = /的WebContent /圖片/
創建一個java的程序來創建你的HTML郵件,並使用屬性文件來生成圖像位置。
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.
}趕上(IOException異常E) {// }
你的想法。使用普通的html你將不得不硬編碼。
但是,如果你使用一個簡單的java文件,你可以獲得更多的靈活性。
如果您需要代碼發送來自java的電子郵件,請檢查此鏈接。
How can I send an email by Java application using GMail, Yahoo, or Hotmail?