• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending mail through servlet using java mail api

 
NIRANJAN RAILKAR
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I Have Written code for sending mail through servlet
from yahoo account
I am getting the following error
"No provider for pop3".
I Have activated my yahoo account also
still i am getting this error
please help
//code
import java.util.*;
import java.io.*;
import java.text.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendMailServlet extends HttpServlet
{

public static String MAIL_HOST = "smtp.mail.yahoo.com";
public static String MAIL_HOST2 = "pop.mail.yahoo.com";
/**
* <p>Performs the HTTP POST operation
*
* @param req The request from the client
* @param resp The response from the servlet
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException
{
resp.setHeader("Pragma", "No-cache");
resp.setDateHeader("Expires", 0);
resp.setHeader("Cache-Control", "no-cache");
// Set the content type of the response
resp.setContentType("text/html");
// Create a PrintWriter to write the response
java.io.PrintWriter out = new java.io.PrintWriter (resp.getOutputStream());
// Get the data from the form
String from = req.getParameter("MAIL_FROM");
String subject = req.getParameter("MAIL_SUBJECT");
String body = req.getParameter("MAIL_BODY");
String to = req.getParameter("MAIL_TO");
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", MAIL_HOST);
Session sess = Session.getDefaultInstance(props, null);
try {
// Connect to host
Store store = sess.getStore("pop3");
store.connect(MAIL_HOST2, -1, "my-username", "my-password");
// Create a message
Message msg = new MimeMessage(sess);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setContent(body, "text/html; charset=ISO-8859- 1");//HTML type
//msg.setContent(body); //Text type
// Send the message
Transport trans = sess.getTransport("smtp");
trans.connect(MAIL_HOST,"my-username","my-password");
trans.send(msg);
// Let the user know that the mail was sent
out.println("<html>");
out.println("<head>");
out.println("<title>Send Email</title>");
out.println("<body><center>");
out.println("<h2>Your email has been sent!</h2>");
out.println("</center></body></html>");
trans.close();
}
catch (Exception ex) {
// Got an error sending the mail; notify the client
out.println("<html>");
out.println("<head>");
out.println("<title>Send Email Error</title>");
out.println("<body><center>");
out.println("<h2>There was an error sending your email</h2>");
out.println("<br>Message=" + ex.getMessage());
out.println("</center>");
out.println("</body></html>");
}
// Wrap up
out.flush();
}
/**
* <p>Initialize the servlet. This is called once when the
* servlet is loaded. It is guaranteed to complete before any
* requests are made to the servlet
*
* @param cfg Servlet configuration information
*/
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
/**
* <p>Destroy the servlet. This is called once when the servlet
* is unloaded.
*/
public void destroy()
{
super.destroy();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic