title: Could not connect to SMTP host: outlook.office365.com, port: 587; nested exception is: java.net.SocketException: Permission denied: connect
body: <p>I have written code that sends email using outlook.office365.com. When I am running the program getting the following error.</p>
  
  <blockquote>
    <p><strong>javax.mail.MessagingException:</strong> Could not connect to SMTP host: outlook.office365.com, port: 587;
     nested exception is:</p>
    
    <p><strong><code>java.net.SocketException:</code></strong> Permission denied: connect</p>
  </blockquote>
  
  <pre><code>private static final String SERVIDOR_SMTP = "outlook.office365.com";
  private static final int PORTA_SERVIDOR_SMTP = 587;
  private static final String CONTA_PADRAO = "xxxx@xxx.com"; //Cofig  Mail Id
  private static final String SENHA_CONTA_PADRAO = "XYZ"; // Password
  
  private final String from = "xxxx@xxx.com"; 
  private final String to = "xxxx@xxx.com";
  
  private final String subject = "Teste";
  private final String messageContent = "Teste de Mensagem";
  
  public void sendEmail() {
      final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() {
  
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO);
          }
  
      });
  
      try {
          final Message message = new MimeMessage(session);
          message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
          message.setFrom(new InternetAddress(from));
          message.setSubject(subject);
          message.setText(messageContent);
          message.setSentDate(new Date());
          Transport.send(message);
      } catch (final MessagingException ex) {
         System.out.println(" "+ex);
      }
  }
  
  public Properties getEmailProperties() {
      final Properties config = new Properties();
      config.put("mail.smtp.auth", "true");
      config.put("mail.smtp.starttls.enable", "true");
      config.put("mail.smtp.host", SERVIDOR_SMTP);
      config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
      return config;
  }
  
  public static void main(final String[] args) {
      new SendAttachmentInEmail().sendEmail();
  }
  </code></pre>