title: How to send email to Microsoft direct send (domain-com.mail.protection.outlook.com) using groovy, java
body: <p>I am trying to send email to user@domain.com via domain-com.mail.protection.outlook.com (Office 365 direct send),</p>
  <p>below is my code,</p>
  <pre><code>void sendEmail(InternetAddress sender, List&lt;InternetAddress&gt; recipients, String subject, String body,
                                @Nullable BodyPart optAttachment,
                                XgemailDirectionEnum direction = XgemailDirectionEnum.INBOUND,
                                Optional&lt;String&gt; optionalEnvelopeSenderAddress = Optional.empty())
  {
      final Properties properties = new Properties(_defaultProperties)
      for (InternetAddress recipient : recipients)
      {
          if (optionalEnvelopeSenderAddress.isPresent())
          {
              properties.put(&quot;mail.smtp.from&quot;, optionalEnvelopeSenderAddress.get())
          }
  
          Session session
        
          properties.put(&quot;mail.smtp.host&quot;, domain-com.mail.protection.outlook.com)
          properties.put(&quot;mail.smtp.port&quot;, 25)
          properties.put(&quot;mail.debug&quot;, &quot;true&quot;)
          session = Session.getInstance(properties)
          
  
          Message message = new MimeMessage(session)
          message.setFrom(sender)
  
          message.setRecipients(
              Message.RecipientType.TO,
              recipient as InternetAddress[]
          )
          message.setSubject(subject)
          message.setText(body)
          
          Transport.send(message)
      }
  </code></pre>
  <p>I tried adding properties properties.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;) also, but no luck.</p>
  <blockquote>
  <p>I am getting following error</p>
  </blockquote>
  <pre><code>com.sun.mail.util.MailConnectException: Couldn't connect to host, port: domain-com.mail.protection.outlook.com, 25; timeout -1
  </code></pre>
  <p>Following is mail.debug log</p>
  <pre><code>DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
  
  
  DEBUG SMTP: useEhlo true, useAuth false DEBUG SMTP: trying to connect to host &quot;domain-com.mail.protection.outlook.com&quot;, port 25, isSSL false
  </code></pre>