Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Servlet problem

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Servlet problem

    Hi
    my servlet is not sending email but my java main class can send an email.
    code for servlet
    Please help.
        protected void processRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
     
     
            HttpSession session = request.getSession();   
            ServletContext context = getServletContext();      
     
     
            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 =(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);        
     
        }    
        code for java main class
     
    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]="marhubane@gmail.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);
            }       
     
        }
    }


  2. #2
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Servlet problem

    Hello,

    What is the exception you got? Please post any error messages you encountered here.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 06:36 PM.

Similar Threads

  1. Problem in Servlet redirect
    By surendran610 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 17th, 2011, 12:15 PM
  2. Need help with a simple jsp and servlet problem
    By javacoder_APAC in forum Java Servlet
    Replies: 4
    Last Post: March 23rd, 2011, 08:32 AM
  3. Authentication problem in a servlet
    By Asido in forum Java Servlet
    Replies: 3
    Last Post: September 17th, 2010, 05:57 AM
  4. Problem with the servlet and jdbc
    By manjukdvg in forum Java Servlet
    Replies: 3
    Last Post: September 8th, 2010, 08:44 AM