简体   繁体   中英

java iterate through properties file

I have a project to send emails via a Java Application. I have almost everything working, I just need to be able to configure the number of emails I send.

I have all of the email addresses in a Properties File. The application spec requires me to set a value in this file that will determine the number of emails to send. Here is an example of my properties file, in the hope that this helps:

email1=tom@foo.com
email2=jerry@foo.com
email3=spike@foo.com
iterate=1  //this is the value that needs to be changed 
           in order to decide the number of emails to send.

In my application code, I have the following:

Application

Properties props = new Properties();
String to = props.getProperty("to");
String from = props.getProperty("from");
String host = props.getProperty("host");
String port = props.getProperty("port);

props.setProperty("mail.smtp.host", host);
props.setProperty("mail.smtp.port", port);

try
    {
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("<subject goes here>");
        message.setText(<body goes here>);
        Transport.send(message);
    }

I think you put all your email id against single key

emails=tom@foo.com,jerry@foo.com,spike@foo.com

Then read this emails string and split it into to array.

Then read iterate , and execute your for loop on email array upto no of iterate present in your properties file.

Edited

If this email id's for to emails

String emails = props.getProperty("emails);
String iterate = Integer.valueOf(props.getProperty("iterate));
String arr[]= emails.split(",");
String toArr[] = new String[iterate];
System.arraycopy(arr, 0, toArr, 0, iterate);

now you have toArr array, using this toArr send your email's

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