-
-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properties aquired through ConfigLoader should be typed explicitly and converted if necessary #168
Comments
Hi @jmorenojuez, thanks for the report! Can you post up a small example code? |
Hi Benny,
Yes, no problem.
This is my simplejavamail.properties file:
simplejavamail.smtp.host=smtp.production.host
simplejavamail.smtp.port=587
simplejavamail.transportstrategy=SMTPS
simplejavamail.smtp.username=username
simplejavamail.smtp.password=1111
simplejavamail.defaults.from.address=from@default.com <mailto:simplejavamail.defaults.from.address=from@default.com>
And this is the constructor of my EmailService:
public EmailService() {
File propsFile = new File(PROPERTIES_FILENAME);
if (propsFile.exists()) {
LOGGER.info("Overwritting simplejavamail properties from external file '{}'",PROPERTIES_FILENAME);
propMap = ConfigLoader.loadProperties(propsFile, true);
LOGGER.trace("Java mail properties read {}", propMap);
} else {
propMap = ConfigLoader.loadProperties(ConfigLoader.DEFAULT_CONFIG_FILENAME, true);
}
mailer = MailerBuilder.buildMailer();
}
And I get the exception when building the mailer instance. By taking a look into your code, the property simplejavamail.smtp.password is expected to be a String but it gets converted to an Integer Object on the ConfigLoader class:
static Object parsePropertyValue(final String propertyValue) {
if (propertyValue == null) {
return null;
}
// read boolean value
final Map<String, Boolean> booleanConversionMap = new HashMap<>();
booleanConversionMap.put("0", false);
booleanConversionMap.put("1", true);
booleanConversionMap.put("false", false);
booleanConversionMap.put("true", true);
booleanConversionMap.put("no", false);
booleanConversionMap.put("yes", true);
if (booleanConversionMap.containsKey(propertyValue)) {
return booleanConversionMap.get(propertyValue.toLowerCase());
}
// read number value
try {
return Integer.valueOf(propertyValue);
} catch (final NumberFormatException nfe) {
// ok, so not a number
}
// read TransportStrategy value
try {
return TransportStrategy.valueOf(propertyValue);
} catch (final IllegalArgumentException nfe) {
// ok, so not a TransportStrategy either
}
// return value as is (which should be string)
return propertyValue;
}
Maybe you should try to convert to Integer only the properties that need to be Integer such as port…
I hope that it helps.
Best regards.
|
bbottema
added a commit
that referenced
this issue
Sep 22, 2018
bbottema
added a commit
that referenced
this issue
Sep 22, 2018
bbottema
changed the title
ClassCastException when setting "simplejavamail.smtp.password" property to an integer number
Properties aquired through ConfigLoader should be typed explicitly and converted if necessary
Sep 22, 2018
Can you please verify that this was solved in 5.0.4? |
Yes indeed has been solved!
Better support than a comercial app 😊
De: Benny Bottema
Enviado: sábado, 22 de septiembre de 2018 11:26
Para: bbottema/simple-java-mail
CC: jmorenojuez; Mention
Asunto: Re: [bbottema/simple-java-mail] Properties aquired throughConfigLoader should be typed explicitly and converted if necessary (#168)
Can you please verify that this was solved in 5.0.4?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
Nice work with your email library; It has made my life much easier.
I think that I migth have found a little bug. Basically if you set a property that it is supposed to be a String like "simplejavamail.smtp.password" to an integer number, I get the followiing Exception when builind a Mailer instance:
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
By setting quoting the property on the propertis file I can bypass the issue but the problem afterwards is that the value of the property has these quotes. Same using double quotes.
Best regards.
The text was updated successfully, but these errors were encountered: