Monthly Archives: July 2014

Accessing Gmail using the JavaMail API

One of the basic features that I want on Gmail is the option to delete the mails within two selected dates.I am not very sure if that is possible, but the idea really inspired me to get hands on in the JavaMail API. It is very easy to use and configure in your own program in eclipse.

Though I used the JavaMail 1.4.7 for the purpose of testing, you can download the latest from here.

After you have downloaded, create a project and go to Properties->Java Build Path. Click on add external jars and browse the path where the jar file is located. Next is to create a class and inside the main program create a Property list as below;

Properties props = new Properties();


Then depending upon the mailer client we want to access we would put the values. Below I try to access my Gmail, so I have given the values as:

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.host", "smtp.gmail.com");

props.put("mail.smtp.port", "587");

Once the properties list is set, we would be required to create the Session object using the below code:


String username="abc@gmail.com";
String password="Password123";
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});

Now the MimeMessage object needs to be created where we set the sender’s email id, receipient email id, subject and text .


message.setFrom(new InternetAddress("abc@gmail.com"));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("xyz@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear,"
+ "\n\n This is a test email!");

Transport.send(message);

Just type the above codes. Run the program and check the gmail inbox. Test mail should be there.
Cheers.. 🙂

Advertisements

Opening a popup on a text box click in Struts using javascript

About a week ago, while working on a project, I had the requirement of opening a popup window if a user clicks on a text box. I knew I would use JavaScript but didn’t know what to write and how to. I googled a bit and finally could create a popup. First of all I wrote the jsp page as :


<td><s:textfield name="txtUserName" label="UserName" readonly="true" value="%{#session['up'].userName.userName}" cssStyle="background:grey;" onclick="popitup('settings/popup.jsp')"></td>

Now coming to the JavaScript function, we can either create a different myjavascript.js page and link it in the heading section of the JSP page as:


<script type="text/javascript" src="myjsfolder/myjavascript.js"></script>

Otherwiese we can just include the function in

<script>

// Put javascript function here

</script>

Finally comes the JavaScript function that would do the work,

So here it is:


function popitup(url) {

var newwindow = window.open(url, 'popup', 'width=250,height=200,top=100,left=100');

}

Simple its done , just click the textbox and the popup window should open. 🙂