title: javax.mail.MessagingException: Could not connect to SMTP host
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7158951/javax-mail-messagingexception-could-not-connect-to-smtp-host-smtp-gmail-com-p">javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25;</a>  </p>
  </blockquote>
  
  
  
  <p>I tried to send an email through a jsp page but it shows the following error:</p>
  
  <blockquote>
    <p><code>javax.mail.MessagingException</code>: Could not connect to SMTP host: www.gmail.com, port: 25, response: 421</p>
  </blockquote>
  
  <p>This is the code I'm using to send the email:</p>
  
  <pre><code>&lt;%
      try
      {
          String host = "www.gmail.com";
          String to = request.getParameter("to");
          String from = request.getParameter("from");
          String subject = request.getParameter("subject");
          String messageText = request.getParameter("body");
          boolean sessionDebug = false;
  
          Properties props = System.getProperties();
          props.put("mail.host", host);
          props.put("mail.transport.protocol", "smtp");
          Session mailSession = Session.getDefaultInstance(props, null);
  
          mailSession.setDebug(sessionDebug);
  
          Message msg = new MimeMessage(mailSession);
          msg.setFrom(new InternetAddress(from));
          InternetAddress[] address = {new InternetAddress(to)};
          msg.setRecipients(Message.RecipientType.TO, address);
          msg.setSubject(subject);
          msg.setSentDate(new Date());
          msg.setText(messageText);
  
          Transport.send(msg);
          out.println("Mail was sent to " + to);
          out.println(" from " + from);
          out.println(" using host " + host + ".");
      }
      catch (MessagingException mex) 
      {
          System.out.println("Error: unable to send message....");
          mex.printStackTrace();
      }
  %&gt;
  </code></pre>
  
  <p>Could someone tell me what is causing this error?</p>