Error sending email

We have recently upgraded our gateway machine and have had all our automatic emails stop working. When I try to execute the following code, where smtpAddress is the address of the SMTP server as a string and fromAddr is the address of the client sending the message as a string:

system.net.sendEmail(smtpAddress, fromAddr, "Subject - Test", "Hello", 0, [value], [], [], timeout)

I’m getting the following error on the gateway log:

3:45:34 PM Gateway ERROR(500): Error sending email.

javax.mail.MessagingException: Could not connect to SMTP host: smtpAddress, port: 25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.inductiveautomation.ignition.gateway.script.GatewayNetUtilities.sendEmail(GatewayNetUtilities.java:151)
at com.inductiveautomation.ignition.gateway.servlets.Gateway._sendEmail(Gateway.java:968)
at com.inductiveautomation.ignition.gateway.servlets.Gateway.doPost(Gateway.java:393)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at com.inductiveautomation.ignition.gateway.bootstrap.MapServlet.service(MapServlet.java:85)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:696)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:526)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:568)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:221)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1110)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:453)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1044)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerList.handle(HandlerList.java:52)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:459)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:280)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:229)
at org.eclipse.jetty.io.AbstractConnection$1.run(AbstractConnection.java:505)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Network is unreachable: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
… 31 more

Where smtpAddress is the smtp address.

I am able to both ping the SMTP server and connect and send emails via telnet from the gateway machine.

From searching for the following Java error online I see others have had issues with Java using IPv6 over IPv4, so I’ve disabled the IPv6 under network adaptor properties but haven’t had any luck so far.

java.net.SocketException: Network is unreachable: connect

We are running an old rev of Ignition (soon to be upgraded) but I’ve briefly looked through the minor revision notes and can’t see any fixes specifically for this fault.

Does anyone have any clues as to why Java thinks it cannot connect to the SMTP server when the machine clearly can connect?

The machine is running Win 7 OS, Ignition 7.7.1.

Is there a firewall rule or some kind of windows policy preventing a process that is running as a service from making an outbound connection? :scratch:

I’ve done a few more tests which seem to indicate that it’s a machine based issue and not a firewall/policy issue. I’ve detailed my tests below:

  1. On a different gateway instance running on my laptop (same OS, Java version and Ignition version) I can successfully send an email through the same script.

  2. I’ve tried modifying the Java option parameters to prefer IPv4 by using the following command line command:

setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true

Then restarted the gateway with no success.

  1. I’ve tried wire-sharking the connection but not too sure what I’m looking for. I know that when I send the emails via Telnet I get some SMTP traffic, but it doesn’t get any when running the script on the gateway (there is no traffic going to the IPv4 IP address of the SMTP server at all). Unfortunately I can’t check the traffic on my laptop gateway instance to compare because it is going through a VPN.

So I haven’t been able to prove that the packets have even left the PC and the “try/except” clause on the “sendEmail” function fails with next to no delay. Does anyone have any other ideas I can try/diagnostics I can log to get a clearer picture of what is going on?

Just to update this post: after much head scratching and some help from the Inductive support guys the problem was narrowed down to an anti-virus setting. To quote Paul:

" the network unreachable error indicates that Ignition sent the connection request to Java, and then Java tried to send it to the OS - but the call was rejected at that point"

Now to go back and turn all the IPv6 settings back on…

1 Like