The code below is taken from a
servlet in my web application on netbeans 6.1
The netbeans
IDE pretty much handles all thats needed for sending email from
java files
=====================CODE==============================
package bc.servlets;
import java.io.*;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;
import javax.servlet.*;
import javax.servlet.http.*;
/**
*
* @author nonso
*/
public class Send extends HttpServlet {
@Resource(name = "mail/myYahoomailSession")
private Session mailmyYahoomailSession;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("Send.processRequest, //////////////////// ENTERED ////////////////////");
try {
this.sendMail("chinomsoikwuagwu@yahoo.com", "Mail From TestWebApp", "Congrats on your success");
System.out.println("Send.processRequest, //////////////////// SUCCESS ////////////////////");
}catch(javax.naming.NamingException e) {
e.printStackTrace();
}catch(javax.mail.MessagingException e1) {
e1.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Send</title>");
out.println("</head>");
out.println("<body>");
out.println("<h2>Servlet Send at " + request.getContextPath () + "</h2>");
out.println("<b>Thanks for signing up. A message has been sent to your mail box</b>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public
String getServletInfo() {
return "Short description";
}
private void sendMail(String email, String subject, String body) throws NamingException, MessagingException {
MimeMessage message = new MimeMessage(mailmyYahoomailSession);
System.out.println("Send.sendMail, message:" + message);
message.setSubject(subject);
System.out.println("Send.sendMail, SUBJECT SET");
message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(email, false));
System.out.println("Send.sendMail, RECIPIENTS SET");
message.setText(body);
System.out.println("Send.sendMail, BODY SET");
javax.mail.Transport.send(message);
System.out.println("Send.sendMail, MESSAGE SENT");
}
// </editor-fold>
}
================================CODE===========================
The output message below is given.
The mail is never sent, even after 30minutes of waiting.
Would love to know why?
Application server startup complete.
deployed with moduleid = mail
DEBUG: JavaMail version 1.4.1
DEBUG: not loading file: C:\Program Files\Java\jdk1.6.0_06\jre\lib\javamail.providers
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.6.0_06\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: not loading file: C:\Program Files\Java\jdk1.6.0_06\jre\lib\javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.6.0_06\jre\lib\javamail.address.map (The system cannot find the file specified)
Send.processRequest, //////////////////// ENTERED ////////////////////
Send.sendMail, message:javax.mail.internet.MimeMessage@1f92ac0
Send.sendMail, SUBJECT SET
Send.sendMail, RECIPIENTS SET
Send.sendMail, BODY SET