Howto access Flags.Flag value in javax.mail?

Dear all,
I am stuck finding a solution for the following problem:
Problem description:
I want to download all unseen mails from a mailaccount using the JavaMail package in MATLAB. I am able to setup, connect and open the folder I want to get the mails from. For finding all messages that are unseen, I want to use the function IMAPFolder.search(SearchTerm term). For this SearchTerm, a Flags.Flag is required which is defined in a nested public static final class in javax.mail.Flags. The problem is now how to get the values of this class.
What I tried and what errors I get:
I already tried several ways to access fields in a nested java class, here is a list:
For the following link below, the first solution does not work for me because I am not able to change the Java library. Also the second solution does not work because it is a static class.
SEEN = javaMethod('valueOf', 'javax.mail.Flags$Flag', 'SEEN')
And this is the error I get:
Error using javaMethod
No method valueOf exists in Java class javax.mail.Flags$Flag
I also found another question about this topic here:
My most recent code:
config = load('myconfig.mat');
props = java.util.Properties;
props.put('mail.imap.port', config.MAIL.port);
props.put('mail.imap.timeout', config.MAIL.imap_timeout);
props.put('mail.imaps.connectiontimeout', config.MAIL.imaps_connection_timeout);
session = javax.mail.Session.getInstance(props);
imapstore = session.getStore('imaps');
imapstore.connect( ...
config.MAIL.server, ...
config.MAIL.port, ...
config.MAIL.username, ...
config.MAIL.password ...
);
folder = imapstore.getFolder('Inbox');
folder.open(folder.READ_ONLY);
% everything works fine up to this point
search_flags = javax.mail.Flags;
flags_class = search_flags.getClass;
nested_classes = flags_class.getClasses;
flag_class = nested_classes(1);
% how do I access the static fields now?
Can someone help me? My Matlab version is R2016a.
EDIT1:
I also tried creating the FlagTerm using
cl = flags_class.getClassLoader;
rt = java.lang.Class.forName('javax.mail.Flags$Flag', false, cl);
search_flags.add(rt.getField('SEEN'));
as seen in http://de.mathworks.com/matlabcentral/newsreader/view_thread/125420 but it resulted in the following error:
No method 'add' with matching signature found for class 'javax.mail.Flags'.

 Accepted Answer

Hi all,
i found the solution by myself. See http://undocumentedmatlab.com/blog/accessing-internal-java-class-members, the last paragraph is about how to solve this problem.
Quote:
>> channel = java.io.FileInputStream('234.jpg').getChannel
channel =
sun.nio.ch.FileChannelImpl@1c7a5d3 <= which extends FileChannel
>> innerClasses = channel.getClass.getSuperclass.getDeclaredClasses;
>> innerClasses(1)
ans =
class java.nio.channels.FileChannel$MapMode
>> read_only_const = innerClasses(1).getField('READ_ONLY').get(1)
read_only_const =
READ_ONLY <= a java.nio.channels.FileChannel$MapMode object
>> fields = innerClasses(1).getFields;
>> read_only_const = fields(1).get(1); % an alternative
>> buffer = channel.map(read_only_const, 0, channel.size);

More Answers (0)

Categories

Find more on Functions in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!