0

尝试使用 Java 邮件发送电子邮件时出现以下错误。

    org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com. Failed messages: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
  nested exception is:
    java.net.UnknownHostException: smtp.gmail.com
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1704)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:306)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
    at com.covisint.validationAdmin.validationAdminBackend.smtpMail.EmailClient.send(EmailClient.java:48)
    at com.covisint.validationAdmin.validationAdminBackend.smtpMail.EmailClientTest.testSend(EmailClientTest.java:33)
    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:601)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.net.UnknownHostException: smtp.gmail.com
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
    at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)

我的 Java 邮件配置文件是

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"
        p:host="${mail.sender.host}" p:port="${mail.sender.port}" p:username="${mail.sender.username}"
        p:password="${mail.sender.password}" p:protocol="${mail.sender.protocol}"
        p:javaMailProperties-ref="mailProperties" />
    <bean id="emailClient"
        class="com.covisint.validationAdmin.validationAdminBackend.smtpMail.EmailClient"
        p:mailSender-ref="mailSender" p:emailSubject="${email.subject}" />
    <util:properties id="mailProperties">
        <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
        <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
        <prop key="mail.smtp.quitwait">${mail.smtp.quitwait}</prop>
        <prop key="mail.smtp.debug">${mail.smtp.debug}</prop>
        <prop key="mail.smtp.socketFactory.port">${mail.smtp.socketFactory.port}</prop>
        <prop key="mail.smtp.socketFactory.class">${mail.smtp.socketFactory.class}</prop>
        <prop key="mail.smtp.socketFactory.fallback">${mail.smtp.socketFactory.fallback}</prop>
        
    </util:properties>

我的属性文件是

email.subject = my email subject
mail.sender.host =smtp.gmail.com
mail.sender.port =465 //465 or 587 (both dint work)
mail.sender.username =****@gmail.com
mail.sender.password =******
mail.sender.protocol =smtp
mail.smtp.auth =true
mail.smtp.starttls.enable =true
mail.smtp.quitwait =false
mail.smtp.debug=true
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback=false

如何解决此错误?

4

1 回答 1

8

我刚刚解决了我的问题。

我在安全的办公网络上,当我更改它开始工作的网络时。

于 2013-04-23T13:38:45.397 回答