javaspringsmtpjakarta-mailemailrelay

Exception in sending emails from java application : Relaying denied


We are using Spring Mail to send emails from java application org.springframework.mail.javamail.JavaMailSenderImpl

Spring Email Configuration is

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl" autowire-candidate="default">
        <property name="host" value="${test.email.host}" />
        <property name="port" value="${test.email.port}" />
        <property name="username" value="${test.email.username}" />
        <property name="password" value="${test.email.password}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>

            </props>
        </property>
    </bean>

Java code snapshot is

    @Autowired(required = true)
    @Qualifier("errorMessageMailSender")
    JavaMailSenderImpl mailSender;

    ..............
    ..............

          try {
                MimeMessage mailMessage = buildEmailMimeMessage(properties,mimeMultipart);
                logger.info(String.format("Built MimeMessage object is <%s>",mailMessage));
                if (mailMessage != null) {
                    mailSender.send(mailMessage);
                    logger.info("Mail sent Successfully");
                }else{
                    logger.info("Mail send failed as Mail message object construction failed.");
                }
                result=true;
            } catch (Exception e) {
                logger.error("An exception occurred while sending mail :: " + e.getMessage());
            }

Property files

test.email.host=mail.mydomain.net
test.email.port=2525
test.email.username=demo@mydomain.net
test.email.password=mypassword

But we are below exception, and email is not being sent

An exception occurred while sending mail :: Failed messages: javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 <demo@mydomain.net>... Relaying denied

Solution

  • The smtp server that you are trying to send the mail to is rejecting to relay. If it is your own local server that is not a problem, you may change configurations to relay mails from your mydomain.net . But if it is an external server (gmail for example), you have to use a real registered domain.

    To test your code against a mail server, I recommend you to setup a dockerized Apache James Server, create some test users on it and send and receive emails.