title: Send email in Java via relay server
body: <p>I am running a Java web application and attempting to send email notifications from it. When we were hosting this application on one of our servers it worked perfectly fine. We pointed it to smtp.gmail.com on port 25 and the messages we able to be sent.</p>
  
  <p>But now we are hosting this on one of our client's servers for security purposes. They happen to block all smtp domains and only allow their own (SMTPRELAY.COMPANYNAME.COM). I changed the host in my code, but now the authentication is failing.</p>
  
  <p>Is it possible to still authenticate my gmail account while using this company's relay server to send the emails? What is the best solution?</p>
  
  <p>Below is the relevant portion of the code.</p>
  
  <pre><code>content = "message content";
  List&lt;String&gt; recipients = LIST_OF_RECIPIENT_EMAIL_ADDRESSES;
  
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", "SMTPRELAY.COMPANYNAME.COM");
      props.put("mail.smtp.port", "25");
      props.put("mail.smtp.ssl.trust", "SMTPRELAY.COMPANYNAME.COM");
      props.put("mail.debug", "true");
  
      Session session = Session.getInstance(props,
        new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
              //This is where the email account name and password are set
              return new PasswordAuthentication("SUPPORTEMAILACCOUNT@gmail.com", "PASSWORD");
          }
        });
      try{
           MimeMessage message = new MimeMessage(session);
  
           message.setReplyTo(new Address[]{new InternetAddress("no-reply@gmail.com")});
           for(String recipient: recipients){
               message.addRecipient(Message.RecipientType.BCC,new InternetAddress(recipient));
           }
           message.setSubject(subject);
           message.setContent(content,"text/html");
           Transport.send(message);
           return true;
        }catch (MessagingException mex) {
           mex.printStackTrace();
           return false;
        }
  </code></pre>
  
  <p>Here is the error I keep getting:</p>
  
  <pre><code>javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful
  </code></pre>