Sample Codes for Programming Languages

>> Friday, April 18, 2008

Sometimes when I wanted to find out how something can be done using Java my obvious choice was to goto exampledepot.com (previously known as javaalmanac.com).

I am wandering whether there are any sites like this for other programming languages, which will be really useful.

Read more...

First Earthquake Experience

This morning around 5.41 am I felt like my bed was shaking. It last about 30-60 seconds. I thought it should be an earthquake but also thought it can be due to a tornado also. It went off and I couldn't figure out what it was.

But this news, says everything about it. I'm glad my first earthquake experience is not so bad.

Read more...

Microsoft Moves in to PC Market with a Low Cost Laptop

>> Tuesday, April 15, 2008

Link to original news
This seems to be great news from Microsoft. I am not sure whether Microsoft is only responsible for WinXP in this laptop, but whatever it is, this might not be a good sign for other PC vendors in the long term.
Can this be the first sign for a laptop which can only run Windows XXX with Windows XXX version built into a flash (or whatever) memory in it?

Read more...

Use java to send mail using your gmail account

>> Sunday, April 13, 2008

One might ask why bother using Java (or any other programming language) to send mail using GMail, since you can easily do that using GMail web. But consider you want to send the same mail to some one, so that they will think the mail was sent only to them (personalize mass email).
(Today it is Sinhala New year festival, in Sri Lanka. So this was a great tool for me to wish everyone).

I tried to use the basic mail client with Google, but it didn't work out as GMail needs more settings. But I found the best Android hacker, aka Dims had put up a nice blog on using Android to send mails with GMail. So I extracted some code from that for this task.
First get activation.jar and mail.jar and put them in your classpath.

Put your recipients list in a simple text file like this and name it recipients.txt.


John=john@greatplace.com
Siripala=siri@srilanka.net


Then use the following code. Make sure you put valid gmail username and a password.


import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

/**
* @author : Eran Chinthaka
* @date : Apr 13, 2008
*/
public class GMailClient extends Authenticator {

// Edit these settings with your username and password
private String gmailUserName = "username";
private String gmailPassword = "password";
//=====================================================

public void postMail(String recipients[], String subject,
String message, String from)
throws MessagingException {
boolean debug = false;

//Set the host smtp address
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");

Session session = Session.getDefaultInstance(props, this);

session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);

InternetAddress[] addressTo =
new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);

// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/html");
Transport.send(msg);
}


public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(gmailUserName, gmailPassword);
}


public static void main(String[] args) {
try {

String message = "Your HTML Message goes here";
String subject = "This is the subject";
String fromAddress = "from@whatever.com";

GMailClient mailClient = new GMailClient();

BufferedReader in = new BufferedReader(
new FileReader("recipients.txt"));
String str;
while ((str = in.readLine()) != null) {
String[] details = str.split("=");
String name = details[0];
String email = details[1];
mailClient.postMail(new String[]{email}, subject,
"Hi " + name + ", \n" + message, fromAddress);

}
in.close();
} catch (IOException e1) {
e1.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}

Read more...