java mail给java.net.connectexception:连接被拒绝

时间:2011-11-22 01:50:20

标签: java email exception

我在Windows上使用相同的代码,效果很好。当我将代码移动到centos时,它给出了异常: javax.mail.MessagingException:无法连接到SMTP主机:stmp.gmail.com,port:587;   嵌套异常是:     java.net.ConnectException:拒绝连接

有人可以给我一些关于这个例外的建议吗?

非常感谢。

3 个答案:

答案 0 :(得分:4)

你拼错了!它应该是“ smtp .gmail.com”而不是“ stmp .gmail.com”。

答案 1 :(得分:1)

'拒绝连接'是指两件事之一。您指定的主机:端口不正确或者插入的防火墙没有播放球。

答案 2 :(得分:0)

如果您刚刚学习如何通过Java发送邮件,请尝试以下操作,否则,您需要将其设置为您的电子邮件提供商SMTP服务器,然后此SMTP服务器将邮件发送到相应的位置,而不是这段代码的情况。

注意:代码是用Java Servlet编写的。


public class MailClient extends HttpServlet
{
  private class SMTPAuthenticator extends Authenticator
  {
        private PasswordAuthentication authentication;

        public SMTPAuthenticator(String login, String password)
        {
             authentication = new PasswordAuthentication(login, password);
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication()
        {
             return authentication;
        }
  }

  protected void processRequest(HttpServletRequest request, 
  HttpServletResponse response) throws ServletException, IOException
  {
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
       try
       {
            String from = "xyz.com";
            String to = "abc.com";
            String subject = "Your Subject.";
            String message = "Message Text.";
            String login = "xyz.com";
            String password = "password";

            Properties props = new Properties();
            props.setProperty("mail.host", "smtp.gmail.com");
            props.setProperty("mail.smtp.port", "587");
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.starttls.enable", "true");

            Authenticator auth = new SMTPAuthenticator(login, password);

            Session session = Session.getInstance(props, auth);

            MimeMessage msg = new MimeMessage(session);

           try
           {
                msg.setText(message);
                msg.setSubject(subject);
                msg.setFrom(new InternetAddress(from));
                msg.addRecipient(Message.RecipientType.TO, 
                new InternetAddress(to));
                Transport.send(msg);
           }
           catch (MessagingException ex)
           {
                Logger.getLogger(MailClient.class.getName()).
                log(Level.SEVERE, null, ex);
           }
       } 
       finally
       {
            out.close();
       }
  } 

  @Override
  protected void doGet(HttpServletRequest request, 
  HttpServletResponse response) throws ServletException, IOException
  {
       processRequest(request, response);
  } 

  @Override
  protected void doPost(HttpServletRequest request, 
  HttpServletResponse response)  throws ServletException, IOException
  {
       processRequest(request, response);
  }

  @Override
  public String getServletInfo()
  {
       return "Short description";
  }

}