javamail mark gmail message as read

First of all, you can’t mark a message as read if you are using a POP3 server – the POP3 protocol doesn’t support that. However, the IMAP v4 protocol does. You might think the way to do this is to get the message, set the Flags.Flag.SEEN flag to true, and then call message.saveChanges(). Oddly, this … Read more

Retrieving all unread emails using javamail with POP3 protocol

Your code should work. You can also use the Folder.getUnreadMessageCount() method if all you want is the count. JavaMail can only tell you what Gmail tells it. Perhaps Gmail thinks that all those messages have been read? Perhaps the Gmail web interface is marking those messages read? Perhaps you have another application monitoring the folder … Read more

How to set MimeBodyPart ContentType to “text/html”?

Call MimeMessage.saveChanges() on the enclosing message, which will update the headers by cascading down the MIME structure into a call to MimeBodyPart.updateHeaders() on your body part. It’s this updateHeaders call that transfers the content type from the DataHandler to the part’s MIME Content-Type header. When you set the content of a MimeBodyPart, JavaMail internally (and … Read more

Postfix and OpenJDK 11: “No appropriate protocol (protocol is disabled or cipher suites are inappropriate)”

I was facing the same issue using JDK 11. But I resolved it by commenting this line in the java.security file: jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, MD5withRSA, DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, include jdk.disabled.namedCurves

How to attach multiple files to an email using JavaMail?

Well, it’s been a while since I’ve done JavaMail work, but it looks like you could just repeat this code multiple times: DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); For example, you could write a method to do it: private static void addAttachment(Multipart multipart, String filename) { DataSource source = new FileDataSource(filename); BodyPart … Read more

JavaMail API from Maven

The Maven coordinates changed some time ago to be compatible with the scheme described here. The new coordinates are here. In short: The groupId javax.mail is no longer used for the implementation. There is a new artifact at javax.mail:javax.mail-api. It provides the javax.mail-api.jar file. This contains the JavaMail API definitions only, suitable for compiling against. … Read more

How to get the list of available folders in a mail account using JavaMail

Sergey is close, but by default JavaMail’s list() does a LIST “” %, which gives you only top-level folders. GMail puts its system folders (All Mail, Drafts, Sent Mail, Spam, Starred, and Trash) under the non-selectable folder [Gmail], so you really need to do a LIST “” * instead. Otherwise, you’ll just get back INBOX, … Read more

tech