we are retrieving mail from gmail server.we could connect to the gmail server without proxy. As there is a proxy setting in our college we couldn't connect to the gmailserver even after setting the system properties. we could connect to gmail.com from our browser but we couldn't connect from our program.
Properties props = System.getProperties();
props.setProperty("http.proxyHost", "proxyhost");
props.setProperty("http.proxyPort", "proxynumber");
props.setProperty( "mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty( "mail.imap.socketFactory.fallback", "false");
props.setProperty( "mail.imap.port", "993");
props.setProperty( "mail.imap.socketFactory.port", "993");
props.put("mail.imap.host", "imap.gmail.com");
Session session = Session.getDefaultInstance(props, null);
store = session.getStore("imap");
store.connect(dialog.getServer(),dialog.getUsername(),dialog.getPassword());
we get this exception
java.net.UnknownHostException: imap.gmail.com
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:618)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at EmailClient.connect(EmailClient.java:373)
at EmailClient.main(EmailClient.java:475)
by: java.net.UnknownHostException: imap.gmail.com
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(Unknown Source
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:109)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:585)
Chances are that your proxy uses NTLM authentication, I've had issues in the past with Java and NTLM authentication. Not sure if it is actually the proxy giving you issues though.
In any which case, instead of using the JavaMail API directly you might want to have a look at using the GMail API for Java g4j. It will abstract away most of the complexities of dealing with gmail in general.
Happy Coding!
JavaMail does not currently support accessing mail servers
through a web proxy server.
If your proxy server supports the SOCKS V4 or V5 protocol and allows
anonymous connections, and you're using JDK 1.5 or newer and JavaMail
1.4.5 or newer, you can configure a SOCKS proxy
Related
I am facing issue while authentication with LDAP server. Application code(.ear) is deployed in WebLogic Server.In WebLogic server, code is not connecting with LDAP, and the same code setting is working in the tomcat server without any issue.
Note: Sometimes only the first time it is authenticating with WebLogic server.
Java version :java version "1.7.0_80" and weblogic server : 10.3.6.0
Keystore is working perfectly and I have tested with simple java program.
System.setProperty("java.naming.ldap.factory.socket", "javax.net.ssl.SSLSocketFactory");
System.setProperty("javax.net.ssl.trustStore","TheKeyStore.keystore");
System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
JSSESocketFactory jsseFactory = new JSSESocketFactory(null);
ld = new LDAPConnection(jsseFactory);
ld.connect(host,port);
Here is the exception i am getting while connecting ldap in weblogic.
netscape.ldap.LDAPException: JSSESocketFactory.makeSocket XXXXXXXXXXXXXXXXXXXXXXXXXXXX:636, Remote host closed connection during handshake (91); Cannot connect to the LDAP server
at netscape.ldap.factory.JSSESocketFactory.makeSocket(Unknown Source)
at netscape.ldap.LDAPConnSetupMgr.connectServer(Unknown Source)
at netscape.ldap.LDAPConnSetupMgr.openSerial(Unknown Source)
at netscape.ldap.LDAPConnSetupMgr.connect(Unknown Source)
at netscape.ldap.LDAPConnSetupMgr.access$000(Unknown Source)
at netscape.ldap.LDAPConnSetupMgr$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:662)
I have checked TLS protocol and it is not working.
Please advice for this issue
As per this question, Java 7 defaults to TLS 1.0, which can cause the error you mention if that protocol is not accepted.
The company I work for creates software that crawls and indexes various sources for use in search, in this case we're completely stuck on attempting to connect to Lotus Notes over SSL. This is a classic case of "it worked in dev, but failed in prod".
The problem is further compounded by the fact that we're devs and techies, not Lotus Notes experts.
Basically we have a piece of Java software that connects to Lotus to grab the DIIOP IOR text file using this command:
String ior = NotesFactory.getIOR(host, userIOR, pwdIOR);
Now, this is a non-SSL connection and requires username and password to connect. This works perfectly every time.
Then we attempt to create a session using this:
session = NotesFactory.createSessionWithIOR(ior, args, this.user, this.pwd);
where:
args[0] = "-ORBEnableSSLSecurity";
When connecting we consistently receive this stack trace:
NotesException: Session closed due to communications failure
at lotus.domino.cso.ORBCallback.make_error(Unknown Source)
at lotus.priv.CORBA.iiop.Generic.make_error(Unknown Source)
at lotus.priv.CORBA.portable.ObjectImpl._invoke(Unknown Source)
at lotus.domino.corba._IObjectServerStub.createSession(Unknown Source)
at lotus.domino.cso.Session.initSession(Unknown Source)
at lotus.domino.cso.Session.<init>(Unknown Source)
at lotus.domino.cso.Session.createSession(Unknown Source)
at lotus.domino.NotesFactory.createSessionUP(Unknown Source)
at lotus.domino.NotesFactory.createSessionWithIOR(Unknown Source)
[ ... ]
Caused by: org.omg.CORBA.COMM_FAILURE: java.net.SocketException: Connection reset: Connection closed: Host: <redacted> Port: 63149 vmcid: 0x0 minor code: 1 completed: Maybe
at lotus.priv.CORBA.iiop.IIOPConnection.purge_calls(Unknown Source)
at lotus.priv.CORBA.iiop.ReaderThread.run(Unknown Source)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:154)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at lotus.priv.CORBA.iiop.Message.readFully(Unknown Source)
at lotus.priv.CORBA.iiop.Message.createFromStream(Unknown Source)
at lotus.priv.CORBA.iiop.IIOPInputStream.prefill(Unknown Source)
at lotus.priv.CORBA.iiop.IIOPConnection.createInputStream(Unknown Source)
... 1 more
I understand the most common reason for this is because we're not correctly importing the TrustedCerts.class file, however after double and triple checking it's in the class path just fine (and the fact it works in dev environment) we're confident that it's being loaded in.
We know if we disable SSL (aka remove the aforementioned arg) our code will connect to the non-SSL DIIOP port 63148 and work just fine however it is a project requirement that all data must be passed through SSL.
Which finally brings me to my actual question: How do we get Lotus to report why this connection is failing?
I've tried upping the DIIOP log level to 4 which apparently should show all transmissions but the logs show nothing when I attempt to connect. Also it's worth noting the DIIOP service is definitely running and on the correct ports as well.
I've also checked that we're making it through the firewall to the remote box and TCPDUMP on our RedHat machine clearly shows that we establish a connection with the remote Lotus Notes host before it responds closing the connection.
[Pertinent Information]
Application: Java
Library: NCSO.jar
Lotus Version: Domino 8.5.3 FP6
Lotus Server: Windows Server 2008 Standard
Ports: 63148 (non-SSL), and 63149 (SSL)
Alright, the answer was clear as crystal but not something I was actively looking for.
The reason this doesn't work is that the third party certificate is not a SHA-1 certificate and Lotus Notes 8.5.x only supports SHA-1 (as per this documentation).
So basically that certificate of theirs can never be used for SSL authentication programatically or otherwise.
You mentioned DIIOP logging, but if the connection is failing in the SSL handshake, then the guts of the diiop task probably don't know what's wrong since that's occurring at a lower level in the stack. There are, however, additional debug variables that can be set on the server.
DEBUG_SSL_HANDSHAKE=1
DEBUG_SSL_CIPHERS=1
DEBUG_SSL_ALL=1
The last setting can be pushed up to 2 or 3 for more detailed information.
Restart of the server (or possibly of the server's diiop task) may be required to get the settings to take effect.
I'm trying to use JavaMail in order to connect to an IMAP Server (on a remote virtual linux machine - Ubuntu 14 LTS) but I'm getting the following exception:
java.net.ConnectException: Connection refused
I've already tried to establish the connection via Telnet and everything works.
The JavaMail version I'm using is 1.5.5 (also tested with 1.4.7) and I'm using the following properties:
Properties properties = System.getProperties();
properties.setProperty("mail.imap.host", SERVER);
properties.setProperty("mail.store.protocol", "imap");
properties.put("mail.imap.port", PORT);
Session session = Session.getInstance(properties, null);
Store store = session.getStore("imap");
store.connect(USERNAME, PASSWORD);
Notes:
It's working on my personal machine
It's working on another external machine
It's failing on the remote linux virtual machine
The IMAP server is on a different machine - which is accessible to all of the previous
I've done some research and some people told that it could be a firewall/blocking issue but how am I able to connect via Telnet?
Thanks.
Edited
The telnet command I've applied in the test-run:
telnet <server_ip> <port>
After this, the connection is successfuly established and I'm able to apply IMAP commands such as list status.
The output from javamail when it fails is the following:
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:538)
... 12 more
Firewalls usually work port based. This means telnet (23/TCP) can be allowed whereas IMAP (usually 143/993) is blocked.
You should check the firewall and check if the IMAP port is open.
Where is your program running? If you change getDefaultInstance to getInstance does it work?
I've found a solution!!
I performed a network capture on the remote machine and I saw that for some reason the javamail api was using the default IMAP folder and not the one I've selected..
Further tests revealed that changing the property value type to String instead of Integer solved this issue. It's important to notice that this didn't happen on other environments so I'm not sure the reason I had to make this change.
MyCode:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailTLS {
public static void main(String[] args) {
final String username = "myemailid#gmail.com";
final String password = "myemailpassword";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemailid#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("myfriedemail#ymail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler,"+
"\n\n No spam to my email,please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
My Error:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at Mail.SendMailTLS.main(SendMailTLS.java:56)
Caused by: javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1970)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at Mail.SendMailTLS.main(SendMailTLS.java:51)
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:525)
at java.net.Socket.connect(Socket.java:475)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 7 more
Java Result: 1
I am having the Local proxy as 172.17.0.4:8080
with username as :user1, password as: user2 Solutions pls??
Try this server:
smtp.googlemail.com
If you are under a proxy I think you can't use Java Mail.
From : oracle.com
Q: How do I configure JavaMail to work through my proxy server?
A: JavaMail does not currently support accessing mail servers through a web proxy server. One of the major reasons for using a proxy server is to allow HTTP requests from within a corporate network to pass through a corporate firewall. The firewall will typically block most access to the Internet, but will allow requests from the proxy server to pass through. In addition, a mail server inside the corporate network will perform a similar function for email, accepting messages via SMTP and forwarding them to their ultimate destination on the Internet, and accepting incoming messages and sending them to the appropriate internal mail server.
Update
Q: How do I configure JavaMail to work through my proxy server? [updated!]
A: Starting with JavaMail 1.6.0, JavaMail supports accessing mail servers through a web proxy server. Set the "mail.protocol.proxy.host" and "mail.protocol.proxy.port" properties for the proxy server. Proxy server BASIC authentication is supported by setting the "mail.protocol.proxy.user" and "mail.protocol.proxy.password" properties.
In addition, if your proxy server supports the SOCKS V4 or V5 protocol (http://www.socks.nec.com/aboutsocks.html, RFC1928) and allows anonymous connections, and you're using JDK 1.5 or newer and JavaMail 1.4.5 or newer, you can configure a SOCKS proxy on a per-session, per-protocol basis by setting the "mail.smtp.socks.host" property as described in the javadocs for the com.sun.mail.smtp package. Similar properties exist for the "imap" and "pop3" protocols. Authentication for SOCKS servers is supported by the JDK by setting the "java.net.socks.username" and "java.net.socks.password" System properties (and thus apply to all SOCKS connections) as describe in the JDK Networking Properties documentation.
If you're using older versions of the JDK or JavaMail, you can tell the Java runtime to direct all TCP socket connections to the SOCKS server. See the Networking Properties guide for the latest documentation of the socksProxyHost and socksProxyPort properties. These are system-level properties, not JavaMail session properties. They can be set from the command line when the application is invoked, for example: java -DsocksProxyHost=myproxy .... This facility can be used to direct the SMTP, IMAP, and POP3 communication from JavaMail to the SOCKS proxy server. Note that setting these properties directs all TCP sockets to the SOCKS proxy, which may have negative impact on other aspects of your application.
When using older versions of JavaMail, and without such a SOCKS server, if you want to use JavaMail to access mail servers outside the firewall indirectly, you might be able to use a program such as connect to tunnel TCP connections through an HTTP proxy server. Configure JavaMail to use the connect instance as the SOCKS server.
This happens because of Gmail security...
just allow access to your gmail account via apps go to under your account:
https://www.google.com/settings/security/lesssecureapps
and it will work .. smtp.gmail.com is correct.No need to change it.
Also don't forget to check internet connection as well.
could you try with props.put("mail.smtp.host", "gmail.com"); without smtp.gmail.com
Just for development purposes, you can try to open tunnel via your proxy to smtp.gmail.com
proxytunnel -q -p proxy_host:proxy_port -d smtp.gmail.com:465 -a local_port
and use in your code
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "local_port");
It works fine when you uprade javamail to 1.6.2
I have a java complied package to speak with the https server on net. Running the compilation gives the following exception:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source)
I think this is due to the connection established with the client machine is not secure. Is there any way to configure the local machine or ports in order to connect to the remote https server?
I think this is due to the connection
established with the client machine is
not secure.
It is due to the fact that you are talking to an HTTP server, not an HTTPS server. Probably you didn't use the correct port number for HTTPS.
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
You should have a local SMTP domain name that will contact the mail server and establishes a new connection as well you should change the SSL property in your programming below
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection
props.put("mail.smtp.socketFactory.fallback", "true"); // Should be true
I got the same error message when I forgot to log in to the company firewall, before performing a POST request through a proxy.
I got the same error. it was because I was accessing the https port using http.. The issue solved when I changed http to https.
Adding this as an answer as it might help someone later.
I had to force jvm to use the IPv4 stack to resolve the error. My application used to work within company network, but while connecting from home it gave the same exception. No proxy involved. Added the jvm argument
-Djava.net.preferIPv4Stack=true and all the https requests were behaving normally.
If you are running local using spring i'd suggest use:
#Bean
public AmazonDynamoDB amazonDynamoDB() throws IOException {
return AmazonDynamoDBClientBuilder.standard()
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials("fake", "credencial")
)
)
.withClientConfiguration(new ClientConfigurationFactory().getConfig().withProtocol(Protocol.HTTP))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("localhost:8443", "central"))
.build();
}
It works for me using unit test.
Hope it's help!
I face the same issue from Java application built in Jdevelopr 11.1.1.7 IDE. I solved the issue by unchecking the use of proxy form Project properties.
You can find it in the following:
Project Properties -> (from left panle )Run/Debug/Profile ->Click (edit) form the right panel -> Tool Setting from the left panel -> uncheck (Use Proxy) option.
i solved my problem using port 25 and Following prop
mailSender.javaMailProperties.putAll([
"mail.smtp.auth": "true",
"mail.smtp.starttls.enable": "false",
"mail.smtp.ssl.enable": "false",
"mail.smtp.socketFactory.fallback": "true",
]);
In case you use Jetty version 9 or earlier you need to add it to jetty
by
RUN java -jar ${JETTY_HOME}/start.jar --add-to-startd=https
and according to this
Jetty: How to use SSL in Jetty client side
from Jetty version 10 it should work out of the box
In case you are running
Cisco AnyConnect Secure Mobility Agent
Cisco AnyConnect Web Security Agent
try stopping the service(s).
In our corporate network this IS the solution to the issue.
It worked for me now, I have change the setting of my google account as below:
System.out.println("Start");
final String username = "myemail#gmail.com";
final String password = "************";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Transport transport=session.getTransport();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("myemail#gmail.com"));//formBean.getString("fromEmail")
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("myemail#gmail.com"));
message.setSubject("subject");//formBean.getString(
message.setText("mailBody");
transport.connect();
transport.send(message, InternetAddress.parse("myemail#gmail.com"));//(message);
System.out.println("Done");
} catch (MessagingException e) {
System.out.println("e="+e);
e.printStackTrace();
throw new RuntimeException(e);
}
Though I have enabled SSL and TSL while running program in this link of same post. I spend a lot of time but than I realized and found this link.
And done 2 following steps and setting control in google. :
Disable the 2-step verification (password and OTP)
Enabling to allow to access less secure app(Allow less secure apps:
ON.)
Now I am able to send mail using above program.
As EJP said, it's a message shown because of a call to a non-https protocol.
If you are sure it is HTTPS, check your bypass proxy settings, and in case add your webservice host url to the bypass proxy list
if connection is FTPS test:
FTPSClient ftpClient = new FTPSClient(protocol, false);
protocol = TLS,SSL
and false = isImplicit.
I got the same issue and it got resolved by setting "proxyUser" and "proxyPassword" in system properties.
System.setProperty("http.proxyUser", PROXY_USER);
System.setProperty("http.proxyPassword", PROXY_PASSWORD);
along with "proxyHost" and "proxyPort"
System.setProperty("http.proxyHost", PROXY_ADDRESS);
System.setProperty("http.proxyPort", PROXY_PORT);
Hope it will work.
I was facing this exception when using Gmail.
In order to use Gmail I had to turn ON "Allow less secure apps".
This Gmail setting can be found at https://www.google.com/settings/security/lesssecureapps after login the gmail account.
I've got similar error using camel-mail component to send e-mails by gmail smtp.
The solution was changing from TLS port (587) to SSL port (465) as below:
<route id="sendMail">
<from uri="jason:toEmail"/>
<convertBodyTo type="java.lang.String"/>
<setHeader headerName="Subject"><constant>Something</constant></setHeader>
<to uri="smtps://smtp.gmail.com:465?username=myemail#gmail.com&password=mypw&to=someemail#gmail.com&debugMode=true&mail.smtp.starttls.enable=true"/>
</route>
Maybe your default cerficate has expired. to renew it through admin console go "Security >SSL certificate and key management > Key stores and certificates > NodeDefaultKeyStore > Personal certificates" select the "default" alias and click on "renew" after then restart WAS.
If you're running the Java process from the command line on Java 6 or earlier, adding this switch solved the issue above for me:
-Dhttps.protocols="TLSv1"