SMTP Mail Sending Issue : com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

Nirav Prajapati picture Nirav Prajapati · Nov 7, 2017 · Viewed 29.4k times · Source

I am trying to send mail from Java. If i will send mail to same SMTP it working fine. If i will send mail to outside SMTP means like Gmail, Yahoo etc. it'as shows error like,

[com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay][1]

ERROR :

SimpleEmail Start
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

Mail Send Successfully
    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:2064)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1286)
    at javax.mail.Transport.send0(Transport.java:255)
    at javax.mail.Transport.send(Transport.java:124)
    at com.nirav.java.project.demo.JavaMailSend.sendEmail(JavaMailSend.java:26)
    at com.nirav.java.project.demo.NewSimpleMail.main(NewSimpleMail.java:34)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1917)
    ... 5 more

Code For Mail Sending :

try {
            System.out.println("SimpleEmail Start");

            String smtpHostServer = "XX.XX.XX.XXX";
            final String toEmail = "[email protected]";
            final String fromEmail = "[email protected]";
            final String password = "XXXXXXXXXXXX";

            Properties props = System.getProperties();

            props.put("mail.smtp.host", smtpHostServer);
            props.put("mail.smtp.port", "25"); //If other then

            Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            });
            //Session session = Session.getInstance(props, null);

          MimeMessage message = new MimeMessage(session);

          message.addHeader("Content-type", "text/HTML; charset=UTF-8");
          message.addHeader("format", "flowed");
          message.addHeader("Content-Transfer-Encoding", "8bit");
          message.setFrom(new InternetAddress("[email protected]", "NoReply-JD"));
          message.setReplyTo(InternetAddress.parse("[email protected]", false));
          message.setSubject(subject, "UTF-8");
          message.setText(body, "UTF-8");
          message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
          Transport.send(message);

            System.out.println("Mail Send Successfully");
        } catch (Exception e) {
            e.printStackTrace();
        }

Please help me. Thnaks.

Answer

rkosegi picture rkosegi · Nov 7, 2017

Originally I was about to post this as comment, but it's too long.

The error is quite obvious, you are not allowed to use given SMTP server as relay. (What are SMTP relays?)

There are couple of reasons why this can happen:

  • You are not authenticated (need to login before sending)

  • Recipient is not in list of domains allowed to relay to

  • IP address from which you are connecting is not in white list (aka mynetworks in postfix context)

without providing more info (which SMTP server you are using, where are you sending mail, are you authenticated), I guess nobody will help you.

5.7.1 status code from IANA registry

The sender is not authorized to send to the destination. This can be the result of per-host or per-recipient filtering. This memo does not discuss the merits of any such filtering, but provides a mechanism to report such. This is useful only as a permanent error.