Friday, January 21, 2011

Email with Java and JSP

Hey frnds. I am back with some more codes.

If you want to use mail api in your application or website you can use this code.
I am writing here codes in JAVA and JSP both.

import javax.mail.*;
import java.io.*;
import java.util.*;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import java.util.Properties;
import javax.mail.internet.*;
import javax.activation.*;

public class TestMail
{
public static void main(String[] args)
{
try
{
String to = "nisha.barthwal@mivas.in";
String cc = "sampan.saini@mivas.in";
String bcc = "dharmesh.mahawar@mivas.in";
String from = "medha.vadi@mivas.in";
String subject = "Hi There...";
String text = "How are you?";

Properties properties = new Properties();
properties.put("mail.trasport.protocol","smtp");
properties.put("mail.smtp.host", "smtpout.secureserver.net");
properties.put("mail.smtp.port", "80");
properties.put("mail.smtp.auth","true");
//Session session = Session.getDefaultInstance(properties, null);
//Session session = Session.getInstance(properties,null);
Session session=Session.getInstance(properties, new MailAuthenticator());
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
message.setRecipient(Message.RecipientType.BCC, new InternetAddress(bcc));
message.setSubject(subject);
message.setText(text);

Transport.send(message);
}


catch(MessagingException e)
{
System.out.println("Exception found @ e"+e);
}
catch(IllegalStateException ex)
{
System.out.println("Exception found @ ex"+ex);
}
}//end of main

}//end of class TestMail

class MailAuthenticator extends Authenticator {

public MailAuthenticator() {
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("medha.vadi@mivas.in", "password");
}
}


For this you have to keep 2 jar files inside your jdk/jre/lib/ext and jre6/lib/ext folder
1st jar file is activation.jar file and 2nd file is mail.jar. You can download these jar files from www.findjar.com and www.java2s.com.



Now in JSP

<%@ page import="java.io.*,java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*,javax.activation.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@page import="javax.mail.Authenticator"%>
<%@page import="javax.mail.PasswordAuthentication"%>

<%
String result;

try
{
String host = "smtpout.secureserver.net";
String to="sampan.saini@mivas.in";
String from = "helpdesk@mivas.in";
// String cc = "nisha.barthwal@mivas.in";
String pass = "pass";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
//props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "80");
props.put("mail.smtp.auth", "true");

Session session1 = Session.getInstance(props, new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("sampan.saini@mivas.in","omsairam");
}
});
MimeMessage message = new MimeMessage(session1);
Address fromAddress = new InternetAddress(from);
Address toAddress = new InternetAddress(to);
// Address ccAddress = new InternetAddress(cc);

message.setFrom(fromAddress);
message.setRecipient(Message.RecipientType.TO, toAddress);
//message.setRecipient(Message.RecipientType.CC, ccAddress);

message.setSubject("Tech Support");
message.setText("Hi,\n Please Give the solution of problem ID: ");
Transport transport = session1.getTransport("smtp");
transport.connect(host, from, pass);
message.saveChanges();
Transport.send(message);
out.println("------Your Mail Sent Successfully-----");
transport.close();

}
catch(Exception ex)
{

out.println("");
out.println("ERROR: " + ex);
out.println("");
}
%>

2 comments: