12

I have a Java program which uses javax.mail to send an SMTP message. This program works fine on a Linux box, I want to emphasize that beforehand. When I try this same code on my Windows 7 x64 box, I get this error:

send failed, exception: javax.mail.MessagingException: Could not connect to SMTP host:     smtp.west.cox.net, port: 25;
nested exception is:  java.net.SocketException: Network is unreachable: connect

Here is the code:

Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, props.getProperty("mail.to", "[email protected]"));
msg.setSubject(mySubject);
msg.setSentDate(new Date());
msg.setContent(sBuf.toString(), "text/html");
Transport.send(msg);

This program pretty much uses defaults for everything. It works fine on another box on the same network. It uses the same settings that I use for my regular mail client, which works fine. There is something on THIS Windows box that is blocking SMTP, but only for Java.

I have Symantec (Norton) 360 installed. Turning it off makes no difference, but rebooting into Safe Mode (which disables almost everything) allows the program to work and send mail just fine.

So, to recap:

  1. The program code works.
  2. The settings are correct.
  3. SMTP works for Windows Mail and is only blocked for Java on this Windows machine.

Before I spend another day tearing things apart and uninstalling / reinstalling, I wondered if anyone had any advice on fixing this?

8
  • what happens if you try telnet smtp.west.cox.net 25 from the command prompt? Dec 28, 2012 at 3:16
  • What version of Windows? Is Windows Firewall active?
    – Perception
    Dec 28, 2012 at 3:17
  • Since your code works in safe mode ... it's not your code. I suspect Java wasn't allowed via the windows firewall. Dec 28, 2012 at 3:22
  • 4
    This question might better be posted in ServerFault. It's a network/OS problem, not a programming problem.
    – ccleve
    Dec 28, 2012 at 4:12
  • Testing port 25 from PUTTY (telnet) connects to the SMTP server as espected. Dec 28, 2012 at 4:47

6 Answers 6

51

The problem is due to the IPv4/IPv6 preference of Java. Java tries to use IPv6 by default (?) and my ISP does not support IPV6. However, it's enabled on my Windows 7 boxes by default.

If you are experiencing similar behavior, you can verify this by going to "Control Panel/Network and Internet/Network Connections", right-clicking your internet connection, and selecting "Status" from the context menu. The Status dialog will display two lines similar to this:

IPv4 Connectivity:  Internet
IPv6 Connectivity:  No Internet access

This is the root of the problem - Java prefers IPv6, which it cannot use to connect to the internet.

To fix this, do either one (or both) of these things:

  1. Disable IPv6 on your Windows 7 box.
  2. Start your java program using this command line option:

    -Djava.net.preferIPv4Stack=true
    

Doing either one of these will fix the problem.

3
  • Thanks for this explanation, fixes my issue. Someone else on stackoverflow claimed they could use their mail/Exchange server's IPv6 address as the host instead of the server's IPv4 address. but not sure that solves issues you mentioned above. May 1, 2013 at 14:48
  • 1
    This is a really kickass answer, I was banging my head on the wall! I would give you 100 up-votes if I could. Jun 7, 2013 at 1:47
  • Just passing in JVM argument "-Djava.net.preferIPv4Stack=true" solved my issue. Sep 20, 2013 at 15:44
12

I had the same problem during upgrade form java 1.6 to java 1.7. The problem occured because java 1.7 using IPv6 by default. To fix this, you need to add Java Option like on example below.

Just run this command on Windows cmd:

setx _JAVA_OPTIONS -Djava.net.preferIPv4Stack=true
1
  • what are the stars for?
    – azerafati
    Mar 2, 2015 at 12:18
3

Synantec EP 12.1 RU5 fixes your issue.

Symantec Endpoint Protection blocks email sent by Java mail client

Fix ID: 3399185

Symptom: The Internet Email Auto-Protect feature of Symantec Endpoint Protection blocks SMTP email sent by a Java mail client.

Solution: Updated the Internet Email Auto-Protect driver to allow processing of IPv4-mapped IPv6 addresses.

2

I was also facing the same issue , basically it was a Java 7 issue, Just passing in JVM argument "-Djava.net.preferIPv4Stack=true", i got rid from issue .

1
  • please add such short answers as comments
    – MichaC
    Oct 2, 2013 at 17:03
1

Although this issue is solved: it might still be useful to point out that some Windows Firewalls block/allow access to ports based on the executable name (rather than purely the port number itself) - so you can end up with one program (say Outlook) being allowed and another (say Java) being blocked.

-1

I had similar issue but strangely everything was working fine with jdk 1.6 once I install jdk 1.7 I got the exception. I wonder what could be the reason for this!

After adding IPV4 argument it is working fine..Thank you so much.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.