Skip to content

Instantly share code, notes, and snippets.

@jresendiz27
Created November 7, 2014 20:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jresendiz27/a205a27d0704a60b18c3 to your computer and use it in GitHub Desktop.
Save jresendiz27/a205a27d0704a60b18c3 to your computer and use it in GitHub Desktop.
Using Groovy to read mails from GMail. Just for development purposes, GMail will not allow login in first time, so I enabled the option to log from non secure apps SSL it's necessary Another good tutorial coudl be find here using SSL: http://agileice.blogspot.mx/2008/10/using-groovy-to-connect-to-gmail.html
/*
Using Groovy to read email form GMail.
Just for development purposes, GMail will not allow in first time the login, so I enabled the option to log from non secure apps
SSL it's necessary
Another good tutorial coudl be find here using SSL:
http://agileice.blogspot.mx/2008/10/using-groovy-to-connect-to-gmail.html
*/
@Grab(group='javax.mail', module='mail', version='1.4.7')
import javax.mail.*
import javax.mail.internet.*;
import javax.mail.search.*;
def host = "imap.gmail.com"
def port = "993"
def username = "your-mail@gmail.com"
def password = "password"
Properties props = new Properties()
props.setProperty("mail.store.protocol", "imaps")
props.setProperty("mail.imap.host", host)
props.setProperty("mail.imap.port", port)
props.setProperty("mail.imap.ssl.enable", "true");
def session = Session.getDefaultInstance(props, null)
def store = session.getStore("imaps")
store.connect(host, username, password)
def folder = store.getFolder("INBOX")
folder.open(Folder.READ_ONLY)
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
Message[] msgs = folder.search(unseenFlagTerm);
//
FetchProfile fetchProfile = new FetchProfile()
fetchProfile.add(FetchProfile.Item.ENVELOPE)
folder.fetch(msgs,fetchProfile)
//
for ( i in 0..(msgs.length -1) ) {
println "***************************************************"
println "***************************************************"
println"${msgs[i].receivedDate}"
println "${msgs[i].sender}"
println "${msgs[i].from}"
println "${msgs[i].subject}"
msgs[i].writeTo(System.out)
println "***************************************************"
println "***************************************************"
}
@kirankumar-webpro
Copy link

How to get the Very First mail in Gmail Using Groovy.

@AchmadSyah27
Copy link

yes, how to get last email in gmail using groovy?

@peter18775
Copy link

Just remove the for loop and instead of i use -1 as python index.

@DNovS
Copy link

DNovS commented Feb 9, 2024

When calling the script, the error "failed to connect" is called, although the specified password and login are valid. Imap support is enabled in the mailbox. Gmail needs additional permissions - it's easier to refuse google because they didn't do it humanly. I used zoho - the same problem
Stack: Apache Nifi, Groovy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment