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", "me@mine.com"));
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:
- The program code works.
- The settings are correct.
- 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?
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.
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.
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:
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:
Start your java program using this command line option:
Doing either one of these will fix the problem.
Synantec EP 12.1 RU5 fixes your issue.
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:
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 .