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

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the use of httpservletresponsewrapper?

701


How forward () method is different from send redirect () method?

705


Explain the servlet filter.

707


Explain servlet life cycle?

820


What's the advantages using servlets than using cgi?

720


What is the difference between servlet and filter?

710


What is java servlet session?

708


What’s the use of the servlet wrapper classes??

829


Difference between httpservlet and generic servlets?

811


What is lazy loading and what is Generic Servlet Class?

807


List the Different types of servlet?

775


What do you mean by scope object and what are its types?

767


How can I send user authentication information while making URL Connection?

729


How to get the server information in a servlet?

880


How to commuincate between an applet and a servlet?

771