Save Draft Message Using Java Mail API? - java

Currently im doing as following,
#Override
public void saveDraftMessage(MimeMessage draftMessage) throws MessagingException
{
Folder draftsMailBoxFolder = imapsStore.getFolder("inbox");//[Gmail]/Drafts
draftsMailBoxFolder.open(Folder.READ_WRITE);
draftMessage.setFlag(Flag.DRAFT, true);
MimeMessage draftMessages[] = {draftMessage};
draftsMailBoxFolder.appendMessages(draftMessages);
}
It works but , as you could see message is being appended to "inbox" folder without complain from server end !
Is there any kind of validation or an alternative method to ensure that message is saved as Draft only at appropriate place.

As others have suggested above, you need to store your draft messages in a different folder. You can choose the name of that folder. If you're only using Gmail and you want to be consistent with what Gmail is doing, saving it in the folder Gmail uses ("[Gmail]/Drafts"?) would make sense. Remember to delete the message from the folder when you send it.

Related

Google Drive setup push notifications

We want to receive notifications from google when we do anychange(Add,Edit OR Delete) on google drive folder for these purpose we have integrated google watch api in our spring boot application.
code snippet :
public Channel setUpWatch() throws IOException, GeneralSecurityException {
Channel channel = new Channel();
channel.setAddress("https://somedomain.com/notifications");
channel.setType("web_hook");
channel.setId(UUID.randomUUID().toString());
channel.setKind("api#channel");
StartPageToken pageToken =
driveServiceProvider.getDriveService().changes().getStartPageToken().execute();
System.out.println(pageToken.getStartPageToken());
Channel changesChannel = driveServiceProvider.getDriveService().changes()
.watch(pageToken.getStartPageToken(), channel).execute();
System.out.println(changesChannel.getExpiration());return
return changesChannel;
}
After running this code we are getting 200 in response, but we are not receiving any push notification from google when we do any operation in google drive. We are getting empty changeList
We are checking this code on local with no domain registration
Is there any other way to get the edit, add or deleted files list from google drive?
We do not want any notification on any domain or address. Whenever we trigger the api and if any file(s) is changed, we want that file(s) details like drive url, file name.
It seems that you are passing the current token to the changeList api.
This will return no results.
Actually, the solution is to find the changes since last token till the current token using a loop. So, store the last token somewhere and iterate till last token to current and pass the iterative value to chageList api to get the files changed.
Hope that works.
Push-notification channel will not send you requests for files inside a folder inside of a folder, just because you set up a watch on the folder itself.
To request push notifications, you need to set up a notification channel for each resource you want to watch.
If you preform a watch on a folder you will get a notification if for example the name of the folder is changed.
If you want to know if there are changes to a file then you will need to set up the watch for each of the files.

Reading emails from IMAP - how to flag them as SEEN when processed?

I'm reading and processing emails received from IMAP using JODD mail library. API is very nice but I struggle with one logical issue. I'm using code as following:
EmailFilter filter= new EmailFilter();
filter.flag(Flags.Flag.SEEN, false);
session.receiveEmailAndMarkSeen(filter);
By calling session.receiveEmailAndMarkSeen I receive all unread emails and these are marked as read immediately. Now when processing fails in my code for any reason, and I try to receive emails again all these unprocessed emails are marked as read already and not downloaded anymore. I would rather download emails and mark them as read individualy as beeing processed successfully.
So I tried to receive them with session.receiveEmail but not sure how to mark them as read when processed? Any hint how to do it? I can see that email object has 'flag' property I can set but not sure how to send this information back to server.
To summarize possible solutions:
Re-fetch email with Seen flag. The downside is that email is fetched again.
What you wrote - using a Session and a Folder.
Finally - starting from the next version of Jodd, you will have the method updateEmailFlags that would give you options to just call it:
mymail.flags(newFlags);
ReceiveMailSession.updateEmailFlags(mymail);
The result would be the same.
SOLVED: I'm creating connection manualy using common JAVA mail classes - Session and Store.
Session sess = Session.getDefaultInstance(props, null);
Store store = sess.getStore("imaps");
store.connect("imapServerHost", "username","password");
... then I create folder object (points to Inbox)
Folder folder = store.getFolder(this.imapFolder);
folder.open(Folder.READ_WRITE);
... then I receive emails using session and store
ReceiveMailSession session=new ReceiveMailSession(sess, store);
... after email processed, I send back SEEN=true message using folder object.
Flags f=new Flags();
f.add(Flags.Flag.SEEN);
folder.setFlags(new int[] {email.getMessageNumber()}, f,true);

