How to use javamail to connect to ms exchange webmail? - java

I checked that I am not able to access the pop 3 port and imap port. I think I can only access via https webmail.
So for this case how do I actually connect to the microsoft exchange server and read the emails inside using java mail can anyone provide any specific code samples to achieve this?

Connecting to Exchange via OWA sounds like its going to be difficult; here's a post which deals with at least the authentication side of things.
In any event, you can connect to an Exchange server via IMAP if the IMAP service is turned on. Check with your server admin to confirm the service is running. If it is running, see this other post for how to set connection parameters.

Related

how to secure Socket communication so that only authorised client application can connect or listen to the port of server application

I have server application which runs on local host and the client also runs on local host.
As of now I am using java.net.serversocket and any application which has the ip and port detail of server can listen to the port.
My requirement is to secure the ports or secure the communication between the server and client application such that only my application client (authorised) one can listen to the ports or connect to server application. The data sent to and from client and server also has to be secured.
Apologies if naming conventions are not correct. I have been searching for solution and couldn't get anything for this, all I got is how to connect and make application communicate using socket programming , but no where I got the answer as how to secure the communication.
What you need here is some sort of authentication method to authorise only your client to communicate with the server. If you are using an existing communication protocol then it might have a specification for authentication already. If you are using your own protocol then you'll have come up with your own design for authentication.
It could be as simple as the server issuing some sort of request for authentication to the client. The client would then have to provide a satisfactory response (eg a user/password) otherwise the server would close the connection.
I would recommend taking a look at how some other protocols (eg HTTP) handle authentication to get some insight and also understand potential pitfalls.

Use Javamail through a proxy

I have a Java application which uses Javamail (v.1.5.4) for send and review mails into a POP/SMTP mail server. This application must run in our coporate network, where we have a proxy (f*ck!) that blocks my requests. I have googled possible solutions and Javamail says that we can use SOCKS5:
Q: How do I configure JavaMail to work through my proxy server? [updated!]
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.
That solution is not valid for me, so I have to look for new alternatives. Some people say that they implements a custom SocketFactory, but I am not sure if that is enough. Someone has tried?
Another possible solution could be use another library, but I don't find anything that could avoid this proxy.
Has anyone treat this problem? How do you solve it?
Summary
Problem: I have to send and read e-mails in a Java application, but my proxy blocks the requests.
What I have tried? Using javamail, I have tried to use SOCKS5 solution, but with no effect.
What I am looking for? A way to avoid this proxy. Someone tells about a custom SocketFactory(but I am not sure if this is valid). I don't find any alternative to Javamail.
Regards!!
Actually JavaMail does support SOCKS proxy, just not authenticated proxies.
That solution is not valid for me
But you don't explain why.
There's another way of configuring Java Mail with SOCKS proxy (even authenticated one) that doesn't involve configuring your own socket factory. There's an open source library called Simple Java Mail (full disclosure: I maintain it), which is really simple to use:
new Mailer(
new ServerConfig("smtp.host.com", 587, "user#host.com", "password"),
TransportStrategy.SMTP_TLS,
new ProxyConfig("socksproxy.host.com", 1080, "proxy user", "proxy password")
).sendMail(email);
However, if your proxy is actually an HTTP proxy, you're out of luck and you will need to resort to something like Corkscrew or connect.
Although it's not a programmatic solution, the cleanest way would be to check if your company has an internal mail server and use that one to send your emails. It doesn't require using SOCKS or proxies, just configuration.
A nice side-effect may be that emails sent in name of your company are also sent by your company. If the mail administrators have set-up SPF records correctly, it greatly reduces the risk of your emails ending up in someone's junk / spam folder.
Using your own mail server is generally the best solution, but if you don't have your own mail server the JavaMail FAQ describes other solutions, such as using Corkscrew or connect to work through your web proxy server.
As per the latest release of Javamail API 1.6.2 , JavaMail supports accessing mail servers through a web proxy server and also authenticating to the proxy server. See my answer here stackoverflow.com/questions/36278073/how-to-let-javamail-support-http-proxy/52855090#52855090

not getting mail from Java application using smtp

I have a java application which send mail using SMTP,but recently a modified its property file and restarted my tomcat server [although property change is a password change and is not related to smtp ].
now it is not sending mails :(
I checked my smtp server and tried to send mail from it.It is sending.
Any idea what went wrong?
any help would be appreciated
thanks in advance
Your SMTP server logs should tell you whether something was sent to it, and why it didn't relay the email.
If your change was password related, an authentication error may be the cause.
I would look at the following first:
Which credentials is your Java application using to send requests to the SMTP server?
Then, are these credentials still accepted on the server?
Finally, most SMTP servers will have a separate list of either included or excluded source IPs, from where to accept message requests, see if your Java app's source machine is allowed there.

Accept only connections from my application

I am working on a java project where we have a server and a client application. The server accepts a connection and sends requested data to a client through socket programming.
Everything works fine but when uploaded and working on server I get connections from unknown ip's. And this application will be used from many countries so there wont be specific ip's to whitelist.
Is there a way to ban / reject these ip's so that only connections from my application should be accepted by the server using sockets. Is it possible to send custom data when requesting connections to the server so that it will tell the server to accept only these connections.
The selective acceptance you describe falls within the scope of authentication and authorization. You will want connecting clients to:
Identify themselves to you, so you can determine wether they are allowed access to the server. This can be accomplished by many means, ie IP or MAC address whitelisting, client side certificates, basic/digest authentication, or some other custom a uthentication scheme.
Once allowed access, you can further scope down what the connecting client can do in the system via authorization rules.
I recommend taking a look at libraries like Apache Shiro, that will do some of the heavy lifting for you.
After accepting the inbound connection you can use Socket.getInetAddress() on the returned Socket to retrieve and subsequently validate the IP.
If the IP is not allowed, then use Socket.close() to close the unwanted connection.
Edit:
Validation can of course be based on things beyond just IP. Once the connection is open you can use its streams to transfer arbitrary data to identify the client for instance closing the connection following an authentication failure.
Doing this you should, however, consider the possibility of someone being able to intercept your communications. In other words using a secure connection would make sense.
I'm not aware of a way in which you can authenticate clients in Java prior to opening (accepting) the connection.
If your server and client should be validated, you should think about using certificates also.
Here are some more information :
the java class
another SO question

Java mail: sending email without SMTP

I want to send an email without using SMTP protocol. Is that possible to implement using Java? Since, my remote machine does not have access to google, yahoo and other accounts. and even my office mail can not be configured using SMTP server due to some security issues. Is any other way to send an email from remote machine.
The JavaMail section at java.sun.com lists many third party products that plugin to the JavaMail API. Hopefully one of those will fit your needs but I can't be more specific because you don't say what non-SMTP sending options you have open to you.
You could setup Your own SMTP server on remote machine, IMHO, it is better than incorporate it into program directly.
I want to send an email without using
SMTP protocol. Is that possible to
implement using Java?
With Java you can implement any Layer-5 network protocol.
ALL mail servers using SMTP to receive messages. At any time you have to connect with SMTP to the destination mail server.
If you cannot get out from local network to the Internet with some services you will need a proxy or network tunnel to connect the destination.

Categories