简体   繁体   中英

Read email contents using Apache Commons Email

I've different properties file as shown below:

abc_en.properties
abc_ch.properties
abc_de.properties

All of these contain HTML tags & some static contents along with some image urls.

I want to send email message using apache commons email & I'm able to compose the name of the template through Java using locale as well.

String name = abc_ch.properties;

Now, how do I read it to send it as a Html Msg parameter using Java?

HtmlEmail e = new HtmlEmail();
e.setHostName("my.mail.com");
...
e.setHtmlMsg(msg);    

How do I get the msg param to get the contents from the file? Any efficient & nice solun?

Can any one provide sample java code?

Note: The properties file has dynamic entries for username & some other fields like Dear ,....How do I substitute those dynamically?

Thanks

I would assume that *.properties is a text file.

If so, then do a File read into a String

eg:

String name = getContents(new java.io.File("/path/file.properties");

public static String getContents(File aFile) {
    StringBuffer contents = new StringBuffer();
    BufferedReader input = null;
    try {
         InputStreamReader fr=new InputStreamReader(new FileInputStream(aFile), "UTF8");
        input = new BufferedReader( fr );
      String line = null; 
      while (( line = input.readLine()) != null){
        contents.append(line);
        contents.append(System.getProperty("line.separator"));
      }
    }
    catch (FileNotFoundException ex) {
      //ex.printStackTrace();
    }
    catch (IOException ex){
      //ex.printStackTrace();
    }
    finally {
      try {
        if (input!= null) {
          input.close();
        }
      }
      catch (IOException ex) {
        //ex.printStackTrace();
      }
    }
    return contents.toString();
  }

regards


Well, I kind of guess that you are trying to send mails in multiple languages by rendering the elements from different property files at runtime. Also, you said "locale". Are you using the concept of " Resource Bundles )"?
1)You need to understand the naming conventions for naming a property file , without which the java compiler will not be able to load the appropriate property file at run time. For this read the first page on the Resource Bundles page.
2) Once your naming conventions is fine, you can load the appropriate prop file like this:

Locale yourLocale = new Locale("en", "US");
ResourceBundle rb = ResourceBundle.getBundle("resourceBundleFileName", yourLocale);

3) Resource Bundle property file is nothing but a (Key,Value) pairs. Hence you can retrieve the value of a key like this:

String dearString = rb.getString("Dear");
String emailBody= rb.getString("emailBody");

4) You can later use this values for setting the attributes in your commons-email api.

Hope you find this useful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM