2013-07-27 59 views
1

我想從我的Grails應用程序(通過服務)使用javamail發送郵件。 使用的從地址和地址都具有組織域名。 當通過命令行作爲groovy腳本(在同一臺服務器上)運行時,相同的代碼按預期工作。JavaMail SMTPAddressFailedException:550 5.7.1無法中繼...(Grails)

但是,(在對戰模式)Grails的服務器上運行產生以下異常:

javax.mail.SendFailedException: Invalid Addresses; 
    nested exception is: 
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for [email protected] 

    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 

代碼編寫如下:

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 = "[email protected]" 

      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("<p style='font-family:calibri;color:blue;font-size:20px;text-align:center;'>"+body+"</p>", "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="[email protected]" 
     def subject="Call Scheduler Notification" 
     def body='<font face="Segoe UI"><p><b><font color="#6B0667">Hi --- ---,<br><br>Your Call (id: 1281) Was Updated Successfully</b></font></p><table cellpadding="10" bgcolor="#B799B6"><caption><b><font color="#6B0667">Call Details</font></b></caption><tr><td><b>Title</b></td><td>---------</td></tr><tr><td><b>Start</b></td><td>---------</td></tr><tr><td><b>End</b></td><td>---------</td></tr><tr><td><b>Number</b></td><td>---------</td></tr><tr><td><b>Passcode</b></td><td>---------</td></tr><td><b>Special Instructions</b></td><td></td></tr><td><b>Callback Number</b></td><td>---------</td></tr></table><br><p><b><font color="#6B0667">Regards<br>Call Scheduler</b></font></p></font>' 
     mail(to, subject, body); 
    } 

} 

我如何獲得過去的這個錯誤?

更新:仍然不完全確定這裏出了什麼問題。但重置所有tcp/ip連接(從郵件服務器)解決了這個問題。

+1

命令行和grails執行(不同的地址,屬性等)之間必須有所不同。嘗試在會話屬性中設置「mail.debug = true」,並比較輸出 –

回答

相關問題