Helo everyone
I'm a South African programmer java(J2EE,J2ME and J2SE) but I'm new in J2EE.
My servlet cant send an email.but my java main class can.
Please help.
Code for servlet
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
 
/**
 *
 * @author VMasanabo
 */
@WebServlet(name = "SendEmailServlet", urlPatterns = {"/SendEmailServlet"})
public class SendEmailServlet extends HttpServlet {
 
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
 
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SendEmailServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet SendEmailServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             * 
             */
 
        HttpSession session = request.getSession();   
        ServletContext context = getServletContext(); 
 
       /* String recipients[] = new String[1];
        recipients[0]="makgubane@yahoo.com";
 
        String username ="Mike";
        String rights = "None";
        String password = "jjjjj6876";
 
        String subject="Account details";
 
 
        String from ="VMasanabo@csir.co.za";
        String message="Your user name : "+username+"\n Your password : "+password+"\n Your rights : "+rights;*/
 
 
 
        String username =(String)session.getAttribute("username");
        String rights = (String)session.getAttribute("rights");
        String password = (String)session.getAttribute("password");
 
        String userMail = (String)session.getAttribute("mail");
 
 
 
        String recipients[] = new String[1];
        recipients[0]= userMail;
 
        String from ="VMasanabo@csir.co.za";//(String)session.getAttribute("AdministratorMail");
 
        String message="Your user name : "+username+"\n Your password : "+password+"\n Your rights : "+rights;
 
        String subject="Account details";
 
 
            try 
            {
                postMail(recipients,subject ,message, from);
                context.getRequestDispatcher("/CreateAccountPage.jsp").forward(request, response);
 
            } 
            catch (MessagingException ex) 
            {
                Logger.getLogger(SendEmailServlet.class.getName()).log(Level.SEVERE, null, ex);
            }
 
 
        } finally {            
            out.close();
        }
    }
 
 
    public void postMail(String recipients[],String subject,String message,String from)throws MessagingException
    {        
        boolean debug = false;
        Properties prop = new Properties();
        prop.put("mail.smtp.host", "wabe.csir.co.za");
 
        Session sessions = Session.getDefaultInstance(prop);
        sessions.setDebug(debug);
 
        Message msg = new MimeMessage(sessions);
        InternetAddress addressFrom = new InternetAddress(from);
 
        msg.setFrom(addressFrom);
 
        InternetAddress addressTo[] = new InternetAddress[recipients.length];
 
        for(int x=0;x<addressTo.length;x++)
        {
            addressTo[x] = new InternetAddress(recipients[x]);
        }
 
        msg.setRecipients(Message.RecipientType.TO, addressTo);
 
        msg.setSubject(subject);
        msg.setContent(message,"text/plain");
 
        Transport.send(msg);
 
 
 
    }    
 
    /*public static void main(String arg[])
    {
        try {
            String recipients[] = new String[1];
            recipients[0]="makgubane@yahoo.com";
 
            String username ="Mike";
            String rights = "None";
            String password = "jjjjj6876";
 
            String subject="Account details";
            String message="Your user name : "+username+"\n Your password : "+password+"\n Your rights : "+rights;
 
            String from ="VMasanabo@csir.co.za"; 
 
            SendEmailServlet emailServlet = new SendEmailServlet();
            emailServlet.postMail(recipients,subject ,message, from);
        } catch (MessagingException ex) {
            Logger.getLogger(SendEmailServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
 
    }*/
 
 
 
 
    // <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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    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
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
 
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

Code for java main class
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;
 
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
 
 
/**
 *
 * @author VMasanabo
 */
public class JavaApplication1 {
 
 
    public void postMail(String recipients[],String subject,String message,String from)throws MessagingException
    {
        boolean debug=false;
        Properties props = new Properties();
        props.put("mail.smtp.host", "wabe.csir.co.za");
 
        Session session = Session.getDefaultInstance(props, null);
        session.setDebug(debug);
 
        Message msg = new MimeMessage(session);
 
        InternetAddress addressFrom = new InternetAddress(from);
 
        msg.setFrom(addressFrom);
 
        InternetAddress [] addresTo = new InternetAddress[recipients.length];
 
        for(int i =0;i<recipients.length;i++)
        {
            addresTo[i]= new InternetAddress(recipients[i]);
        }
 
        msg.setRecipients(Message.RecipientType.TO, addresTo);
 
        msg.setSubject(subject);
        msg.setContent(message,"text/plain");
        Transport.send(msg);
 
    } 
 
    public static void main(String[] args) 
    {
        String recipients[] = new String[1];
        recipients[0]="makgubane@yahoo.com";
 
        String username ="Vusi";
        String rights = "None";
        String password = "testf6876";
 
        String subject="Account details";
        String message="Your user name : "+username+"\n Your password : "+password+"\n Your rights : "+rights;
 
        String from ="VMasanabo@csir.co.za";
 
        JavaApplication1 application1 = new JavaApplication1();
        try {
            application1.postMail(recipients, subject, message, from);
        } catch (MessagingException ex) {
            Logger.getLogger(JavaApplication1.class.getName()).log(Level.SEVERE, null, ex);
        }
 
 
 
 
    }
}