• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Exception while connecting to mailbox in java

 
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Attached is the code that I am using to connect to mailbox in java



But I am getting the following exception:


javax.mail.MessagingException: Connect failed;
 nested exception is:
       javax.net.ssl.SSLException: Connection reset
       at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:210)
       at javax.mail.Service.connect(Service.java:295)
       at javax.mail.Service.connect(Service.java:176)
       at javax.mail.Service.connect(Service.java:125)
       at TestProgram.main(TestProgram.java:50)
Caused by: javax.net.ssl.SSLException: Connection reset
       at sun.security.ssl.Alert.createSSLException(Alert.java:127)
       at sun.security.ssl.TransportContext.fatal(TransportContext.java:327)
       at sun.security.ssl.TransportContext.fatal(TransportContext.java:270)
       at sun.security.ssl.TransportContext.fatal(TransportContext.java:265)
       at sun.security.ssl.SSLTransport.decode(SSLTransport.java:141)
       at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1198)
       at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1107)
       at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:400)
       at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:372)
       at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
       at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
       at com.sun.mail.pop3.Protocol.<init>(Protocol.java:107)
       at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:261)
       at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:206)
       ... 4 more
       Suppressed: java.net.SocketException: Broken pipe (Write failed)
               at java.net.SocketOutputStream.socketWrite0(Native Method)
               at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
               at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
               at sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
               at sun.security.ssl.TransportContext.fatal(TransportContext.java:358)
               ... 16 more
Caused by: java.net.SocketException: Connection reset
       at java.net.SocketInputStream.read(SocketInputStream.java:210)
       at java.net.SocketInputStream.read(SocketInputStream.java:141)
       at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:466)
       at sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:460)
       at sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:159)
       at sun.security.ssl.SSLTransport.decode(SSLTransport.java:110)
       ... 13 more

 
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a highly unusual approach to making a JavaMail connection. Based on what information did you write that code?

Update: The host isn't publicly accessible - are you running this code somewhere where it is?
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:That's a highly unusual approach to making a JavaMail connection. Based on what information did you write that code?

Update: The host isn't publicly accessible - are you running this code somewhere where it is?



Yes, you are right, this server may not be publicly accessible. It is inside our company premises.
And why is that unusual approach to make a JavaMail connection. Isnt it how I should be doing it?
 
Tim Moores
Bartender
Posts: 7645
178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See https://javaee.github.io/javamail/#Samples for an archive of sample code, and https://javaee.github.io/javamail/FAQ for lots of good information on how to use JavaMail.
 
Marshal
Posts: 80873
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YG: Please don't add things to an old post; that makes it difficult to understand the replies. Please post the new material as a new post. I am reverting the changes.
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem got resolved.

The issue was with the Java Mail API version that was being used.
When I updated it to latest javax.mail.jar the problem was gone.
 
Marshal
Posts: 6000
418
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yogesh Gandhi wrote:The issue was with the Java Mail API version that was being used.
When I updated it to latest javax.mail.jar the problem was gone.


For the benefit of future readers of this conversation, what version did you upgrade from and to?
 
Rancher
Posts: 326
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things:

1) DON'T use POP3 and set the SocketFactory to SSLSocketFactory - use POP3S instead!
2) Adding this security provider shouldn't be required - this looks like some very old JVM - you should consider a java upgrade to somewhat more modern.

As an addition: 3) You don't have to tinker with breaking the PKI cert path checking - just add the root ca certificate to your truststore.

Also: Use IMAP in favor of POP3 - it has the advantage that the e-mails are not deleted on the server while they're downloaded to the client. If your mail system doesn't offer or support imap get in touch with your it admins.
 
Yogesh Gandhi
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:

Yogesh Gandhi wrote:The issue was with the Java Mail API version that was being used.
When I updated it to latest javax.mail.jar the problem was gone.


For the benefit of future readers of this conversation, what version did you upgrade from and to?



Well, I found 2 solutions for my problem.
1. To update Java Mail API to latest version (1.6) available as of today
2. TO add a line props.setProperty("mail.pop3.ssl.protocols","TLSv1.2") before making connect operation.

 
Matthew Bendford
Rancher
Posts: 326
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More interesting is the part of the question: Which was the old version you upgraded from?
Also: Adding tls1.2 explicit shouldn't be required on modern jvm - what java version do you use? I suspect an old java8 or maybe even some way older java7 or such ...
 
Tim Moores
Bartender
Posts: 7645
178
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yogesh Gandhi wrote:2. TO add a line props.setProperty("mail.pop3.ssl.protocols","TLSv1.2") before making connect operation.


If that means that the mail server in question still supports TLS 1.0 and/or TLS 1.1, run -don't walk- to disable those protocol suites. They're obsolete and no longer considered secure.
 
reply
    Bookmark Topic Watch Topic
  • New Topic