Skip to content

Instantly share code, notes, and snippets.

@vergeev
Last active July 31, 2022 02:26
Show Gist options
  • Save vergeev/8ee381325adabc39cc8439ea82681a9c to your computer and use it in GitHub Desktop.
Save vergeev/8ee381325adabc39cc8439ea82681a9c to your computer and use it in GitHub Desktop.
Opens mailbox on yandex.ru with IMAP, reads the last message and all unread ones.
import com.sun.mail.imap.IMAPMessage;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.FlagTerm;
public void readYandexMailbox(String login, String password) throws MessagingException, IOException {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imaps.ssl.trust", "imap.yandex.ru");
Session session = Session.getDefaultInstance(props, null);
Store store;
store = session.getStore();
store.connect("imap.yandex.ru", 993, login, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE); // even though we only want to read, write persmission will allow us to set SEEN flag
try {
Message lastMessage = inbox.getMessage(inbox.getMessageCount());
((IMAPMessage)lastMessage).setPeek(true); // this is how you prevent automatic SEEN flag
MimeMessage cmsg = new MimeMessage((MimeMessage)lastMessage); // this is how you deal with exception "Unable to load BODYSTRUCTURE"
printMessage(cmsg);
markMessageAsSeen(lastMessage, inbox);
FlagTerm unreadFlag = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message unreadMessages[] = inbox.search(unreadFlag);
for (Message unreadMessage : unreadMessages) {
// SEEN flag here will be set automatically.
printMessage(unreadMessage);
}
} finally {
inbox.close(true);
}
}
private void printMessage(Message message) throws MessagingException, IOException {
Address[] addresses = message.getFrom();
for (Address address : addresses) {
System.out.println("FROM: " + address.toString());
}
System.out.println("SUBJECT:" + message.getSubject());
System.out.println("TEXT:" + getText(message).trim());
}
private void markMessageAsSeen(Message message, Folder folder) throws MessagingException {
folder.setFlags(new Message[] {message}, new Flags(Flags.Flag.SEEN), true);
}
private boolean textIsHtml = false; // if test is html, use html parser (e.g. org.jsoup)
// copy-paste from http://www.oracle.com/technetwork/java/javamail/faq/index.html#mainbody
private String getText(Part p) throws
MessagingException, IOException {
if (p.isMimeType("text/*")) {
String s = (String)p.getContent();
textIsHtml = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
Multipart mp = (Multipart)p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment