Apache Commons Net POP3 connecting with Gmail - java

I am using Apache Commons Net's POP3 support to retrieve emails from my Gmail inbox. I use the following code to open a connecting with Gmail's POP3 server:
String server = "pop.gmail.com";
String username = "<my gmail>";
String password = "<my password>";
POP3Client pop3 = new POP3Client();
pop3.setDefaultTimeout(15000);
pop3.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
System.out.println("Connecting...");
try {
pop3.connect(server, 995);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
But this causes the following error:
java.io.EOFException: Connection closed without indication.
at org.apache.commons.net.pop3.POP3.__getReply(POP3.java:117)
at org.apache.commons.net.pop3.POP3._connectAction_(POP3.java:153)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:189)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
at com.sms.POP3Mail.main(POP3Mail.java:66)
Why is this happening, and how can I fix it?

You need to set the implicit flag in the constuctor to connect to Gmail and use the POP3SClient:
POP3SClient pop3 = new POP3SClient(true);

I found the problem. Google requires SSL when using POP3. I added SSL authentication and it's all good now.

Related

Server java app not working on a local connection

hi have at home a rasperry pi running a server java app, connected to de router with the dynamic DNS configured and the in/out communication ports openned.
When i run the android apication client througt 4g everithing is working sucessfull. But when i run the same app connected to the wifi on my local net, where the server are running, the server application looks like death.
Any ideas?
Thanks.
router config
I think the problem is related to your DNS. If you are connected using your wifi, you have to return a local IP-Address. Do you have any chance of configuring your router to return the raspberries local IP-Address for wifi-clients?
Take a look here: link
A simple solution, even if it is not very elegant, is adding the following conditions to your code:
If there is an error connecting to the Dyn DNS, try to connect to the local IP address. (In case you are in the Wifi LAN)
If the local IP address fails, try again your Dyn DNS (in case the user is a real user with real communication problems)
(repeat until the connection is successful)
You can also identify your testing devices (using Settings.Secure.ANDROID_ID, or the IMEI) and use the local IP only for them. Another option is making the URL configurable (with a hidden option for example).
Because of my app can run local, only needs to connect to server for updates. I have block the connection on the server app if the ip from client and server are equals.
At the moment is the best solution to keep the server app running properly.
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new
InputStreamReader(whatismyip.openStream()));
String ip = in.readLine();
try {
SSLServerSocketFactory sslFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) sslFactory.createServerSocket(PORT);
int idSession = 0;
while (true) {
SSLSocket socket = (SSLSocket)ss.accept();
if(socket.getInetAddress().getHostAddress().equals(ip)){
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
((ServidorThread) new ServidorThread(socket, idSession)).start();
idSession++;
}
} catch (IOException ex) {
ex.printStackTrace(System.out);
Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
}
}

Mail service GAE issue - sending mail exception "javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;"

I'd like to send mail from my GAE project. I've followed the documentation example...
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("xxx#xxxx.appspotmail.com", "Example.com Admin"));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress("xxxxx#gmail.com", "Mr. User"));
msg.setSubject("Your Example.com account has been activated");
msg.setText("This is a test");
Transport.send(msg);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
After deployment, I get this exception message
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
But the documentation says that:
When you create a JavaMail Session, if you do not provide any SMTP server configuration, App Engine uses the Mail service for sending messages
But it seems to try connecting to a SMTP server... and obviously there is no SMTP server on localhost...
I've never used this service... my quotas are full available.
Please, help me !
had the same issue today. just got it working. app engine sdk already includes the classes you will need to send email:
https://cloud.google.com/appengine/docs/standard/java/javadoc/com/google/appengine/api/mail/MailService.Message
that and the related classes are the way to invoke the mail service. replace your message classes with those, remove all references to javax.mail. one other thing in case you're referencing this (as I was):
https://cloud.google.com/appengine/docs/standard/java/mail/sending-mail-with-mail-api
I couldn't get it to work, doesn't looks like it would without an smtp host at least. Nice of google to provide nonsensical documentation for a non-working example in their example code base
also, if you follow the "who can send mail" link it tells you that any address of the form anything#[APP_NAME].appspotmail.com or anything#[APP_ALIAS].appspotmail.com should work. using my apps name resulted in "unauthorized sender", but using the app id from the dashboard worked. what should have been a ten minute solution turned into hours of drudgery, but I have a working emailer. thanks, google.
The Mail service API supports the JavaMail (javax.mail) interface which is included with the App Engine SDK. Using any other jars may create the issue. You may follow the code sample in Java 7 and Java 8 which demonstrate how to send mail.
I should note that outbound connections on ports 25, 465, and 587 are not allowed due to spam concerns, so the sender address of a message must be one of the optioned in this link.
You can take your application ID/name (which is the same as the project ID/name) through the dashboard.
Kindly note that Issue Tracker is reserved for reporting bugs and feature requests. If you encounter any issue related to APP_NAME or APP_ALIAS, it is recommended to report the issue there so that we would be able to dig into the problem.

