dcsimg
How to Configure JavaMail with TomcatServer or SMTP Server
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Fareed_md
Posted On:   Sunday, May 23, 2004 06:20 AM

Hi !

I am totally new to JavaMail. I do have good knowledge of Core Java.Please send me Step by step procedures.

How to Configure Tomcat for JavaMail ??
Where to save the Class files and other files ??
What are the Environment variables that are to be set?
Do I need to have some other SMTP server apart from Tomcat ?
Where does this SMTP Server stand in whole scenario ??

Re: How to Configure JavaMail with TomcatServer or SMTP Server

Posted By:   Vijay_Kumar  
Posted On:   Wednesday, July 7, 2004 12:34 AM

Hi,

here I am giving you how to configure javamail in tomcat step-by-step,


Step 1: Servlet for sending mail "SendMailServlet.java"


// import the JavaMail packages

import javax.mail.*;

import javax.mail.internet.*;



// import the servlet packages

import javax.servlet.*;

import javax.servlet.http.*;



// import misc classes that we need

import java.util.*;

import java.io.*;



public class SendMailServlet extends HttpServlet

{

String smtpServer;



public void init(ServletConfig config) throws ServletException

{

super.init(config);

}


public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException

{

// get the message parameters from the HTML page
String from = req.getParameter("from");

String to = req.getParameter("to");

String subject = req.getParameter("subject");

String text = req.getParameter("text");




PrintWriter out = res.getWriter();

res.setContentType("text/html");



try

{

// set the SMTP host property value



Properties properties = System.getProperties();

properties.put("mail.smtp.host", "www.yourdomain.com"); //any domain address who provide smtp service



// create a JavaMail session

Session session = Session.getDefaultInstance(properties);



// create a new MIME message

MimeMessage message = new MimeMessage(session);



// set the from address

Address fromAddress = new InternetAddress(from);

message.setFrom(fromAddress);



// set the to address

if (to != null)

{

Address[] toAddress = InternetAddress.parse(to);

message.setRecipients(Message.RecipientType.TO, toAddress);

}

else

throw new MessagingException("No "To" address specified");



// set the subject

message.setSubject(subject);



// set the message body

message.setText(text);



// send the message

Transport transport = session.getTransport("smtp");



Transport.send(message);



out.println("Message sent successfully.");

}

catch (AddressException e)

{

out.println("Invalid e-mail address.
" + e.getMessage());

}

catch (SendFailedException e)

{

out.println("Send failed.
" + e.getMessage());

System.out.println("Detail:->
");

e.printStackTrace();

}

catch (MessagingException e)

{

out.println("Unexpected error.
" + e.getMessage());

}

catch (Exception e)

{

System.out.println("* Exception:->
"+e);

e.printStackTrace();

}

}

}



compile this servlet be sure that you have mail.jar & activation.jar in your classpath.



step 2. HTML to access servlet SendMailServlet.html





















From:
To:
Subject:










step 3. create WEB-INF directory in your main directory & put web.xml in that











SendMailServlet

SendMailServlet

SendMailServlet





SendMailServlet

/SendMailServlet








step 4. put SendMailServlet's class file in WEB-INF/classes directory


step 5. create SendMailServlet.war in your main directory

jar cvf SendMailServlet.war *.*



step 6. put this war file in your tomcat's webapp, start
tomcat access url
http://localhost:8080/SendMailServlet/SendMailServlet.html

& send mail
About | Sitemap | Contact