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:
Before I spend another day tearing things apart and uninstalling / reinstalling, I wondered if anyone had any advice on fixing this?
MailConnectException: Couldn't connect to host, port: There can be multiple reasons for this but the most common reason for this error is the port, that you are using to send SMTP mails. Few ISPs/hosting providers block SMTP outgoing port 25. If that the case, try changing the port to 587 or 2525.
SMTP Blocks are an effective method for temporarily preventing a domain or indiviual user from sending email from the server. For example, if a particular account is sending an abnormal amount of email, you can add their address to the SMTP Blocks list and they will be unable to send email until you remove them.
StartTLS is a protocol command used to inform the email server that the email client wants to upgrade from an insecure connection to a secure one using TLS or SSL. StartTLS is used with SMTP and IMAP, while POP3 uses the slightly different command for encryption, STLS.
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:
Start your java program using this command line option:
-Djava.net.preferIPv4Stack=true
Doing either one of these will fix the problem.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With