title: Spring JavaMailSender: recipient set as "default"
body: <p>It is my first question, so i would like to keep it simple.
  I have a problem with JavaMailSender from Spring Framework version 4.1.8.RELEASE. It appears that for a particular SMTP it does not add recipient to the message. I want to mention that we use the same solution in our other projects and it always worked. Since now, there were no problems with recipients.</p>
  
  <p>Here is code example how we setup recipient(setupRecipients method):</p>
  
  <pre><code>String emailTo = entity.getEmailTo();
  Preconditions.checkNotNull( !Strings.isNullOrEmpty( emailTo ) );
  emailTo = emailTo.replace( ",", " " );
  emailTo = emailTo.replace( ";", " " );
  InternetAddress[] emails = InternetAddress.parse( emailTo );
  
  for ( InternetAddress address : emails )
      address.validate();
  
  for ( InternetAddress address : emails )
      messageHelper.setTo( address.getAddress() );
  
  if ( !Strings.isNullOrEmpty( bcc ) )
      messageHelper.addBcc( bcc );
  </code></pre>
  
  <p>Here is example how we send an email(where mailSender is of type JavaMailSender):</p>
  
  <pre><code>    MimeMessage message = this.mailSender.createMimeMessage();
  
      try {
          MimeMessageHelper e = new MimeMessageHelper(message, true, "utf-8");
          e.setFrom(this.from);
          message.setSubject(this.createSubject(entity));
          this.setupRecipients(entity, e);
          this.setupBody(entity, e);
          this.setupAttachments(entity, e);
          this.setupInlines(entity, e);
          this.mailSender.send(message);
      } catch (IOException | MessagingException var4) {
          Throwables.propagate(var4);
      }
  </code></pre>
  
  <p>When i try to send an email with that special SMTP, i get this response:</p>
  
  <pre><code>org.springframework.mail.MailSendException: Failed messages: javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is:
   com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 &lt;default&gt;: Recipient address rejected: need fully-qualified address
   ; message exception details (1) are:
      Failed message 1:
      javax.mail.SendFailedException: Invalid Addresses;
      nested exception is:
      com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 &lt;default&gt;: Recipient address rejected: need fully-qualified address
  
      at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1196)
      at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:584)
      at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:433)
      at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345)
      at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
      at packagename.EmailExporter.export(EmailExporter.java:57)
      at packagename.ExportJobImpl$ExportCallback.onResult_aroundBody0(ExportJobImpl.java:86)
      at packagename.ExportJobImpl$ExportCallback$AjcClosure1.run(ExportJobImpl.java:1)
      at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:66)
      at org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:72)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
      at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:70)
      at packagename.ExportJobImpl$ExportCallback.onResult(ExportJobImpl.java:81)
      at packagename.ExportableEntityEnumeratorImpl.lambda$iterate$9(ExportableEntityEnumeratorImpl.java:30)
      at packagename.ExportableEntityEnumeratorImpl$$Lambda$31/1744828123.processRow(Unknown Source)
      at org.springframework.jdbc.core.JdbcTemplate$RowCallbackHandlerResultSetExtractor.extractData(JdbcTemplate.java:1607)
      at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:708)
      at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:695)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:727)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:752)
      at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:767)
      at packagename.ExportableEntityEnumeratorImpl.iterate_aroundBody0(ExportableEntityEnumeratorImpl.java:28)
      at packagename.ExportableEntityEnumeratorImpl$AjcClosure1.run(ExportableEntityEnumeratorImpl.java:1)
      at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96cproceed(AbstractTransactionAspect.aj:66)
      at org.springframework.transaction.aspectj.AbstractTransactionAspect$AbstractTransactionAspect$1.proceedWithInvocation(AbstractTransactionAspect.aj:72)
      at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
      at org.springframework.transaction.aspectj.AbstractTransactionAspect.ajc$around$org_springframework_transaction_aspectj_AbstractTransactionAspect$1$2a73e96c(AbstractTransactionAspect.aj:70)
      at packagename.ExportableEntityEnumeratorImpl.iterate(ExportableEntityEnumeratorImpl.java:27)
      at packagename.ExportJobImpl.run(ExportJobImpl.java:50)
      at packagename.MultiExportJobImpl.run(MultiExportJobImpl.java:42)
      at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 504 5.5.2 &lt;default&gt;: Recipient address rejected: need fully-qualified address
  
      at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1094)
      ... 38 more
  </code></pre>
  
  <p>Here is the list of what we tried:</p>
  
  <ol>
  <li>First of all, we have consulted SMTP provider; however, their answer was that recipient is set as "default" and nothing else. We also requested logs but since then they did not provide them(it is not ours SMTP but still they did not provide them even to the owner).</li>
  <li>Secondly, when i tried to configure JavaMail with Spring and send email locally with the use of my development machine instead of the server our application usually runs on, it worked as intended. Also SMTP has no problems with sending an email when configured with for ex. Thunderbird.</li>
  <li>When we switched their SMTP with our SMTP on the server, it works.</li>
  </ol>
  
  <p>Conclusion is that the problem must be either in the server configuration, between server and SMTP or SMTP. However i have no idea what might actually cause it.</p>