所以这就是我遇到的问题:
当我尝试连接到smtp服务器时,当我在netbeans中尝试时,它会给我一个错误,说我无法连接:
我知道我的计算机可以连接到邮件服务器,因为telnet在我尝试时工作得很好,这让我觉得问题出在代码上。
但是,当我编写的程序在另一台计算机上使用时,它的工作正常。据wireshark称,该程序从未尝试连接到邮件服务器。
我已经尝试让自己成为防火墙的管理员,这对程序没有任何影响。
这里是我使用的代码:
private static final Properties SYS_PROPS = System.getProperties();
public void sendEmail(String user, String Message, String subject, String recipient) throws ResourceException {
try {
//Set the host property
SYS_PROPS.setProperty("mail.smtp.host", "host");
System.setProperty("java.net.preferIPv4Stack" , "true");
//Create a session for all the emails
Session session = Session.getDefaultInstance(SYS_PROPS);
//Get the name of the contact peep
String name = user;
//Create a new message
MimeMessage message = new MimeMessage(session);
MimeMultipart multiPart = new MimeMultipart();
//Set to, from, and subject
message.setFrom(new InternetAddress("http://localhost:8084/appName"));
message.addRecipient(RecipientType.TO, new InternetAddress(recipient));
message.setSubject(subject);
//Next, add the message part
BodyPart arm = new MimeBodyPart();
arm.setContent(Message, "text/html");
multiPart.addBodyPart(arm);
//Set the multipart to the message
message.setContent(multiPart);
//Send the message
Transport.send(message);
} catch (MessagingException ex) {
log(ex);
}
}
我目前的理论是,我不得不配置netbeans或tomcat,以允许我发送邮件。
我真的很感激您能给予的任何帮助!
我在使用该程序时遇到此错误:
SEVERE: null
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 10.1.0.5, 25; timeout -1;
nested exception is:
java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at com.echoman.saasadmin.dao.EmailDao.sendEmail(EmailDao.java:347)
at com.echoman.saasadmin.services.TenantService.SendEmail(TenantService.java:229)
at com.echoman.saasadmin.form.EmailForm$2.buttonClick(EmailForm.java:148)
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:979)
at com.vaadin.ui.Button.fireClick(Button.java:393)
at com.vaadin.ui.Button$1.click(Button.java:57)
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:287)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:180)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:93)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
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:505)
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:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
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: java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
... 52 more
这是我的IP配置
Ethernet adapter Local Area Connection
Media State:Media Disconnected
Connection-Specic DNS Suffix:
Ethernet adapter Ethernet:
Connection-specific DNS Suffix :echoman.local
Ipv4 Address :10.1.2.15
Subnet mask :255.255.252.0
Default GateWay :10.1.0.5