简体   繁体   中英

Showing Error while sending email using java servlet?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM