I get java.net.SocketException: Permission denied: connect when sending an email in Jenkins
java, jenkins, windows-7
Solution
Arguments after the `-jar` argument will be passed to the application, not the java vm. So try moving `-Djava.net.preferIPv4Stack=true` before the `-jar` argument.
Problem
My Configuration: - Windows 7 machine - Java 7 - Jenkins 1.511 running as Service on Local Account My jenkins.xml file ``` <service> <id>jenkins</id> <name>Jenkins</name> <env name="JENKINS_HOME" value="%BASE%"/> <executable>C:\Program Files\IBM\SDP8.5\jdk\bin\java.exe</executable> <arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=8080 -Djava.net.preferIPv4Stack=true</arguments> </service> ``` My hudson.tasks.Mailer.xml file ``` <hudson.tasks.Mailer_-DescriptorImpl plugin="mailer@1.4"> <defaultSuffix>@example.com</defaultSuffix> <hudsonUrl>http://localhost:8081/</hudsonUrl> <adminAddress>Jenkins Build Server <do-not-reply@example.com></adminAddress> <smtpHost>smtp.example.com</smtpHost> <useSsl>false</useSsl> <charset>UTF-8</charset> </hudson.tasks.Mailer_-DescriptorImpl> ``` Note: "example.com" substituted for my real domain/email/smtp server. When I instruct Jenkins to send a test email, I get this error: ``` Failed to send out e-mail javax.mail.MessagingException: Could not connect to SMTP host: smtp.example.com, port: 25; nested exception is: java.net.SocketException: Permission denied: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) at javax.mail.Service.connect(Service.java:295) ``` My understanding with this error, is that there is a bug in Windows which is exposed by Java 7 with regards to the Firewall and how Java 7 uses a ipv4 mapped ipv6 address. This is reported to be solved by adding -Djava.net.preferIPv4Stack=true to the java run time. I have had success with adding -Djava.net.preferIPv4Stack=true on this machine using the ANT email task. However, in Jenkins, I have not had any luck. Note that it appears the jenkins.xml file is completely ignored. The -Djava.net.preferIPv4Stack=true parameter is not set. The PATH and JAVA_HOME both point to the IBM run time, but Jenkins still starts with the Oracle v7 run time. Jenkins must be spawning off a new process to start and picking up the system java 7. Using the above configuration, if I go to Jenkins scripting console and interrogate it using "System.getProperty("java.net.preferIPv4Stack"), I get nothing (not set). If I set that parameter, I still get the error. If I add -Djava.net.preferIPv4Stack=true to the command line (not using the service), it is still not set when interrogated through the scripting console. My questions Why am I getting this error and how to I resolve it? Remember that I can write an ANT script that successfully sends email using the same smtp server and java run times. How do I get Jenkins to start up with the -Djava.net.preferIPv4Stack=true parameter and specified run time? Some Notes: - I cannot uninstall Java 7 due to lame corporate issues. - I cannot install Java 6 due to the same issues. - I am allowed to install the IBM JDK/JRE.