title: JavaMail SMTPAddressFailedException: 550 5.7.1 Unable to relay for ... (Grails)
body: <p>I am trying to send mail using javamail from my Grails application (via a service).
  Both the from and to addresses being used have the organization domain names.
  The same code, when run via command line as a groovy script (on the same server) works as expected.</p>
  
  <p>However, running on the grails server (under war mode) produces the following exception :</p>
  
  <pre><code> javax.mail.SendFailedException: Invalid Addresses;
    nested exception is:
      com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for ---@---.com
  
      at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1873)
      at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
      at javax.mail.Transport.send0(Transport.java:195)
      at javax.mail.Transport.send(Transport.java:124)
      at javax.mail.Transport$send.call(Unknown Source)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:124)
      at cs.MailService.mail(MailService.groovy:66)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at 
  </code></pre>
  
  <p>The code is written as follows :</p>
  
  <pre><code>import javax.mail.internet.*
  import javax.mail.*
  import javax.activation.*
  
  class Mailer {
  
  def static mail(String toAddress, String subject, String body) {
          println "mail called"
          println "${toAddress}, ${subject}, ${body}"
  
          try {
              def fromAddress = "---@---.com"
  
              def properties = new Properties()
              properties.setProperty("mail.transport.protocol","smtp")
              properties.setProperty("mail.smtp.host","---------")
  
              def sessionInstance = Session.getDefaultInstance(properties,null);
              def msg = new MimeMessage(sessionInstance)
  
              def stringTokenizer = new StringTokenizer(toAddress,";")
              def toAddressList = []
              while(stringTokenizer.hasMoreElements()){
                  toAddressList.add(new InternetAddress(stringTokenizer.nextElement().toString()));
              }
              def to = new InternetAddress[toAddressList.size()]
              to = (InternetAddress[]) toAddressList.toArray(to)
              msg.setRecipients(MimeMessage.RecipientType.TO,to)
              def from = new InternetAddress(fromAddress)
              msg.setFrom(from);
              msg.setSubject(subject)
  
              def mimeMultipart = new MimeMultipart()
              def mimeBodyPart = new MimeBodyPart()
              mimeBodyPart.setContent("&lt;p style='font-family:calibri;color:blue;font-size:20px;text-align:center;'&gt;"+body+"&lt;/p&gt;", "text/html")
              mimeMultipart.addBodyPart(mimeBodyPart)
  
              msg.setContent(mimeMultipart)
  
              def transporter = sessionInstance.getTransport("smtp")
              transporter.connect()
              transporter.send(msg)
          } catch(Exception e) {
              e.printStackTrace()
          }
      }
  
      public static void main(String[] args) {
          def to="---@---.com"
          def subject="Call Scheduler Notification"
          def body='&lt;font face="Segoe UI"&gt;&lt;p&gt;&lt;b&gt;&lt;font color="#6B0667"&gt;Hi --- ---,&lt;br&gt;&lt;br&gt;Your Call (id: 1281) Was Updated Successfully&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;&lt;table cellpadding="10" bgcolor="#B799B6"&gt;&lt;caption&gt;&lt;b&gt;&lt;font color="#6B0667"&gt;Call Details&lt;/font&gt;&lt;/b&gt;&lt;/caption&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Title&lt;/b&gt;&lt;/td&gt;&lt;td&gt;---------&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Start&lt;/b&gt;&lt;/td&gt;&lt;td&gt;---------&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;End&lt;/b&gt;&lt;/td&gt;&lt;td&gt;---------&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Number&lt;/b&gt;&lt;/td&gt;&lt;td&gt;---------&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Passcode&lt;/b&gt;&lt;/td&gt;&lt;td&gt;---------&lt;/td&gt;&lt;/tr&gt;&lt;td&gt;&lt;b&gt;Special Instructions&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;td&gt;&lt;b&gt;Callback Number&lt;/b&gt;&lt;/td&gt;&lt;td&gt;---------&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br&gt;&lt;p&gt;&lt;b&gt;&lt;font color="#6B0667"&gt;Regards&lt;br&gt;Call Scheduler&lt;/b&gt;&lt;/font&gt;&lt;/p&gt;&lt;/font&gt;'
          mail(to, subject, body);
      }
  
  }
  </code></pre>
  
  <p>How do i get past this error ?</p>
  
  <p><strong>Update :</strong> Still not entirely sure what was wrong here. But resetting all the tcp/ip connections (from the mail server) resolved the issue.</p>