title: Postfix returning Invalid Addresses; 454 <to@domain.com>: Relay access denied
body: <p>I'm using a pre-configured Postfix server and interacting with it using JavaMail.  I am trying to send email notifications to any external domain that may request it.  </p>
  
  <p>Here is the java method to send a test email:  </p>
  
  <pre><code>public void TestEmail() {
  
      String to = "to@domain.com";
  
      String from = "noreply@hostdomain.com";
      final String username = "user";
      final String password = "password";
  
      String host = "xxx.xxx.xxx.xxx";
  
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "587");
  
      Session session = Session.getInstance(props,
              new Authenticator() {
                  protected PasswordAuthentication getPasswordAuthentication() {
                      return new PasswordAuthentication(username, password);
                  }
      });
      try {
          Message message = new MimeMessage(session);
  
          message.setFrom(new InternetAddress(from));
  
          message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  
          message.setSubject("Test Email");
  
          message.setContent("This is a test email", "text/html");
  
          Transport.send(message);
  
          System.out.println("The test message was sent!");
      } catch (MessagingException e) {
          throw new RuntimeException(e);
      }
  
  }
  </code></pre>
  
  <p>When I run the method that contains the JavaMail code, I get<br>
      <code>generalError: javax.mail.SendFailedException: Invalid Addresses;
    nested exception is:
          com.sun.mail.smtp.SMTPAddressFailedException: 454 4.7.1 &lt;to@domain.com&gt;: Relay access denied</code>  </p>
  
  <p>I was able to test outgoing mail from the Postfix server using<br>
  <code>echo "This is the body" | mail -s "This is the subject" to@domain.com</code><br>
  and did receive the email in the testing inbox.  </p>
  
  <p>I'm not 100% sure what all the settings are on the Postfix server, but I am suspecting that is where the issue is, but I'm not familiar enough with it to start digging around and changing things all willy-nilly.</p>
  
  <p><strong>EDIT:</strong>
  I removed the <code>Authenticator()</code> set up from the Session creation and replaced <code>Transport.send()</code> with recommended code block  </p>
  
  <pre><code>        Transport transport = session.getTransport("smtp");
          transport.connect(host, 587, username, password);
          transport.sendMessage(message, message.getAllRecipients());
          transport.close();  
  </code></pre>
  
  <p>The entire new set of code is:  </p>
  
  <pre><code>    String host = "xxx.xxx.xxx.xxx";
  
      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", "587");
  
      Session session = Session.getInstance(props, null);
  
      try {
          Message message = new MimeMessage(session);
  
          message.setFrom(new InternetAddress(from));
  
          message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  
          message.setSubject("Test Email");
  
          message.setContent("This is a test email", "text/html");
  
          Transport transport = session.getTransport("smtp");
          transport.connect(host, 587, username, password);
          transport.sendMessage(message, message.getAllRecipients());
          transport.close();
  
          System.out.println("The test message was sent!");
      } catch (MessagingException e) {
          System.out.println("Error: " + e);
          e.printStackTrace();
          throw new RuntimeException(e);
      }
  
  }
  </code></pre>
  
  <p>This has changed the error message I am receiving to: <code>Unrecognized SSL message, plaintext connection?</code></p>
  
  <hr>
  
  <h3>Solved</h3>
  
  <p>Solution: The Java code was not the issue.  The Postfix server was not configured to accept emails originating from a non-local IP/host. The IPs were added to the main.cf in the mynetworks variable.</p>