Created
November 7, 2014 20:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 "***************************************************" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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