Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I get java.net.SocketException: Permission denied: connect when sending an email in Jenkins

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="[email protected]">
  <defaultSuffix>@example.com</defaultSuffix>
  <hudsonUrl>http://localhost:8081/</hudsonUrl>
  <adminAddress>Jenkins Build Server &lt;[email protected]&gt;</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

  1. 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.

  2. 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.
like image 550
Brian Chapman Avatar asked Apr 19 '13 17:04

Brian Chapman


2 Answers

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.

like image 98
NilsH Avatar answered Oct 20 '22 14:10

NilsH


2.How do I get Jenkins to start up with the -Djava.net.preferIPv4Stack=true parameter and specified run time?

I'm not sure about the Java runtime, but this is what worked for me for the parameter: "-Djava.net.preferIPv4Stack=true".

1) notepad "C:\Program Files (x86)\Jenkins\jenkins.xml"

2) Find the line similar to:

<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -jar "%BASE%\jenkins.war" --httpPort=8080 -Djava.net.preferIPv4Stack=true</arguments>

3) Move the "-Djava.net.preferIPv4Stack=true" part to before the -jar argument. It should now look similar to:

<arguments>-Xrs -Xmx256m -Dhudson.lifecycle=hudson.lifecycle.WindowsServiceLifecycle -Djava.net.preferIPv4Stack=true -jar "%BASE%\jenkins.war" --httpPort=8080</arguments>

4) Save the file, exit notepad.

5) Restart the jenkins service

6) Send the test email from the "Manage Jenkins -> Configure System" web page. The email should now work.

like image 28
Jason Avatar answered Oct 20 '22 12:10

Jason