javax.mail.SendFailedException,无法获取本地主机名

时间:2015-03-26 13:36:06

标签: java tomcat javamail

    >>>>>Sending data QUIT<<<<<<
    javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Can't get local hostname.  Please correctly configure JDK/DNS or set mail.smtp.localhost)
        at javax.mail.Transport.send(Transport.java:163)
        at javax.mail.Transport.send(Transport.java:48)
        at pl.intibs.logic.tools.EmailSender.sendEmail(EmailSender.java:56)
        at pl.intibs.paad.components.SampleMeasurementComponent.buttonClick(SampleMeasurementComponent.java:293)
        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.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
        at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:977)
        at com.vaadin.ui.Button.fireClick(Button.java:393)
        at com.vaadin.ui.Button$1.click(Button.java:61)
        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.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
        at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
        at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:291)
        at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:184)
        at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:92)
        at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
        at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
        at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:350)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: javax.mail.MessagingException: Can't get local hostname.  Please correctly configure JDK/DNS or set mail.smtp.localhost
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.getLocalHost(SMTPTransport.java:1942)
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendEhlo(SMTPTransport.java:1870)
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendHandshake(SMTPTransport.java:1828)
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:393)
        at javax.mail.Service.connect(Service.java:242)
        at javax.mail.Service.connect(Service.java:85)
        at javax.mail.Service.connect(Service.java:70)
        at javax.mail.Transport.send(Transport.java:94)
        ... 46 more
    >>>>>Sending data QUIT<<<<<<
    javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Can't get local hostname.  Please correctly configure JDK/DNS or set mail.smtp.localhost)
        at javax.mail.Transport.send(Transport.java:163)
        at javax.mail.Transport.send(Transport.java:48)
        at pl.intibs.logic.tools.EmailSender.sendEmail(EmailSender.java:56)
        at pl.intibs.paad.components.SampleMeasurementComponent.buttonClick(SampleMeasurementComponent.java:293)
        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.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
        at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
        at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:977)
        at com.vaadin.ui.Button.fireClick(Button.java:393)
        at com.vaadin.ui.Button$1.click(Button.java:61)
        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.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
        at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
        at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:291)
        at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:184)
        at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:92)
        at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
        at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
        at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:350)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
    Caused by: javax.mail.MessagingException: Can't get local hostname.  Please correctly configure JDK/DNS or set mail.smtp.localhost
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.getLocalHost(SMTPTransport.java:1942)
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendEhlo(SMTPTransport.java:1870)
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.sendHandshake(SMTPTransport.java:1828)
        at org.apache.geronimo.javamail.transport.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:393)
        at javax.mail.Service.connect(Service.java:242)
        at javax.mail.Service.connect(Service.java:85)
        at javax.mail.Service.connect(Service.java:70)
        at javax.mail.Transport.send(Transport.java:94)
        ... 46 more

    public class EmailSender {
static String from = "account@int.pan.wroc.pl"; 
        private static String footer = "\n\n________________________________________________________\nJest to automatyczna wiadomość, proszę nie odpowiadać."+
                                       "\nThis is an automated message, please do not reply."; //stopka dodawana do każdego maila

        public static void sendEmail(String to, String subject, String messageText) throws AddressException, MessagingException {

            Properties props = new Properties();
            props.put("mail.smtp.host", "mserv3.int.pan.wroc.pl");
            props.put("mail.smtp.starttls.enable", "true"); 
            props.put("mail.smtp.password", "pass");
            props.put("mail.smtp.port", "587");
    //      props.put("mail.smtp.auth", "true");
            Session mailSession = Session.getDefaultInstance(props);
            Message simpleMessage = new MimeMessage(mailSession);

            InternetAddress fromAddress = null;
            InternetAddress toAddress = null;

                fromAddress = new InternetAddress(from);
                toAddress = new InternetAddress(to);

                simpleMessage.setFrom(fromAddress);
                simpleMessage.setRecipient(RecipientType.TO, toAddress);
                simpleMessage.setSubject(subject);
                simpleMessage.setText(messageText);

                Transport.send(simpleMessage);   // Exception here      

        }
    }

我有问题。我的代码在我的localhost机器上运行良好。但是当我在服务器上部署.war时它无法发送电子邮件。我不知道为什么。 服务器位于同一网络中并成功ping通。 mserv3.int.pan.wroc.pl所以这不是DNS的问题。

我的本​​地开发机器:Ubuntu 14.04 + Tomcat 7 服务器:CentOS 6 + Tomcat 7

2 个答案:

答案 0 :(得分:1)

stacktrace告诉你问题的原因:

  

引起:javax.mail.MessagingException:无法获取本地主机名。请正确配置JDK / DNS或设置mail.smtp.localhost

看起来好像JavaMail无法确定它运行的主机的名称。尝试异常中的建议。

以下声明的结果是什么?

bash> cat /etc/hostname

如果无法通过DNS设置主机名或获取名称,请尝试使用属性mail.smtp.localhost

Here是失败方法的源代码。也许被调用的#getHostName()-method的JavaDoc很有帮助:

  

获取此IP地址的完全限定域名。尽力而为方法,这意味着我们可能无法返回FQDN,具体取决于底层系统配置。

     

如果有安全管理器,则此方法首先使用hostname和-1作为其参数调用其checkConnect方法,以查看是否允许调用代码知道此IP地址的主机名,即连接到主机。如果不允许该操作,它将返回IP地址的文本表示。

答案 1 :(得分:0)

我的服务器主机名

bash> hostname
vm10.int.pan.wroc.pl

我补充说:

props.put(&#34; mail.smtp.localhost&#34;,&#34; vm10.int.pan.wroc.pl&#34;);

现在它可以工作,但我不明白为什么。我必须分享我的.war。人们不应该在使用CentOS 6的每台机器上编辑它。