How to properly delete a copied gmail message using javamail?

I have a scenario like this. I have copied a gmail message from Inbox to a child folder of Inbox, say test-folder. Now the message is there in Inbox and as well as in Inbox/test-folder. Now later if I want to delete (expunge) the copy in Inbox/test-folder using javamail, it is also being deleted from Inbox also.
I know that gmail maintains only 1 copy of the message in its database and it just tags folder names to the message, so it is obvious if I expunge it from other folder, it will also get deleted from the original folder.
The following code works for other IMAP based mails like yahoo and etc.
Folder inbox = store.getFolder("INBOX");
Folder child = store.getFolder("INBOX/test-folder");
inbox.open(Folder.READ_WRITE);
child.open(Folder.READ_WRITE);
AppendUID[] appendUIDs = inbox.copyUIDMessages(new Message[]{ message }, child);
AppendUID appendUID = appendUIDs[0];
long uid = appendUID.uid;
// EDIT: I have to close and reopen the child folder, otherwise getMessageByUID will return null.
child.close(false);
child.open(Folder.READ_WRITE);
Message copiedMessage = child.getMessageByUID(uid);
if (!copiedMessage.isExpunged() && !copiedMessage.isSet(Flags.Flag.DELETED)) {
copiedMessage.setFlag(Flags.Flag.DELETED, true);
}
inbox.close(true);
child.close(true);
The above code delete only the message in Inbox/test-folder, not from Inbox for Yahoo and all. But for gmail it deletes the message from Inbox as well as Inbox/test-folder.
Email client like evolution, handles this scenario properly for gmail. It deletes the message only from the target folder. So how to achieve this using javamail or gimap library?
NOTE: I am using 1.5.5 of the javamail library.
Seems like that should work, but Gmail doesn't exactly follow the imap spec. What does the debug output show?

Lotus Notes Java API. Mail forwarding

