1 Reply Latest reply: Nov 11, 2002 9:28 AM by 843830 RSS

    JavaMail connects to SMTP server & then tries to send to localhost

    843830
      I have a "simple" thread class that tries to send messages to an SMTP Server on our LAN. The Transport.connect method connects to the specified host and port successfully (which is not localhost). However, when Transport.send is called using the same Transport object a SendFailed Exception is thrown with the error "Could not connect to SMTP host: localhost, port: 25". I have no idea as to why it is trying to connect to localhost as nothing updates the Transport object between creation and the calling of the Send method (this is shown in the code below).

      The constructor contains the following code where member variables are declared as follows:

      private Session m_session = null;
      private Properties m_props = new Properties();
      private Transport m_transport = null;

      // Constructor
      {
      ...
      m_props.put("mail.smtp.host", m_host);
      m_props.put("mail.smtp.port", m_port);
      m_props.put("mail.debug", "true");

      // have tried Session.getDefaultInstance also
      m_session = Session.getInstance(m_props);
      m_session.setDebug(true);

      }

      The run method is as follows:
      // run
      {

      while (!ThreadStop)
      {
      Transport transport = m_session.getTransport("smtp");
      transport.connect(m_host, m_nPort, null, null);

      // vecMessages is a Vector of MimeMessage objects
      for (int i=0; i<vecMessages.size(); i++)
      {
      MimeMessage message = (MimeMessage) vecMessages.get(i);

      if (null != message)
      {
      transport.send(message, message.getAllRecipients());
      }

      }
      transport.close();
      }

      }

      Any help would be greatly appreciated
        • 1. Re: Fixed the problem
          843830
          The problem lies in the run method where I was creating the Transport object and then calling transport.send(...). The send method appears to use the defaults (host:localhost port:25). I changed the code to call transport.sendMessage (...) and this worked. sendMessage obviously uses the settings that were used to create the Transport object.

          Hope this helps someone else.