title: Java: How to send an email from a corporate domain
body: <p>I've been trying to send e-mails from a custom domain trying to log into the outlook server, since we use it as a platform to send e-mails from that direction, but it gave me an <code>AuthenticationFailedException</code>; more specifically:</p>
  <pre><code>javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [PR0P264CA0272.FRAP264.PROD.OUTLOOK.COM]
  
      at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:965)
      at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:876)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:780)
      at javax.mail.Service.connect(Service.java:388)
      at javax.mail.Service.connect(Service.java:246)
      at javax.mail.Service.connect(Service.java:195)
      at javax.mail.Transport.send0(Transport.java:254)
      at javax.mail.Transport.send(Transport.java:124)
      at baseDatos.BaseDeDatos.mailMasivo(BaseDeDatos.java:595)
      at baseDatos.BaseDeDatos.main(BaseDeDatos.java:167)
  </code></pre>
  <p>I've been looking into some older posts that may be the solution to my problem, but I don't know how to implement it into my code.</p>
  <p>My code looks something like this:</p>
  <pre><code>    String to = &quot;to@mail.com&quot;;
      String from = &quot;from@cdomain&quot;;
      String password = &quot;password123&quot;;
      String subject = &quot;Hello&quot;;
      BodyPart body1 = new MimeBodyPart();
      String body= &quot;This is an e-mail&quot;;
      body1.setText(body);
      Multipart mainBody = new MimeMultipart();
      mainBody.addBodyPart(body1);
      
      String host = &quot;outlook.office365.com&quot;;
      Properties properties = System.getProperties();
      properties.put(&quot;mail.smtp.host&quot;, host);
      properties.put(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
      properties.setProperty(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
      Session session = Session.getInstance(properties, new OutLookAuthenticator(from, password));
      Transport transport = session.getTransport(&quot;smtp&quot;);
      Message message = new MimeMessage(session);
      message.setSubject(asunto); 
      message.setContent(mainBody);
      message.setFrom(new InternetAddress(from));
      InternetAddress[] address = {new InternetAddress(to)};
  
      message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
           
      transport.send(message);
  </code></pre>
  <h2>Update</h2>
  <p>I've changed the code to implement the spring framework library so I could send mail synchronously. Here's how it looks like now:</p>
  <pre><code>            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
              mailSender.setHost(&quot;outlook.office365.com&quot;);
              mailSender.setPort(587);
              String from = keyboard.nextLine();
              mailSender.setUsername(from);
              String password = readPassword();
              mailSender.setPassword(password);
  
              MimeMessage message = mailSender.createMimeMessage();
  
              final Properties properties = new Properties();
              properties.put(&quot;mail.smtp.auth&quot;, Boolean.TRUE);
              properties.put(&quot;mail.smtp.starttls.enable&quot;, Boolean.TRUE    );
              mailSender.setJavaMailProperties(properties);
              
              try {
              final MimeMessageHelper helper = new MimeMessageHelper(message, true);
              helper.setFrom(from);
              helper.setTo(to);
              helper.setSubject(subject);
              helper.setText(text, true);
              } catch (MessagingException e) {
                  e.printStackTrace();
              }
              mailSender.send(message);
  </code></pre>
  <p>And now, it is printing this stack trace:</p>
  <pre><code>Exception in thread &quot;main&quot; org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [MR2P264CA0048.FRAP264.PROD.OUTLOOK.COM]
  
  at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:440)
  at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
  at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
  at baseDatos.BaseDeDatos.mailIndividual(BaseDeDatos.java:440)
  at baseDatos.BaseDeDatos.main(BaseDeDatos.java:160)
  </code></pre>