2014-02-20 109 views
1

我在glassfish 2.1中運行了相同的代碼,但在glassfish 4中部署它會拋出以下錯誤: 嚴重:javax.mail.MessagingException:501 5.5.4無效地址。 錯誤被拋出代碼:t.connect(mailhost,user,password);我用javamail + glassfish發送電子郵件時遇到問題4

public void sendmailto(String amsg, String mailaddress, String asubject) { 
    String to = mailaddress;  // to address  
    String from = "[email protected]"; // from address 
    String subject = asubject; // the subject line 
    String message = amsg;     // the body of the message 
    String mailhost = "atlas"; // SMTP server 
    String user = "soporte";     // user ID 
    String password = "soportepass";    // password 
    boolean auth = true; 
    boolean ssl = false; 
    Properties props = System.getProperties(); 

    if (mailhost != null) { 
     props.put("mail.smtp.host", mailhost); 
    } 
    if (auth) { 
     props.put("mail.smtp.auth", "true"); 
    } 
    javax.mail.Session session = javax.mail.Session.getInstance(props, null); 

    javax.mail.Message msg = new MimeMessage(session); 

    try { 
     msg.setFrom(new InternetAddress(from)); 
     msg.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to)); 
     msg.setSubject(subject); 
     msg.setSentDate(new Date()); 
     msg.setText(message); 
     SMTPTransport t = (SMTPTransport) session.getTransport(ssl ? "smtps" : "smtp"); 
     try { 
      if (auth) { 
       t.connect(mailhost, user, password); 
      } else { 
       t.connect(); 
      } 
      t.sendMessage(msg, msg.getAllRecipients()); 
     } finally { 
      t.close(); 

     } 

    } catch (Exception e) { 
     e.printStackTrace(); 

    } 
} 

堆棧跟蹤

Grave: javax.mail.MessagingException: 501 5.5.4 Invalid Address 
    at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:2076) 
    at com.sun.mail.smtp.SMTPTransport.helo(SMTPTransport.java:1469) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:660) 
    at javax.mail.Service.connect(Service.java:345) 
    at javax.mail.Service.connect(Service.java:226) 
    at util.MailSender.sendmailto(MailSender.java:80) 
    at tools.MailManager.enviarMail(MailManager.java:119) 
    at tools.MailManager.sendmailto(MailManager.java:42) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46) 
    at sun.reflect.GeneratedMethodAccessor179.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140) 
    at sun.reflect.GeneratedMethodAccessor134.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) 
    at com.sun.proxy.$Proxy337.sendmailto(Unknown Source) 
    at tools.ErpTools.guardarRegisto(ErpTools.java:151) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46) 
    at sun.reflect.GeneratedMethodAccessor179.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163) 
    at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140) 
    at sun.reflect.GeneratedMethodAccessor134.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) 
    at com.sun.proxy.$Proxy343.guardarRegisto(Unknown Source) 
    at usuarios.manejadorSessionBean.login(manejadorSessionBean.java:87) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081) 
    at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153) 
    at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695) 
    at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582) 
    at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883) 
    at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822) 
    at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369) 
    at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667) 
    at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212) 
    at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88) 
    at com.sun.proxy.$Proxy342.login(Unknown Source) 
    at ManagedBean.LoginMB.login(LoginMB.java:88) 
    at ManagedBean.LoginMB$Proxy$_$$_WeldClientProxy.login(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAc 
Grave: cessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at com.sun.el.parser.AstValue.invoke(AstValue.java:275) 
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) 
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) 
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) 
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) 
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) 
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) 
    at javax.faces.component.UICommand.broadcast(UICommand.java:315) 
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) 
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) 
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) 
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) 
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) 
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) 
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) 
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) 
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) 
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) 
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) 
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) 
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) 
    at java.lang.Thread.run(Thread.java:744) 
+0

將堆棧跟蹤從例外添加到您的帖子可能會有所幫助。 – MarkOfHall

+0

我試着用appconsole,問題仍然存在,但在WXP 32位運行好了,我變得瘋了...... – meyquel

回答

0

http://www.oracle.com/technetwork/java/javamail/faq/index.html#debug

在這裏我找到了解決途徑...... 步驟1)設置會話調試真正

javax.mail.Session session = javax.mail.Session.getInstance(props, null); 
session.setDebug(true); 

第1步後,我看到過光的問題,那就是記錄顯示:

DEBUG:setDebug:JavaMail的版本1.5.0

DEBUG:getProvider()返回javax.mail.Provider [TRANSPORT,SMTP,com.sun.mail。 smtp.SMTPTransport,甲骨文]

DEBUG SMTP:useEhlo真,useAuth真正

DEBUG SMTP:試圖連接到主機 「地圖集」,端口25,isSSL假

220 atlas.emni.moa。 minbas.cu微軟ESMTP MAIL服務,版本:6.0.3790.3959準備在週五,2014年2月21日14時10分50秒-0500

DEBUG SMTP:連接到主機 「地圖集」,端口:25

EHLO info0 .emni.moa.minbas.cu。 501 5.5.4無效地址

HELO info0.emni.moa.minbas.cu。 501 5.5.4無效地址

步驟2)設置 「mail.smtp.localhost」 屬性...(我的計算機名 'info0')

props.put("mail.smtp.localhost", "info0"); 

ORACLE JAVAMAIL'DOC SAY: 的SMTP提供程序在SMTP HELO命令中使用InetAddress.getLocalHost()。getHostName()的結果。如果該呼叫未能返回任何數據,則不會在HELO命令中發送名稱。檢查您的JDK和名稱服務器配置,確保該調用返回正確的數據。您也可以將「mail.smtp.localhost」屬性設置爲您要用於HELO命令的名稱。

3

見的JavaMail常見問題debugging tips

它看起來像在抱怨你的發件人地址或收件人地址。

請注意,如果您選擇使用SSL,則代碼會被破壞。如果您選擇「smtps」傳輸,則「mail.smtp。*」屬性將被忽略。不要選擇不同的傳輸方式,請設置「mail.smtp.ssl.enable」屬性。

+0

謝謝,你給我的路徑找到問題的解決方案...... – meyquel

+0

謝謝你,把'mail.smtp.ssl.enable'屬性設置爲'true'來實現SSL是我的訣竅。 –