Can you send the mail from a servlet ,if yes tell how?

Answers were Sorted based on User's Feedback



Can you send the mail from a servlet ,if yes tell how?..

Answer / imtiyaz

WAY 1
-----
Using javamail api we can send and receive email from
different mail servers. To use javamail api we require
mail.jar & activation.jar which must be placed to set in
classpath.


import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*; // important
import javax.mail.event.*; // important
import java.net.*;
import java.util.*;
public class servletmail extends HttpServlet
{
public void doPost(HttpServletRequest
request,HttpServletResponse response) throws
ServletException, IOException
{
PrintWriter out=response.getWriter();
response.setContentType("text/html");
try
{
Properties props=new Properties();
props.put
("mail.smtp.host","localhost"); // 'localhost' for
testing
Session session1 = Session.getDefaultInstance
(props,null);
String s1 = request.getParameter
("text1"); //sender (from)
String s2 = request.getParameter("text2");
String s3 = request.getParameter("text3");
String s4 = request.getParameter("area1");
Message message =new MimeMessage(session1);
message.setFrom(new InternetAddress(s1));
message.setRecipients
(Message.RecipientType.TO,InternetAddress.parse(s2,false));
message.setSubject(s3);
message.setText(s4);
Transport.send(message);
out.println("mail has been sent");
}
catch(Exception ex)
{
System.out.println("ERROR....."+ex);
}
}
}

WAY 2
---------
By using net package also protocol you can send a mail from
specific smtp server servlet,
please go with the following link you can find a sample
program
http://web.bvu.edu/faculty/schweller/emailUsingServlet.htm

Is This Answer Correct ?    0 Yes 0 No

Can you send the mail from a servlet ,if yes tell how?..

Answer / ravikiran.chd

yes we can send a mail with the help of send mail or simple
mail API,
we have to use port 25 to send the mail and pop before
authentication is required if the mail we have to send have
to go to the inbox

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Servlets Interview Questions

What happens, when client requests for server object, which is not yet loaded into the memory?

2 Answers  


why are using HttpServlet in realtime projects and why are not using Genericservlet

2 Answers  


Why do we have servlet filters?

0 Answers  


where the session data will stored?

4 Answers  


Which event is fired at the time of setting, getting or removing attribute from application scope?

0 Answers  






When the methods init() and Distroy() will be called?

2 Answers  


Which exception is thrown if the servlet is not initialized properly?

0 Answers  


what is meant by servlet to servlet communication?

1 Answers   KPIT,


Hi frnd can i any one kindly can post for me portlet,hibernate and spring example application and with flow explantion configuration using Jdeveloper.and related links ar tutorials kindly please send me .its urgent for me .thanks in advance...........else can any one send to kondaiah.goddeti@gmail.com

0 Answers  


Explain web container.

0 Answers  


can we create more than ServletContext and ServletConfig in your application

6 Answers   Patni,


What is the web server used for running the Servlets?

0 Answers  


Categories