0

I am writing a simple java program to send mail to a particular Email ID using java mail using SSL

Following is my code

        public static String sendMail(String to)
        {
             String status_message = null;

             try 
             {
                   Properties props = new Properties();
                   props.put("mail.smtp.host", "smtp.gmail.com");
                   props.put("mail.smtp.socketFactory.port", "465");
                   props.put("mail.smtp.socketFactory.class",
                        "javax.net.ssl.SSLSocketFactory");
                   props.put("mail.smtp.auth", "true");
                   props.put("mail.smtp.port", "465");

                   Session session = Session.getDefaultInstance(props,
                   new javax.mail.Authenticator() 
                   {
                       @Override
                       protected PasswordAuthentication getPasswordAuthentication() 
                       {
                             return new PasswordAuthentication("[email protected]","password");//change accordingly
                       }
                  });

               //compose message
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress("[email protected]"));//change accordingly
                message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
                message.setSubject("Email Subject");
                message.setText("Text Content included in inside Email"); 

                //send message
                Transport.send(message);

                status_message = "success";
    } 
    catch (MessagingException ex) 
    {
                status_message = ex.getMessage();
    } 


}

When i ran this code inside a pariticular main Thread. It worked successfully.

But this same didn't worked when i included it inside a function called within a servlet

When i read the status_message value. It's showing smtp.

Can anyone help me find out what the issue is. Am i missing anything ? I want to send mail through a jsp servlet.

2
  • Posting useless statements like "didn't work" is basically a waste of time. Post the exception, stack trace, etc. Commented Mar 20, 2013 at 23:36
  • @EJP status_message contains the error message and it displays smtp... Commented Mar 21, 2013 at 5:02

1 Answer 1

1

You've made several of the common JavaMail mistakes. Correct them and see if that solves your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.