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 = "smtp.gmail.com";
private static final String SMTP_AUTH_USER =
xxx@gmail.com;
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("mail.host", SMTP_HOST_NAME);

Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
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:\\TwainExample.java");

attachFilePart.setDataHandler(new DataHandler(fds));
attachFilePart.setFileName(fds.getName());
Multipart mp = new MimeMultipart();
mp.addBodyPart(textPart);
mp.addBodyPart(attachFilePart);
message.setContent(mp);
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("
xxx@yahoo.com"));
transport.connect(SMTP_HOST_NAME, SMTP_AUTH_USER,
SMTP_AUTH_PWD);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}

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".
<html>
<head>
<script language=javascript type=text/javascript>
<!--
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if (evt.keyCode == 13) {return false;}
}

document.onkeypress = stopRKey;
-->
</script>
</head>

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

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" %>
<html>
<head>
<title>Cookie Input Form</title>
</head>
<body>

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

</body>


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);
response.addCookie(cookie);


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

</body>

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];
break;
}
}
}
%>
<html>
<head>
<title>Show Saved Cookie</title>
</head>
<body>
<%
if (myCookie == null) {
%>
No Cookie found with the name <%=cookieName%>
<%
} else {
%>
<p>Welcome: <%=myCookie.getValue()%>.
<%
}
%>
</body>

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".