How could one go about accessing email without interfering in any way that would be visible to a user with standard email clients such as Thunderbird?
P.S: I've marked this as both java and language-agnostic so the approach can be described in general steps or detailled programatically.
You would like to access the mail server directly over network programmatically. You only need to know the address (URL) of the mail server (usually in flavor of smtp.domain.com), the port number (usually 25) and the login username and password (the one of the existing mail account at the mail server).
In low-level, you need to know socket programming. In Java, there's the java.net.Socket API for this. Also see this tutorial. To communicate with a mail server you need to learn the SMTP or IMAP protocols, depending on what the mail server in question understands, to send/retrieve commands as bytes over the socket accordingly.
In high-level, you can use a more convenient API which doesn't require you to understand the low level specifics (which may be pretty complicated and verbose). In Java, you can use the JavaMail API for this. It has an excellent FAQ with a lot of code examples.
Related
I am creating an Intra Office Messaging System in Java using client-server architecture. The features I want to implement are: real-time chatting and messaging to a specific user or group of users, file transfer and voice-chat.
I have implemented the server and the client module using which the client can login on the server. I have used DataInputStream and DataOutputStream for this. When user submits username and password, I store them in a single string separated by a semi-colon ";" and then I send this string to server using DataInputStream where I separate them and run a DB query and send appropriate acknowledgement to the client application.
Now I want to implement chatting and messaging. My question is, shall I use the same approach for this? Or is there a better solution? Also, how can I send the message to specific client(s) (client A wants to send message only to client B). While suggesting a solution, please keep in mind that I have to implement voice chat as well (gstreamer) as filetransfer!!!
Also, I maintain an array containing names of all clients who log-in to the server which is used to display the list of logged-in clients to each client.
Firstly, by sending the username and password as a plain-text string, you're practically giving them away freely - anyone with a few basic tools can sniff the username and passwords. You are going to need to read up on cryptography and how you can secure the connections. Java has a built-in cryptography library which makes this very easy to do.
If possible, I would recommend going for an already developed chat protocol like XMPP (Jabber), for which there already exist many free Java library implementations, such as Smack, that will do everything for you. There really shouldn't be any need to re-invent the wheel here unless you are doing this for a school project that doesn't allow any use of external libraries, which would be extremely ambitious in of itself. XMPP has support for text chat, voice chat, and file transfers.
There are also already several fully featured open-source chat clients that you could modify to suite your specific needs. One thing to keep in mind though, is the licensing on open-source projects. Some open-source licenses, like the popular GPL, require that using any part of the open-source project in your project requires that you release the source code for your entire project. This could be extremely disastrous for a corporation, so watch out.
If you still want to start from scratch, then you'll need to implement your own protocol of communication. You'll have to design this yourself, while taking into consideration how you'll incorporate gstreamer and file transfers.
Again, I would recommend at least looking at some already designed protocols, like XMPP, to get some ideas.
Usually, protocols have,
Some data explaining what type of request/response this is. This could be a numeric value stored as a single byte, or some text string like as is done in HTTP
Some more data pertaining to who the message is for. Could be an IP address, username, combination of both, etc. Not necessary if you're doing the communication directly, i.e. not through a middle-man server
The time the request was sent
The data itself
Some sort of encryption. Best initiated after the user was authorized
For example, a really basic protocol could be,
Request type: 1 byte. 1 = text, 2 = voice data, 3 = file transfer, 4 = request for currently logged-in client list
Destination: int (IP address)
Time: long. Best to send this as UTC time, e.g. what System.currentTimeMillis returns
Length of data: int
Data: variable length data, depending on type
Then, for each type of data being sent, you'd implement the data differently,
Text: string as sent by DataStream
Voice: voice data from gstreamer (not sure how gstreamer works)
File transfer:
File name: String as sent by DataStream
Length: long
Data: As read from FileInputStream
List of currently-logged in clients:
Data: as sent by DataStream.writeObject
Good luck
I have java web application to which I'd like to add emailing capabilities, however, I'm unsure what is needed to accomplish this. Specifically I want my app to be able to:
Send emails confirming sign-up
Allow users to send emails to one another, using my app's domain i.e. dan#my-app.com
From my research it seems I'll need a mail transfer agent (MTA) like Postfix and possibly a IMAP server like Courier; but I don't understand the need for the IMAP server.
Thanks.
You need code inside your web app to create and dispatch the email into the SMTP-world. Usually JavaMail is used for this, and you can either enclose it in your web application or (preferred) have the web container provide a correctly configured instance through JNDI. This is vendor specific.
If you do not have a SMTP-server for JavaMail to connect to (frequently this is Exchange for Windows shops), you can either get one running (ask your IT administrator) or use Google Mail or Hotmail or others if it is ok for your web application to send mail through them. It is a bit tricky to use GMail as a SMTP-server, but when set up correctly works very well.
You will need the SMTP-server, as it handles all the boring details regarding MX records and resending if the SMTP-server does graylisting, etc. etc.
Oh, and IMAP is for getting delivered mail, not sending mail. You don't need it.
If it's a Java web app, then the server part is a servlet. Given an email message sent from a client form, your server needs to send that text off as an email.
There's code in the Java EE stack to do this, or you can specifically download JavaMail. This will allow your programs to act as mail clients.
Your MTA receives messages from your servlet and sends them to the users. So far so good.
But you also need a postbox, i.e. the equivalent of a mail in-box for your users. Postfix, QMail and others offer a basic "flat" mailbox model, where mail is simply stored until the client picks it up, and then (usually) deleted. Access is via POP3. IMAP offers a lot more organizational capability, i.e. the ability to specify hierarchies of nested mailboxes, to transfer mails between them and so on. You probably won't want to create a GUI front end to all that complexity, so I'd guess you don't really need an IMAP server. You do, however, want a relatively simple POP3 server to allow your servlet to access the mailboxes via TCP/IP. This is usually part of the "standard" email server packages.
To have your own domain known to the world, you need access to the MX records of your DNS service, i.e. you have to set up one or two of your hosts, on an Internet-facing address, to be your post office.
Finally, if you want to save yourself a lot of trouble, be very careful in configuring your MTA (SMTP server) such that there is no chance for it being used as an open relay. i.e. it should not be possible for your users to send mail to the outside world in general (or hackers will find a way to abuse your Web interface to do this), and mail from the Internet should not reach your users. Most importantly, there should be no way for mail from the Internet to be forwarded to someplace else in the Internet. Find an open relay testing service (they're free) on the 'net and get one to run a test on your configuration once you think you're done.
EDIT:
Looking at Thorbjorn's answer, I realized you probably don't want your users receiving their mail through your app; they probably already have email providers and accounts of their own. In that case, you don't need to worry about inbox capability or a POP3 server. You could consider offering full email services at your domain but that's a very thankless job and if you have any choice, leave that dirty work to GMail, Yahoo, Hotmail and their ilk. Whatever service you provide will never please your customers enough, and you'll be fighting spam and other crime every day.
For starters your server has to have mailing abilities. In linux land sendmail is usually what this will be.
Additionally, check out javaMail.
http://www.oracle.com/technetwork/java/index-jsp-139225.html
I want my Java application to send out emails to users. But I cant get a good solution.
Now, I got some on Google but they use a SMTP server which I dont have. I was wondering if setting up one on my Linux machine would be easy?
So, I am using mailx now to send out emails but it sends emails from root which is definately not good. Is there any way to send out emails from a proper email using java? like you can do in php and other languages?
Use commons-email to send email from java in a simple, straightforward way (see the docs).
You need an SMTP server always - even in "php and other languages", but perhaps you don't know you need it, because it is bundled in your LAMP package.
One solution is to use google as an SMTP server. Either via your account, or via google apps. Otherwise setting up an smtp server (postfix for example) linux appears trivial, but isn't - you have to take into consideration many things - see this post by Jeff Atwood.
So ultimately, I'd suggest using the options provided by google.
if you'r looking to host your own mail server, then apache james is a pretty good option.
or other solution could be using a third-party mailservers such as gmail or yahoo; and use the JavaMail API to send emails.
If you don't have an SMTP server, Asprin is a send-only SMTP server, which is a pretty good fit. It suffers from the same problem any do-it-yourself SMTP server will, in that it will look more like a Spam source, so using a proper SMTP server used for e-mail should be done if possible.
I am building a web application that has to be able to do the following:
Send emails to the (external) addresses registered by the users (say, Gmail, Yahoo Mail, etc).
Send/receive emails to local mailboxes that live in the application. These are more "messages" in that sense.
What are the potential open source technologies I could integrate with here? Thanks for your inputs.
So, you want a SMTP server? If you want to have it in Java, then I can suggest to pick Apache James. But in fact every decent SMTP server would suffice. You can just use JavaMail API, or the more convenienced Apache Commons Email which is built on top of JavaMail API, to talk with any SMTP server to send/read emails.
If you didn't already realize, you can also just make use of an existing SMTP server provided by your ISP or the web hosting. In this case only JavaMail or Commons Email would have been sufficient.
While Commons Email will help you get the sending part done, receiving email will require that you access mailboxes via IMAP or POP through the Java Mail API.
Java Mail is a little cumbersome to use, but this Stackoverflow question has a working IMAP sample to help you get started.
There is a JCA (Java Connector Architecture) adapter that makes your Java EE server open port 25 for receiving mails.
http://sourceforge.net/projects/mailra/
A quite old tutorial which is incomplete with some examples for IMAP watching can be found here:
https://community.jboss.org/wiki/InboundJavaMail
A general introduction to JCA can be found here:
http://www.adam-bien.com/roller/abien/entry/a_simple_transactional_file_jca
I'm trying to implement a Web Anonymizer (like ktunnel) in java, but I really could not get the idea, I need some information about how a web anonymizer works. I really do not need the source or a sample application, just the idea or a tutorial explaining the anonymizer idea.
Thanks.
A basic anonymizer just acts as an encrypted proxy, creating an encrypted "tunnel" between a proxy server and a client, where all traffic from the client goes through the proxy. This accomplishes 3 things:
The client cannot* be determined by looking at traffic between the proxy and endpoint. Hosts on the other end just see the proxy server.
The content of a client's traffic is hidden from monitoring, because the connection to the proxy is encrypted.
It is impossible* to determine the endpoint for traffic originating from the client, because all of it appears to go to the proxy only.
*In reality, a simple anonymizer doesn't provide full protection, because if you look at the amount of traffic between client and proxy, and the traffic between proxy and various sites, you can associate a specific client with their traffic. This is called traffic analysis.
Fancier anonymizers, such as Tor, provide protection against traffic analysis and a lot of other techniques to break anonymity, BUT that's really beyond the scope of the question.
From your point of view, all that matters is writing the proxy software. Your program should be able to create and manage encrypted connections to clients. This means it needs to be able to (securely) initiate an encrypted connection to a host, pass on connections to external hosts, and then pass traffic back and forth. Basically, it needs to act as a router.
There are protocols in place for how to accomplish this -- I suggest you read up on the SOCKS protocol, or Tor. Your best bet if this is a learning project is to write basic SOCKS proxy software. If this is for actual use, there should be libraries in Java that provide the necessary services.
EdiT:
Ktunnel is a less fancy proxy -- it uses a CGI script to redirect information from a URL back and forth. Basically, you enter an address, it fetches the page for that address, and sends it to you. Fairly simple, actually.
I don't know ktunnel, but for basic information about anonymity networks have a look at Tor at wikipedia.