Showing posts with label mail. Show all posts
Showing posts with label mail. Show all posts

Thursday, March 22, 2012

How to read IMAP email with Groovy

There is quick snippet how to read email from Google IMAP with Groovy:

@Grab(group='javax.mail', module='mail', version='1.4')

import javax.mail.*
import java.util.Properties

def session = Session.getDefaultInstance(new Properties(["mail.store.protocol":"imaps", "mail.imaps.host":"imap.gmail.com", "mail.imaps.port":"993"]),null)
def store = session.getStore("imaps")
store.connect('imap.gmail.com', 'test@gmail.com', 'pass')
def folder = store.getFolder("INBOX")
folder.open(Folder.READ_WRITE)
folder.messages.each { msg ->
  println msg.content
}