2011-11-22 111 views
2

我在Windows上有相同的代码,它运作良好。当我将代码移动到centos时,它会给出例外: javax.mail.MessagingException:无法连接到SMTP主机:stmp.gmail.com,port:587; 嵌套的例外是: java.net.ConnectException:连接被拒绝java邮件给java.net.connectexception:连接拒绝

任何人都可以请给我有关此异常的一些建议吗?

非常感谢。

+0

看起来类似于这个http://stackoverflow.com/questions/3649014/send-email-using-java – Jasonw

+0

你是否成功执行了“telnet stmp.gmail.com 587”? –

回答

4

您犯了拼写错误!它应该是“smtp .gmail.com”而不是“stmp .gmail.com”。

+0

对不起,我只是输入错误。我用stmp.gmail.com来连接。 –

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"; 
    } 

} 
+1

如何解决ConnectionException? – EJP

1

'连接被拒绝' 意味着两件事情之一。您指定的主机:端口不正确,或者中间的防火墙未打球。

相关问题