By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,737 Members | 1,971 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,737 IT Pros & Developers. It's quick & easy.

problem in my Email integration servlet program

P: 83
Hi
I prepared a program for Email Integration. That is email send to others through servlet's. I gave the code but it has some error in my code. Please check the code and give the feedback.

The Servlet code:

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4.  
  5. public class MailServlet extends HttpServlet  {
  6.  
  7.         protected void doPost(HttpServletRequest request, HttpServletResponse response) 
  8.                     throws ServletException, IOException 
  9. {
  10.     System.out.println("Entering MailServlet");
  11.  
  12. // extract parameters from HttpServletRequest object
  13.     String name = (String)request.getParameter("name");
  14.     String email = request.getParameter("email");
  15.     String messageBody = request.getParameter("message");
  16.     String subject = "Mail from MailServlet";
  17.     String destinationAddress = "sangeetha@indexcareer.com"; 
  18.     try 
  19.     {
  20.         // look up MailSession
  21.         Context context = new InitialContext();
  22.         Session mailSession = (Session)context.lookup("java:comp/env/mySession");
  23.         Message msg = new MimeMessage(mailSession);
  24.         msg.setFrom(new InternetAddress(email));
  25.         msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(destinationAddress));// Set the subject and body text
  26.         msg.setSubject(subject);
  27.         msg.setText(messageBody); // send message
  28.         Transport.send(msg);
  29.         System.out.println("Message Sent");
  30.         javax.servlet.RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sent.jsp"); 
  31.         dispatcher.forward(request,response); 
  32.     }
  33. catch (NamingException e) 
  34.     {
  35.         e.printStackTrace();
  36.         javax.servlet.RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp"); 
  37.         dispatcher.forward(request,response); 
  38.     }
  39. catch (Exception e)
  40.     {
  41.         e.printStackTrace();
  42.         javax.servlet.RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp"); 
  43.         dispatcher.forward(request,response);
  44.     }
  45.     System.out.println("Exiting MailServlet");
  46. }
  47. }
  48.  
The Html code is

[HTML]<FORM action="/JavaMailDemo/MailServlet" method="post">
Name: <INPUT type="text" name="name" size="20"><BR>
E-mail: <INPUT type="text" name="email" size="20"><BR>
Message: <TEXTAREA rows="3" cols="5" name="message"></TEXTAREA>
<BR>
<BR>
<INPUT type="submit" name="Submit" value="Submit">
</FORM>
[/HTML]
please help to solve the problem.

Regrads
Sang......
Dec 27 '06 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
Hi
I prepared a program for Email Integration. That is email send to others through servlet's. I gave the code but it has some error in my code. Please check the code and give the feedback.

The Servlet code:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class MailServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("Entering MailServlet");

// extract parameters from HttpServletRequest object
String name = (String)request.getParameter("name");
String email = request.getParameter("email");
String messageBody = request.getParameter("message");
String subject = "Mail from MailServlet";
String destinationAddress = "sangeetha@indexcareer.com";
try
{
// look up MailSession
Context context = new InitialContext();
Session mailSession = (Session)context.lookup("java:comp/env/mySession");
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(email));
msg.setRecipients(Message.RecipientType.TO,Interne tAddress.parse(destinationAddress));// Set the subject and body text
msg.setSubject(subject);
msg.setText(messageBody); // send message
Transport.send(msg);
System.out.println("Message Sent");
javax.servlet.RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sent.jsp");
dispatcher.forward(request,response);
}
catch (NamingException e)
{
e.printStackTrace();
javax.servlet.RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp");
dispatcher.forward(request,response);
}
catch (Exception e)
{
e.printStackTrace();
javax.servlet.RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp");
dispatcher.forward(request,response);
}
System.out.println("Exiting MailServlet");
}
}

The Html code is

<FORM action="/JavaMailDemo/MailServlet" method="post">
Name: <INPUT type="text" name="name" size="20"><BR>
E-mail: <INPUT type="text" name="email" size="20"><BR>
Message: <TEXTAREA rows="3" cols="5" name="message"></TEXTAREA>
<BR>
<BR>
<INPUT type="submit" name="Submit" value="Submit">
</FORM>

please help to solve the problem.

Regrads
Sang......
Strictly speaking this should have been posted in the java forum. You said it gave an error. What type of error was it? Explain the problem in detail so that it will be easier to give suggestions.
Dec 27 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.