English 中文(简体)
java 邮局提供java.net 电话:链接拒绝
原标题:java mail give java.net.connectexception: connection refused

I have the same code on windows and it works well. When i move the code to centos, it gives the exception: javax.mail.MessagingException: Could not connect to SMTP host: stmp.gmail.com, port: 587; nested exception is: java.net.ConnectException: Connection refused

请允许我就这一例外提出一些建议吗?

非常感谢。

问题回答

你们犯了错! 它应当是“smtp.gmail.com”,而不是“stmp.gmail.com”。

拒绝联系意味着两件事之一。 不论东道方:你指定的港口是不正确的,或间歇性防火墙是打着球的。

如果你重新学习如何通过 Java寄邮件,尝试其他方式,那么,你需要把邮件送给你的电子邮件提供者员工和管理当局协调会服务器,而这一员工和管理当局协调会服务器则将邮件送至与本守则无关的适当地点。

<>0> 该守则在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";
  }

}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签