I need to archive/store a gmail email form java, but i only know set it on read/unread...
Here is my code :
public class CheckingMails {
private static Session session = null;
private static Store store = null;
private static Folder inbox = null;
public static void check(String host, String storeType, final String user,
final String password)
{
try {
Properties properties = new Properties();
properties.setProperty("mail.host", "imap.gmail.com");
properties.setProperty("mail.port", "995");
properties.setProperty("mail.transport.protocol", "imaps");
session = Session.getInstance(properties,new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}});
store = session.getStore("imaps");
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
Message messages[] = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
for (int i = 0; i < messages.length; i++) {
if(messages[i].getSubject().contains("Ticket#")){
System.out.println("Number of Ticket = " + messages.length);
messages[i].setFlag(Flag.SEEN, true);
}
}
inbox.close(true);
store.close();
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Can someone explain me how can i archive it?
I assume by "archive it" you mean "move it to the archive folder on the server".
Use Folder.copyMessages to copy the message to the Archive folder, then mark the original message as DELETED using Message.setFlag.