UnboundID LDAP SDK startTLS "No extended operation handler" error

Created an in-memory UnboundID LDAP server. The server listens on an unencrypted port and a TLS port. I can bind to it with LDAP on the first port, and LDAPS on the second port. However, I cannot connect to it using startTLS. The error is:
LDAPException(resultCode=53 (unwilling to perform), errorMessage='No
extended operation handler is defined for extended request OID
'1.3.6.1.4.1.1466.20037'.', diagnosticMessage='No extended operation
handler is defined for extended request OID
'1.3.6.1.4.1.1466.20037'.')
Here is how the server is configured:
try {
InMemoryListenerConfig ldapsConfig = InMemoryListenerConfig.createLDAPSConfig("ldaps",
LDAPS_PORT,
getServerSSLContext().getServerSocketFactory());
InMemoryListenerConfig ldapConfig = InMemoryListenerConfig.createLDAPConfig("ldap",
LDAP_PORT);
InMemoryDirectoryServerConfig serverConfig serverConfig = new InMemoryDirectoryServerConfig(BASE_DN);
serverConfig.addAdditionalBindCredentials(BIND_RDN, BIND_PASSWORD);
serverConfig.setListenerConfigs(ldapConfig, ldapsConfig);
server = new InMemoryDirectoryServer(serverConfig);
server.startListening();
} catch (LDAPException e) {
fail(e.getMessage());
}
The message tells me I need to add some kind of handler to the server, but I cannot find an example in the docs I've skimmed.

IP address for connecting server on the same LAN using SignalR

I'm trying to create a SignalR connection between an ASP.NET server (SignalR-Server 3.0.0) and an Android client on a real device. So far I've managed to successfully establish a connection on the same computer using a console application client targeting the IP http://127.0.0.1:5000/.
The server computer and the Android device are both connected to the same LAN (over Wi-Fi), so I tried to set the target IP to be the server computer LAN IP, but an exception is thrown:
java.net.SocketTimeoutException: failed to connect to /192.168.14.167
(port 5000) after 15000ms
Here's the relevant client app, using SignalR Java client:
Platform.loadPlatformComponent(new AndroidPlatformComponent());
String serverUrl = "http://192.168.14.167:5000/signalr";
mHubConnection = new HubConnection(serverUrl);
mHubProxy = mHubConnection.createHubProxy("Hub");
ClientTransport clientTransport = new ServerSentEventsTransport(mHubConnection.getLogger());
SignalRFuture<Void> signalRFuture = mHubConnection.start(clientTransport);
try {
signalRFuture.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
return;
}
Am I missing anything/doing anything wrong? Any help will be appreciated.

Pass hashed password to Java Mail API

Good morning everybody,
I'm developping an ERP for my company with the GWT Framework and I would get
the number of unread emails using the Java Mail API.
I can do this but, the problem is I stores the SHA-512 hashed password on the
database and I would not pass the clear password to the Java Mail API, but just the hashed password to avoiding to transmit the clear password on the network.
I use this code to get the number of unread mail:
private static int getNumberOfUnreadMails() {
int numberOfUnreadMails = 0;
Properties properties = new Properties();
properties.put("mail.imap.host", "myserver.com");
properties.put("mail.imap.user", "developper#myserver.com");
properties.put("mail.imap.socketFactory", 143);
properties.put("mail.imap.socketFactory.class", "java.net.ssl.SSLSocketFactory");
properties.put("mail.imap.port", 143);
Session session = Session.getDefaultInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("developper#myserver.com", "mypassword");
}
});
Store store;
try {
store = session.getStore("imap");
store.connect();
Folder folder = store.getFolder("Inbox");
numberOfUnreadMails = folder.getUnreadMessageCount();
} catch (Exception e) {
e.printStackTrace();
}
return numberOfUnreadMails;
}
I can also use another hashing algorithm.
If you know a solution for my problem, thaks you in advance.
P.S.: Sorry for my poor English, I’m French.
Your IMAP-server will need the unhashed password to be able to authenticate. You probably are already using SSL (as you set mail.imap.socketFactory.class), so your password is never sent in the clear.
BTW: the correct way to use IMAP with SSL with javamail is to use the imaps protocol (and use the mail.imaps.*, not using the imap protocol and specifying an SSL socket factory as the socket factory. Also usually the IMAP with SSL port is 993, not 143 .

Categories

Resources