Monday, December 14, 2009

Graphical + Attachment java Email Client

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EmailClient {

private static final String SMTP_HOST_NAME = "";
private static final String SMTP_AUTH_USER =;
private static final String SMTP_AUTH_PWD = "xxx";

public static void main(String[] args) throws Exception {

new SimpleMailWithAttachment().test();


public void test() throws Exception {
Properties props = new Properties();

props.put("mail.transport.protocol", "smtp");
props.put("", SMTP_HOST_NAME);

Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing javamail with attachment");
MimeBodyPart textPart = new MimeBodyPart();
textPart.setContent("<h1>Check attachment</h1>", "text/html");
MimeBodyPart attachFilePart = new MimeBodyPart();

FileDataSource fds =
new FileDataSource("d:\\");

attachFilePart.setDataHandler(new DataHandler(fds));
Multipart mp = new MimeMultipart();
new InternetAddress(""));
transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER,

Friday, December 11, 2009

Disable enter key using java script

Use the below code to disable enter key. You can also used this to disable un wanted keys,too. Just add more combination of keys after "evt.keyCode == 13".
<script language=javascript type=text/javascript>
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = ( ? : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13) {return false;}

document.onkeypress = stopRKey;

<textarea rows="5"></textarea>

Thursday, December 10, 2009

How to write & read cookie in java?

In this jsp page you can pass the value you want to set as your cookie.

<%@ page language="java" %>
<title>Cookie Input Form</title>

<form method="post" action="setcookie.jsp">
<p><b>Enter Your Name: </b><input type="text" name="username"><br>
<input type="submit" value="Submit">


This page captures the cookie input value and set cookie in client hard disk.

<%@ page language="java" import="java.util.*"%>
String username=request.getParameter("username");
if(username==null) username="";

Date now = new Date();
String timestamp = now.toString();
Cookie cookie = new Cookie("username",username);
cookie.setMaxAge(365 * 24 * 60 * 60);

<title>Cookie Saved</title>
<p><a href="showcookievalue.jsp">Next Page to view the cookie value</a><p>


This is a page you can to use to see the cookie value later time.
<%@ page language="java" %>
String cookieName = "username";
Cookie cookies [] = request.getCookies ();
Cookie myCookie = null;
if (cookies != null)
for (int i = 0; i < cookies.length; i++)
if (cookies [i].getName().equals (cookieName))
myCookie = cookies[i];
<title>Show Saved Cookie</title>
if (myCookie == null) {
No Cookie found with the name <%=cookieName%>
} else {
<p>Welcome: <%=myCookie.getValue()%>.

Wednesday, December 2, 2009

Use keytool to generate keys in java

  1. keytool -genkey -alias weblogic -keyalg RSA -keystore server.keystore
  2. keytool -selfcert -export -alias weblogic -storepass changeit -file server.cer -keystore server.keystore
  3. keytool -genkey -alias client -keyalg RSA -keystore client.keystore
  4. keytool -selfcert -export -alias client -storepass changeit -file client.cer -keystore client.keystore
  5. keytool -import -v -trustcacerts -alias weblogic -file server.cer -keystore client.keystore -keypass changeit -storepass changeit
  6. keytool -keystore \jre\lib\security\cacerts -storepass changeit -file client.cer -import -alias client -trustcacerts
Here, i have created server.cer and client.cer certificates. Password used is "changeit".