import javax.mail.*
import java.util.Properties
def emailAddress = "you@gmail.com"
def password = "password"
def server = "imap.gmail.com"
def port = 993
def props = new Properties()
props.setProperty("mail.store.protocol", "imaps")
props.setProperty("mail.imaps.host", server)
props.setProperty("mail.imaps.port", port.toString())
//very important or you will get errors such as Exception in thread "main" com.sun.mail.util.DecodingException: BASE64Decoder: Error in encoded stream: needed 4 valid base64 characters but only got 1 before EOF, the 10 most recent characters were: "Q3w5ilxj2P"
props.setProperty("mail.imaps.partialfetch", "false");
def session = javax.mail.Session.getDefaultInstance(props, null)
def store = session.getStore("imaps")
store.connect(emailAddress, password)
println "connected to Gmail"
def inboxFolder = "INBOX"
def folder = store.getFolder(inboxFolder)
folder.open(Folder.READ_ONLY)
javax.mail.search.SearchTerm searchTerm = new javax.mail.search.AndTerm(
new javax.mail.search.SubjectTerm("SUBJECT YOUR ARE SEARCHING FOR"),
)
def messages = folder.search(searchTerm)
messages.eachWithIndex {message, i ->
def content = message.content
if (content instanceof javax.mail.Multipart) {
javax.mail.Multipart mp = (javax.mail.Multipart) content;
for (int j = 0; j < mp.getCount(); j++) {
javax.mail.Part part = mp.getBodyPart(j)
String disposition = part.getDisposition()
if (disposition != null && disposition.equalsIgnoreCase(javax.mail.Part.ATTACHMENT)) {
saveFile(part.getFileName(), part.getInputStream())
}
}
}
}
def saveFile(String fileName, InputStream inputStream) {
println 'Saving file: ' + fileName
def file = new File('/tmp/' + fileName)
if (file.exists()) {
for (int i = 0; file.exists(); i++) {
file = new File('/tmp/' + i + '_' + fileName)
}
}
file.append(inputStream)
println 'File saved: ' + fileName
}
java_dood
“The best thing about a boolean is even if you are wrong, you are only off by a bit.”
Sunday, October 16, 2011
Download GMail email attachments by subject
I used to use my work scanner to scan all of my documents, photos, etc. So recently I needed to find something. However, since all of them are labeled as Document.pdf I had hard time finding what I was looking.
My options were to download my mail using Thunderbird and then search for those emails or to download one attachment at the time.
I did not like either. So, I wrote this simple groovy script that downloads attachments for a specific email subject.
US States Enum
So you can use it in your gsp pages
enum State {
Alabama("AL"),Montana("MT"),Alaska("AK"),Nebraska("NE"),
Arizona("AZ"),Nevada("NV"),Arkansas("AR"),NewHampshire("NH"),
California("CA"),NewJersey("NJ"),Colorado("CO"),NewMexico("NM"),
Connecticut("CT"),NewYork("NY"),Delaware("DE"),NorthCarolina("NC"),
Florida("FL"),NorthDakota("ND"),Georgia("GA"),Ohio("OH"),Hawaii("HI"),
Oklahoma("OK"),Idaho("ID"),Oregon("OR"),Illinois("IL"),Pennsylvania("PA"),
Indiana("IN"),RhodeIsland("RI"),Iowa("IA"),SouthCarolina("SC"),Kansas("KS"),
SouthDakota("SD"),Kentucky("KY"),Tennessee("TN"),Louisiana("LA"),Texas("TX"),
Maine("ME"),Utah("UT"),Maryland("MD"),Vermont("VT"),Massachusetts("MA"),
Virginia("VA"),Michigan("MI"),Washington("WA"),Minnesota("MN"),
WestVirginia("WV"),Mississippi("MS"),Wisconsin("WI"),Missouri("MO"),Wyoming("WY")
final String value
State(String value) {
this.value = value
}
String toString() {
value
}
String getKey() {
name()
}
}
Tuesday, December 14, 2010
Java compare
Collections.sort(data, new Comparator(){
public int compare(Object o1, Object o2) {
return o2.get(1).compareTo(o1.get(1));
}
});
Saturday, August 14, 2010
mvn + grails
mvn archetype:generate \
-DarchetypeGroupId=org.grails \
-DarchetypeArtifactId=grails-maven-archetype \
-DarchetypeVersion=1.3.3 \
-DarchetypeRepository=http://snapshots.repository.codehaus.org \
-DgroupId=com.ndnlogic \
-DartifactId=test-grails
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [archetype:generate] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Preparing archetype:generate
[INFO] No goals needed for project - skipping
[INFO] [archetype:generate {execution: default-cli}]
[INFO] Generating project in Interactive mode
[INFO] Archetype defined by properties
[INFO] Using property: groupId = com.ndnlogic
[INFO] Using property: artifactId = qotd
Define value for property 'version': 1.0-SNAPSHOT: 0.1
[INFO] Using property: package = com.ndnlogic
Confirm properties configuration:
groupId: com.ndnlogic
artifactId: qotd
version: 0.1
package: com.ndnlogic
Y: y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: grails-maven-archetype:1.3.3
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.ndnlogic
[INFO] Parameter: packageName, Value: com.ndnlogic
[INFO] Parameter: package, Value: com.ndnlogic
[INFO] Parameter: artifactId, Value: qotd
[INFO] Parameter: basedir, Value: /home/nem/code/grails
[INFO] Parameter: version, Value: 0.1
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: /home/nem/code/grails/qotd
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 29 seconds
[INFO] Finished at: Sat Aug 14 11:50:51 EDT 2010
[INFO] Final Memory: 14M/130M
[INFO] ------------------------------------------------------------------------
Friday, September 05, 2008
IM Bot to update Twitter
So for all of you twitters out there here is a little tutorial how to write your own IM bot to easily update twitter using IM client. I know i knoq you can use the official IM twitter bot BUT it is down right now. So, why not use your own.
First of all read post How to Write Your Own IM Bot in Less Than 5 Minutes
Follow all the steps for creating your own im bot.
Next add the following code to the .php script from the IM Bot blog post.
enjoy!
First of all read post How to Write Your Own IM Bot in Less Than 5 Minutes
Follow all the steps for creating your own im bot.
Next add the following code to the .php script from the IM Bot blog post.
// Set username and password
$username = 'user';
$password = 'pass';
// The message you want to send
$message = $_REQUEST['msg'];
// The twitter API address
$url = 'http://twitter.com/statuses/update.xml';
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$url");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$message");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
echo 'message';
} else {
echo 'success';
}
*/
?>
Thats it!enjoy!
Friday, November 16, 2007
Subscribe to:
Posts (Atom)
