I read this documentation:
http://docs.oracle.com/javaee/6/api/javax/mail/internet/package-summary.html
so I add some properties to mimeMessage:
Properties props = new Properties();
props.put("mail.mime.decodefilename", true);
Session mailConnection = Session.getInstance(props, null);
source = new FileInputStream(emlFile);
MimeMessage message = new MimeMessage(mailConnection, source);
now I am expectaing that method bodyPart.getFileName() return correct name of file. But with this configuration it still doesn work and I need to call mimeUtils: MimeUtility.decodeText - what I dont want. I also try:
props.put("mail.mime.decodefilename", "true");
but with no success. So what I am doing wrong ?
UPDATE:
after debuging I had this sollution:
this works
Properties props = System.getProperties();
props.put("mail.mime.decodefilename", "true");
this doesnt work:
Properties props = new Properites();
props.put("mail.mime.decodefilename", "true");
so if filename is decoding depends on system property too. Does anyone know which properties ? I dont have pattion to try all system properties and solve which one it is
MimeMessage.getFileName
If the mail.mime.encodefilename System property is set to true, the
MimeUtility.decodeText method will be used to decode the filename.
Now when one looks at the implementation, this is how the MimeUtility.decodeText comes into picture during the invocation of getFileName:
if (decodeFileName && filename != null) {
try {
filename = MimeUtility.decodeText(filename);
} catch (UnsupportedEncodingException ex) {
throw new MessagingException("Can't decode filename", ex);
}
}
Where decodeFileName is initialized like this:
s = System.getProperty("mail.mime.decodefilename");
// default to false
decodeFileName = s != null && !s.equalsIgnoreCase("false");
The javadoc seems to be conflicting with the implementation.
So, try setting mail.mime.decodefilename instead of mail.mime.encodefilename, probably using System.setProperty.
Related
Trying to move from jcifs to jcifs-ng (the latest jar jcifs-ng-2.1.2.jar) to copy files to/from remote.
My code using old jcifs:
System.setProperty("jcifs.smb.client.responseTimeout", "10000");
System.setProperty("jcifs.smb.client.soTimeout", "2000");
if (winsIPList.trim().equals("")) {
System.setProperty("jcifs.smb.client.dfs.disabled", "true");
} else {
System.setProperty("jcifs.smb.client.dfs.disabled", "false");
System.setProperty("jcifs.netbios.wins", winsIPList.trim());
System.setProperty("resolveOrder", "DNS");
}
NtlmPasswordAuthentication auth = new
NtlmPasswordAuthentication(filesrvDomainIP, filesrvDomainUser,
filesrvDomainPassword);
smbRemoteFile = new SmbFile("smb:" + remoteFile.replace("\\", "/"), auth);
<here the code to copy file>
Found few examples in stackoverflow, but looks like they are old.
Part of them include usage of NtlmPasswordAuthentication(context, DomainIP, DomainUser,DomainPassword) which is deprecated in the last jcifs-ng package.
Others use
SmbFile smbRemoteFile = new SmbFile(remoteFile, someContext)
which is reported as undefined by compiler
Could somebody provide an example that works?
Working example:
BaseContext baseCxt = null;
Properties jcifsProperties = new Properties();
jcifsProperties.setProperty("jcifs.smb.client.enableSMB2", "true");
jcifsProperties.setProperty("jcifs.smb.client.dfs.disabled","true");
Configuration config = new PropertyConfiguration(jcifsProperties);
baseCxt = new BaseContext(config);
auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator(DomainIP, DomainUser,
DomainPassword));
SmbFile smbRemoteFile = new SmbFile("smb:" + remoteFile.replace("\\", "/"), auth);
According to this issue: jcifs-ng Issue #36: Chicken/egg relationship between CIFSContext and credentials
Class NtlmPasswordAuthentication is replaced by NtlmPasswordAuthenticator.
So you might replace your NtlmPasswordAuthentication usage with:
NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator(domain, username, password)
Besides, this answer might be helpful.
I have a properties file say as follows:
apple=1
mango=2
banana=3
pineapple=4
I am using value annotation in the java program to access the values. I have a method in my class that computes a value i want to update the apple attribute in the properties file with the value that the method returns.
public class test {
#Value("${apple}")
private int apple;
public void testMethod() {
int new_val = 0;
if (apple > 0)
new_val = 300;
else
new_val = 200;
// now i want to update the value of apple in the file to new_val,(apple = new_val) other attributes should remain unchanged.
}
}
can someone let me know how to update the value in the properties file. In this example i want my properties file to become
apple=300
mango=2
banana=3
pineapple=4
Usually we defines constant values in properties, so it does not change.
But if it is your requirement to change it.
You can do it like:
1) Using Apache Commons Configuration library
PropertiesConfiguration conf = new PropertiesConfiguration("yourproperty.properties");
props.setProperty("apple", "300");
conf.save();
2) Using Java input and output stream
FileInputStream in = new FileInputStream("yourproperty.properties");
Properties props = new Properties();
props.load(in);
in.close();
FileOutputStream out = new FileOutputStream("yourproperty.properties");
props.setProperty("apple", "300");
props.store(out, null);
out.close();
I'm trying to load properties to a Properties object in java using load(new FileReader()) method. All the properties are loaded except the properties start with(#) commented ones. How to load these commented properties to the Properties object using java API. Only manual way?
Thanks in Advance.
I could propose you to extend the java.util.Properties class to override this specificities but it was not designed for it : many things are hardcoded and not overridable. So you should do entire copy-paste of methods with little modification.
For example, at a time, the LineReader used in internal does that when you load a properties file :
if (isNewLine) {
isNewLine = false;
if (c == '#' || c == '!') {
isCommentLine = true;
continue;
}
}
The # is hardcoded.
Edit
Another way could be read line by line the proprties file, remove the first char if it is # and write the read line, modified if needed, in a ByteArrayOutputStream. then you could load the properties with a ByteArrayInputStream from ByteArrayOutputStream.toByteArray().
Here a possible implementation with a unit test :
With as input myProp.properties :
dog=woof
#cat=meow
The unit test :
#Test
public void loadAllPropsIncludingCommented() throws Exception {
// check properties commented not retrieved
Properties properties = new Properties();
properties.load(LoadCommentedProp.class.getResourceAsStream("/myProp.properties"));
Assert.assertEquals("woof", properties.get("dog"));
Assert.assertNull(properties.get("cat"));
// action
BufferedReader bufferedIs = new BufferedReader(new FileReader(LoadCommentedProp.class.getResource("/myProp.properties").getFile()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
String currentLine = null;
while ((currentLine = bufferedIs.readLine()) != null) {
currentLine = currentLine.replaceFirst("^(#)+", "");
out.write((currentLine + "\n").getBytes());
}
bufferedIs.close();
out.close();
// assertion
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
properties = new Properties();
properties.load(in);
Assert.assertEquals("woof", properties.get("dog"));
Assert.assertEquals("meow", properties.get("cat"));
}
# Compulsory Dimension to create port
xOffset=-3
yOffset=50
How to get these xOffset and YOffset in java file.I tried with inputstream but not getting.These variable should get loaded in java file.
You can use Properties class from Java library
Properties prop = new Properties();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(propFileName);
prop.load(inputStream);
The you can get the values as
prop.getProperty("propertyname");
Try the following code:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("your_config.properties");
prop.load(input);
System.out.println(prop.getProperty("xOffset"));
System.out.println(prop.getProperty("yOffset"));
} catch (IOException e) {
// ...
}
As above explained Create a Function to read the property File During or Before Selenium Driver Constructor . So you can use them in test ( Help in Desire Capability Impl).
Store the values in Public Static final ( If you don not want to change them in Selenium and use as Default Property input)
As the values are read by Java Propertie file or .config file before selenium Driver so you can use them in Driver constructor or if you don't want you can use those properties stored as Static anywhere in the project. These values act as GLOBAL param.
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
Hi Mike,
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 )"? Well, in that case before you send mails,
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!