I would like to forward emails from my Lotus Notes inbox to my gmail account.
Lotus Notes rules and agents are disabled on our server, so I developed external application for that.
I am using document.send method and mail successfully arrives to my gmail box.
The only problem is that often the email also duplicated in my Lotus Notes inbox.
I just found that the reason of that is "CC" and "BCC" fields, which I don't clean up,
however, I am looking for the way to forward email as it is - which means keep original CC and BCC and TO fields - exactly on the same way as it is done by forwarding agent.
I am using "IBM Notes 9" on Windows 7 64 bit.
I've prepared a code sample that demonstrates what I am doing.
package com.example;
import lotus.domino.*;
public class TestMailForwarder {
public static void main(String[] args) throws NotesException {
NotesThread.sinitThread();
try {
Session notesSession = NotesFactory.createSession(
(String) null, (String) null, Consts.NOTES_PASSWORD);
DbDirectory dir = notesSession.getDbDirectory(Consts.NOTES_SERVER);
Database mailDb = dir.openDatabaseByReplicaID(Consts.MAILDB_REPLICA_ID);
forwardAllEmails(mailDb);
} finally {
NotesThread.stermThread();
}
}
private static void forwardAllEmails(Database mailDb) throws NotesException {
View inbox = mailDb.getView("$Inbox");
//noinspection LoopStatementThatDoesntLoop
for (Document document = inbox.getFirstDocument();
null != document;
document = inbox.getNextDocument(document)) {
document.send(Consts.GMAIL_ADDRESS);
break;
}
}
}
Instead of trying to send the messages to your GMail, why not upload them using Gmail's IMAP interface. You would require to get the message as MIME content - which probably they are already for external incoming eMails and then push them to GMail.
I don't have a ready code sample, just one for the opposite pulling GMail into Notes, but you should be able to use that as a starting point.
A code sample for the MIME conversion is in an IBM Technote.
Hope that helps
You can't do a transparent forward with code running at the client level. Pure SMTP systems do it by preserving the RFC-822 header content while altering the RFC-821 RCPT TO data. Domino does not give client-level code independent control over these. It just uses the SendTo, CopyTo, and BlindCopyTo items. (There are some tricks that mail management and archiving vendors play in order to do things like this, but they require special changes to the Domino server's router configuration, and software on the other end as well.
Another way of accomplishing this (in response to the question you asked in your comment) would be to have your Java code make a direct connection to the gmail SMTP servers. I'm not sure how easy it is. A comment on this question states that the Java Mail API allows you to control the RCPT TO separately from the RFC822 headers, but I've not looked into the specifics other than taking note that there's an SMTPTransport class -- which is where I'd look for anything related to RFC-821 protocol. The bigger issue is that you will have to take control of converting messages into MIME format. With Notes mail, you may have a mix of Notes rich text and MIME. Theres a convertToMIME method in Notes 8.5.1 and above, but this will only convert the message body. You'll have to deal with any header content separately. (I'm not really up to speed on Notes 9, but AFAIK even though there is functionality in the client to create a .EML file when you drag a message to the desktop, there's no API there to do that for you.)
Finally, I've found a ready solution: AWESYNC.MAIL.
It is a commercial software but it does exactly what I need.

How to capture "send mail" in plugin for IBM Lotus notes

Here is what I am trying to do:
Add a special button to attach files to Notes "New message" window. If files were attached using this button, when email sent, they should be uploaded to the server and link to them added to the email.
My question - is it possible (and how) to capture "send mail" event in the plugin for Lotus Notus?
I don't know how an Eclipse plugin would do this. Furthermore, since Notes can be used off-line -- when it would be impossible to upload files to a server -- it would be better to have code running on the Domino server intercept the mail messages and perform the upload.
Most products that hook mail operations on the server use the Lotus Notes C API's Extension Manager functions to hook the EM_BEFORE notification for the EM_NSFNOTEUPDATE event and check whether the NSFNoteUpdate operation occurred within the server's mail.box files, and then check whether the the message requires special processing (i.e., in your case that would be by looking for a special NotesItem that your button code has inserted into the message). The usual coding method for this is to immediately change the status of the message to put it on hold, preventing the Domino router from attempting to send the message while your code is still working on it. Many products actually have two components - the EM hook DLL and a separate server task that receives a signal from the hook DLL, processes the message, and then releases it from on hold status. This approach keeps your code from tying up router threads while processing large files.
(Note: Newer versions of the Domino server have the ability to use OSGI plugins written in Java instead of using the Notes C API for operations like this. I've not looked into the details of how this might work for operations that process mail messages. )
I sort of figured it out. There is a very nice extension point provided in 8.5 - "com.ibm.notes.mailsend.MailSendAttachmentsDialog", that is specifically exists for custom handling of attachments. You can see it in plugin.xml, in IBM\Lotus\Notes\framework\shared\eclipse\plugins\com.ibm.notes.mailsend_8.5.*.jar.
The only problem is - it handles just attachments and does not have access to anything else. So if somebody figured how to get subject line and the message text from there, please reply.
Update: got it.
NotesUIElement elem = (new NotesUIWorkspace()).getCurrentElement();
if (elem instanceof NotesUIDocument) {
NotesUIDocument doc = ((NotesUIDocument) elem);
String to = doc.getField("EnterSendTo").getText();
String cc = doc.getField("EnterCopyTo").getText();
String bcc = doc.getField("EnterBlindCopyTo").getText();
String subject = doc.getField("Subject").getText();
String body = doc.getField("Body").getText();
....
}

Categories