move (copy) IMAPMessage to another folder on the mail server - java

My application is checking the patterns of the subjects of the mails on the Inbox server folder and if some pattern is found, we should move the email (com.sun.mail.imap.IMAPMessage) to another folder - called 'test' for example (copy will do the job also).
I searched on the Internet for the solution but I could not find anything helpful.
Can you tell me how can I move / copy IMAPMessage from inbox to another folder on server?
Thank you

Presumably you're already using a com.sun.mail.imap.IMAPFolder?
That class has the method addMessages(Message[] msgs). Use it to add a Message to the new folder.
Alternatively, as mentioned by #gospodin, there's a copyMessages(Message[] msgs, Folder destinationFolder) method, which provides a shortcut for copying messages from their original folder to a new one.

It is a bad idea to move a message with methods like copyMessages(), addMessages() or appendMessage() and removing the old message, because these methods generates a new message. The new message have an different Message-ID in the header. If you response on the new message, the receiver cannot relate the response to his sent mail, because he does not know the new Message-ID. You have to cast the folder to a IMAPFolder. IMAPFolder has the method moveMessages(Message[] msgs, Folder targetFolder) to move messages without tampering the header Message-ID.

List<Message> tempList = new ArrayList<>();
tempList.add(myImapMsg);
Message[] tempMessageArray = tempList.toArray(new Message[tempList.size()]);
fromFolder.copyMessages(tempMessageArray, destFolder);

Related

how can i get QQMail Starred folder with java mail

i have try this val f = store.defaultFolder.list("*")
but just get :
INBOX,
Sent Messages,
Drafts,
Deleted Messages,
Junk,
how to access QQMail or other Mail "Starred" folder
"starred" is not a folder is a flag , and i get it by this line code message.flags.systemFlags.contains(Flags.Flag.FLAGGED)
if get true means this message belong to "starred folder"

Paho client for java generating folder like paho101658642587966-tcp1270011883. what is the importance of this?

I am using Paho client for java to connect to activeMq over mqtt. I noticed one strange thing. There are several folder created having names like "paho101658642587966-tcp1270011883" and having empty .lck files. Why are these used and when are they created.
These files are created to store inflight messages for QOS2 message before their delivery to the broker can be confirmed.
They are created by the MQTTDefaultFilePersistence class, you can change the directory name and path by creating your own MQTTDefaultFilePresistence object and passing it to the MQTTClient constructor.
You can also switch to a in memory store, but this will change how QOS2 messages are handled if the client crashes before a delivery can be confirmed.
You can specify the /tmp directory for client execution as:
String receiverId = UUID.randomUUID().toString();
IMqttClient receiver = new MqttClient(
"tcp://" + properties.getProperty("host") + ":" + properties.getProperty("port"), receiverId, new MqttDefaultFilePersistence("/tmp"));

Wicket: AJAXDownload - dowload several files

I am using the wicket framework.
I have a requirement to send to the client browser several individual files (a zip file is not relevant).
I have added to my page an AJAXDownload class that extends AbstractAjaxBehavior - a solution for sending files to the client like this:
download = new AJAXDownload(){
#Override
protected IResourceStream getResourceStream(){
return new FileResourceStream(file){
#Override
public void close() throws IOException {
super.close();
file.delete();
}
};
}};
add(download);
At some other point in my code I am trying to initiate the download of several files to the client using an ajax request whilst looping through an arraylist of files and then each time triggering the AJAXDownload:
ArrayList<File> labelList = printLabels();
for(int i=0; i<labelList.size(); i++){
file = labelList.get(i);
//initiate the download
download.initiate(target);
}
However, it is only sending just one of these files to the client. I have checked and the files have definitely been created on the server side. But only one is of them is being sent to the client.
Can anyone give me an idea what I am doing wrong?
Thanks
You are doing everything correct!
I don't know how to solve your problem but I'll try to explain what happens so someone else could help:
The Ajax response has several entries like:
<evaluate>document.location=/some/path/to/a/file</evaluate>
wicket-ajax.js just loops over the evaluations and executes them. If there is one entry then everything is OK - you have the file downloaded. But if there are more then the browser receives several requests for changing its location in very short time. Apparently it drops all but one of them.
An obvious solution would be to use callbacks/promises - when a download finishes then trigger the next one. The problem is that there is no way how to receive a notification from the browser that such download finished. Or at least I don't know about it.
One can roll a solution based on timeouts (i.e. setTimeout) but it would be error prone.
I hope this information is sufficient for someone else to give you the solution!

How to delete email with javax.email?

I have already seen another similar posts but it is still not working. I am using javax.email with Gmail and I can read messages successfully , but when I want to delete them, it does not do anything... I am doing this:
// Open folder
this.folder = this.store.getFolder("Inbox");
this.folder.open(Folder.READ_WRITE);
// Get messages with specified filters
messages = this.folder.search(messagesFilters);
// Delete
message[0].setFlag(Flag.DELETED,true);
// Close
this.folder.expunge();
this.folder.close(true);
this.store.close();
But nothing happens in Gmail. Do you know how to do it?

Using Java mail Pop3 cannot seem to delete emails from Gmail

I have a java program that acts as a POP3 client using javax.mail. I am able to list and retrieve the contents of a Gmail inbox no problem. However, I cannot seem to delete emails. Here is the (important parts of the) code:
POP3Store sto=... another method creates and connects the POP3Store
Folder ibx=sto.getFolder("INBOX");
ibx.open(Folder.READ_WRITE);
Message[] msgarr=ibx.getMessages();
for(int mi=0; mi<msgarr.length; mi++) {
...do stuff with the message
msgarr[mi].setFlag(Flags.Flag.DELETED, true);
}
ibx.close(true); //folder.close(true) indicates to expunge the folder
sto.close();
After running this and seeing it handle each message, I go into Gmail and the emails are still there, and even showing as unread. If I re-run the java client, it will see and handle the same emails.
This same code happily deletes emails from an exchange server.
How can I get Gmail to delete emails?
Gmail handles POP deletion specially.
You can configure what Gmail should do when a message is deleted through POP in Gmail Settings, on the Forwarding and POP / IMAP tab.
As SLaks says, Gmail is a special case where it has its own settings for controlling deletion that override whatever the client wants to do.
It can be edited in the Forwarding and POP / IMAP
However, I want to add that for a message to be considered "downloaded" by Gmail, you need to have retrieved the content of each message, and in the case of multipart message types, the content of each part within that message.
Here's some example code that I use to force the deletion of unwanted messages from Gmail:
// Grab the content to get the email off the server
// folder is of type javax.mail.Folder and is already in the correct state to get messages from the mail store (Gmail)
Message msg = folder.getMessage(1);
Multipart multipart = (Multipart) msg.getContent();
int partcount = multipart.getCount();
for (int count = 0; count < partcount; count++) {
multipart.getBodyPart(count);
}
If you prepend "recent:" in your pop3 username you will solve.
Example: recent:yourusername#gmail.com
This connects to gmail using Recent Mode

Categories

Resources