title: JavaMail - multiple senders
body: <p>I have a simple javamail class to send emails through SMTP. It works as long as I only send emails from one address. If I try using another address, it raises this exception for some reason:</p>
  <pre class="lang-none prettyprint-override"><code>com.sun.mail.smtp.SMTPSendFailedException: 550 5.1.0 Use your own address, please.
  </code></pre>
  <p>Here's my class:</p>
  <pre class="lang-java prettyprint-override"><code>public class EmailSender {
      private static final String HOST = &quot;xxxx.xxxxxx.xx&quot;;
      private static final String PORT = &quot;xx&quot;;
  
      public static boolean sendMail(String from, String to, String pass, String subject, String text) {
          Properties properties = new Properties();
  
          properties.setProperty(&quot;mail.smtp.host&quot;, HOST);
          properties.setProperty(&quot;mail.smtp.port&quot;, PORT);
          properties.setProperty(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
          properties.setProperty(&quot;mail.user&quot;, from);
          properties.setProperty(&quot;mail.password&quot;, pass);
  
          Session session = Session.getDefaultInstance(properties, new CustomAuthenticator(from, pass));
  
          try {
              MimeMessage message = new MimeMessage(session);
              message.setFrom(new InternetAddress(from));
              message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
              message.setSubject(subject);
              message.setText(text);
  
              Transport.send(message);
  
              return true;
          } catch (MessagingException e) {
              e.printStackTrace();
              return false;
          }
      }
  }
  </code></pre>
  <p>In case you're wondering, the CustomAuthenticator class looks like this:</p>
  <pre class="lang-java prettyprint-override"><code>public class CustomAuthenticator extends Authenticator {
      private String user;
      private String pw;
  
      public CustomAuthenticator(String username, String password) {
          super();
          this.user = username;
          this.pw = password;
      }
  
      public PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(user, pw);
      }
  }
  </code></pre>