0

There are showing error while sending a mail using servlet(in eclispe).I also include mail.jar and activation.jar in my classpath.

My servlet code looks like

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Controller extends HttpServlet 
{
    private static final long serialVersionUID = 1L;
    public Controller() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doProcess(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        doProcess(request,response);
    }
    protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        final String user = request.getParameter("email");
        final String pwd = request.getParameter("pwd");
        String sub = request.getParameter("subject");
        String body = request.getParameter("msg");
        String to = "shiladitya1093@gmail.com";

        //Get the session object
        Properties props = new Properties();
        props.put("mail.smtp.host","localhost");
        props.put("mail.smtp.auth","true");
        Session session = Session.getDefaultInstance(props,  
                 new javax.mail.Authenticator() {  
                  protected PasswordAuthentication getPasswordAuthentication() {  
                   return new PasswordAuthentication(user,pwd);  
                   }  
                });

        //Compose message
        try{
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(user));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.setSubject(sub);
            message.setText(body);

        //Send message
            Transport.send(message);
        }catch(MessagingException e){
            throw new RuntimeException(e);
        }

        request.setAttribute("sendMsg","Message successfully send.");
        RequestDispatcher rd = request.getRequestDispatcher("contactus.jsp");
        rd.forward(request, response);
    }
}

And the error is look like :

HTTP Status 500 - javax.mail.AuthenticationFailedException: 535 No SMTP server defined. Use real server address instead of 127.0.0.1 in your account.

Aditya
  • 1,214
  • 7
  • 19
  • 29
  • 1
    Use real server address instead of 127.0.0.1 in your account.(read your error carefully? – Deepanshu J bedi Jul 26 '14 at 05:04
  • Mail server needs to be properly configured.You have the smtp host as localhost , it should be the smtp server ip.In case you dont have smtp server , use the default smtp configuration provided by google ,you can test the mail api. – Amar Jul 26 '14 at 05:11
  • how can i set smtp server in the eclispe? – Aditya Jul 26 '14 at 05:21

1 Answers1

0

Can you check the following. 1) Is your SMTP server configured on your local machine. 2) Try using the machine name or ip address of the machine where you intend to send the mail to instead of localhost.

Also do provide some details on what is your intended target for the mail (ie where is this mail intended to be delivered). Based on which you might get better response.

ganaraj
  • 420
  • 3
  • 13
  • I want to send message from gmail to gmail and i also try "smtp.gmail.com" instead of localhost but it shows the same problem – Aditya Jul 26 '14 at 05:25
  • try defining the port explicitly 587 and 465 to the gmail smtp server by setting the property "mail.smtp.port". Also you could check whether you are able to reach the server in question by using telnet from the cmd prompt or using some telnet clients like putty (if on windows). This way you can be sure that the server you are trying to access is accessible from your machine before you start – ganaraj Jul 26 '14 at 05:35
  • A look at the following post http://stackoverflow.com/questions/15372803/sending-gmail-error might help where the issue was identified to be due to the antivirus blocking the smtp port, in any case a simpel telnet test would reveal whether you are able access or not. – ganaraj Jul 26 '14 at 05:58
  • See this FAQ entry for [sending to Gmail](http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail), and this FAQ entry of [common mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). – Bill Shannon Jul 26 '14 at 17:45