1

I'm having an issue with JavaMail programming in Netbeans. When I run the code below in the IDE, the email sends as intended. But when I perform a clean and build and attempt to perform the same action from the JAR Executable file, I receive the following information from the debugger:

DEBUG: JavaMail version 1.5.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
    java.net.SocketException: Permission denied: connect

I have tried building with both JDK1.8 and JDK1.7 with no success. Many sites(for example, ) have suggested fixing the IPv6 issue with some variation of the following in the netbeans config folder: -Djava.net.preferIPv4Stack=true. I have placed this in the VM options as well, and as you can see below, I have also tried to implement it in my code. Other attempted fixes still in place are setting the socketFactor.class property to javax.net.ssl.SSLSocketFactory, setting the MailSSLSocketFactory TrustAllHosts to true, and using the sendMessage() method in an instance of the Transport class to send the email. All failed.

I have attempted to telnet gmail through port 587 and the cmd line, and the connection has been established successfully.

I'm a big fan of debugging things myself, but it's been over a week and while many people seem to share the SocketException issue, none of the solutions have been effective.

I am open to solutions to this problem or, frankly, any alternative ways to send emails with Java. Reading emails is not important for this code. Most helpful are code snippets rather than just descriptions so I can drop them into my code for running. Thanks in advance!

Relevant code segment (running in Windows 7):

    static void sendEmail(String toAddress, String subject, String body) throws NoSuchProviderException, MessagingException, GeneralSecurityException {
        try {
            System.setProperty("java.net.preferIPv4Stack", "true");
            String host = "smtp.gmail.com";
            String username = "sampleuser@gmail.com";
            String password = "password";
            Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
            props.put("mail.debug", "true");
            props.put("mail.smtp.debug", "true");
            props.put("mail.smtp.password", password);
            props.put("mail.smtp.user", "sampleuser");
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);

            Session session = Session.getInstance(props);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(toAddress));
            message.setSubject(subject);
            message.setText(body);
            Transport t = session.getTransport("smtp");
            try {
                t.connect(host, username, password);
                t.sendMessage(message, message.getAllRecipients());
            }catch(Exception e){
                System.out.println(e);
            } finally {
                t.close();
            }
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
Community
  • 1
  • 1
Zach Sperry
  • 13
  • 1
  • 4

2 Answers2

1

Possibly the property needs to be set when the JVM starts? Try running with "java -Djava.net.preferIPv4Stack=true -jar ...."

Possibly you have some anti-virus or firewall that preventing "java" from connecting, but allowing "telnet" to connect? Try turning off any anti-virus or firewall temporarily to test.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • Setting the property ahead of time worked! I didn't know that syntax, thank you. So do I have to run it from the command line every time? I was hoping to package this in a .jar to distribute to my coworkers... – Zach Sperry Jul 29 '14 at 21:32
  • Update: just for kicks, I reran it from the command line as java -Djava.net.preferIPv4Stack=false -jar ...." and it worked anyhow (set property to false). It seems the issue only arises when I open my executable by double clicking. – Zach Sperry Jul 29 '14 at 21:35
  • Interesting. It might still be related to your anti-virus or firewall, where it knows to allow the "java" command but doesn't know to allow your jar file. – Bill Shannon Jul 30 '14 at 01:58
  • That would make sense. I don't think I have any say over the anti-virus or firewall, unfortunately. Big company, big IT department. I've tried packaging it in a .cmd file, which works around the issue. The only problem is I don't want my users to see the command window. Is there some way i can launch it from a .cmd file and hide the command window? – Zach Sperry Jul 30 '14 at 13:14
  • My final solution, using suggestion from Bill, was to create a .txt file with the following code: `set path=C:\Program Files (x86)\Java\jre7\bin` [NEW LINE] `start /min java -Djava.net.preferIPv4Stack=true -jar "Path\to\jar\file.jar"` and save the file as a .cmd file. That opens the file and runs a minimize but still useful command line as long as the .jar is running. – Zach Sperry Jul 30 '14 at 18:25
0

Gmail now comes with Sign-in & Secure feature turned off by default. Turn it on for your account and should work fine.

https://myaccount.google.com/security

Ajay Menon
  • 127
  • 6