parsing commands sent through email - java

I am really unsure as to where I should be posting this, but if this is the wrong place could someone direct me where to I could get an answer? I want to be able to send commands to my email address, and have the commands parsed and executed when the message is sent. I.e. I send an email and it contains this:
public class sentThruEmail {
public static void main(String[] args) {
System.out.println("Hello");
}
}
I would want to configure my program to recognize when a new email comes from a sent address, to open it, compile it (in this case compile the java) and then execute it on the machine that the program is running on.
How can I go about figuring out how to do this? Any help would be wonderful, thanks!
EDIT: Or maybe the first step would be how to recognize an email was received from an address at all? In Java, how could I go about that: recognizing that an email was received and outputting something to the screen to alert the fact.

This would be a very bad idea. Blindly accepting email from an untrusted source, compiling it, and executing it is an enormous security hole.

There are several parts of this to consider:
Security
As others mention, there are security risks to consider, here. If that worries you (and it should!), you may want to consider some of the following:
Digitally signing these "command" emails, and verifying that signature before looking at the email
PGP is a popular choice for this
Running your program (which reads the emails) in a "sandbox" environment, such as being chroot'ed or in a jail
Only run a very limited set of commands - perhaps just ones you invent.
Getting Mail
If you still want to do this, given the security issues, you will need your program to read mail.
You probably want to use IMAP for this, or POP.
Parsing
Once you have the mail, you need to parse the contents.
You could just compile it directly if you are only sending code.
You could also send the code as an attachment with a certain MIME type to identify it. That way you could still send a 'normal' email (perhaps with commentary about what this code is for), but your program would be able to cleanly separate out the code.
Responding
How will you communicate results back? Or do you care?
You may want to send a reply email (use SMTP), or update a webpage. A webpage is nice since if you are running the web server locally, you can just write a file directly.
Examples
The standard "confirmation email" system has a lot of similarity to what you describe. Someone sends an email to an automated system, it reads it, does some processing, and replies. Search around for those systems and I'm sure you'll get started.
I created the Exquisite Corpse Emailer project, which does much of what you describe (but in Perl). It only accepts a very small set of limited commands, but it listens to an email address on IMAP, parses the text, updates a database as a result, etc.

But if you want to do it, look into sockets programming, as you would need to connect to your email provider (if he allows terminal login)

Related

Display text in console while writing to it

I am making a chat program in Java. When a user sends a message to the server, the server sends that message to every other user. The problem is, when a user is typing, he can receive a message, that will be written in the middle of the text he is typing.
A user must always be able to receive messages, while at the same time being able to write without having text appearing in the message he is typing.
Is there any way to do this in Java? A good solution would be to have a part of the console where the messages appear, and another one solely for typing.
It seems that there are libraries similar to ncurses, but are there any solution that doesn't rely on external libraries?
Cleaning the console using ANSI escape codes doesn't help since the text an user is typing might simply disappear because of it.
You can create 2 seperate console applications (one which reads, and one which only sends). That way your console windows would be separate and text would not conflict. Bear in mind that if you are using sessions, you would need to bind both sessions to the same user.
Another solution could be reading and storing each individual key without pressing enter. When you receive the response from the other client, you manually clear everything, print the text received, then place whatever the user was typing directly after.
Both of the above solutions seem hacky, and I feel Swing or JavaFX would be the way to go.

Java link to view a specific email from the PST file in outlook

I have learned coding in JAVA for the sole purpose of creating a task manager where I can create tasks and keep all my files, phone calls and emails for the specific task in one place, or rather easily accessible from within the task. I have had success with all requirements but for the email. What I want to achieve is to have a "link" in my java program to a specific email in the PST file and when the link/button is clicked the email must be opened and viewed in Outlook.
I have tried Javamail and I can successfully access emails from the server - however I get too many emails per day and have to delete all emails from the server twice daily. The PST file seems like the best solution - it already has all the information I need without creating duplicates.
I have tried libpst and I can access all of my emails in the PST file with success. However, I cannot seem to pick one email and have it open in Outlook. Since I will use this program only to make my job easier I would prefer not to have to buy any "connectors" and the like - I could just as well then buy a program like this and where is the fun in that?
Any suggestions would be greatly appreciated.
Did you look into using the Outlook Object Model? COM libraries can be accessed in Java using Jacob. To open a message in Outlook by its entry id, use Application.Session.GetItemFromID.
I am not sure if this would work since I havent used the libpst library. Can you export a single email message as a .eml or .msg file? If you can, then you can open the file using outlook easily enough.
This snippet should help
Desktop desktop=Desktop.getDesktop();
desktop.open(<.eml/.msg file>);
Also, take a look the switches available to open outlook from the command prompt.
http://office.microsoft.com/en-in/outlook-help/command-line-switches-HP001003110.aspx

email response that what time email is opened and received

How I can track an email?
I'm using java on the server side for sending emails. I want to track whether it is delivered, opened, etc. How I can do that?
Well, e-mail isn't that simple.
You can use some techniques to get a better understanding of when an e-mail is opened, for instance, but it's not guaranteed.
One usual approach is to include an image, for instance (a beacon), that makes a request to your server when it's loaded.
Of course, some mail clients will ask the user if he allows the loading of external content. If he says no, there's nothing you can do.
Like posted in a comment, you can look up more here: http://en.wikipedia.org/wiki/Email_tracking and by googling the subject, a lot has been written about it.

How to protect a file so it can only be accessed by java?

Okay I have a folder say... http://oldserver.net/file/index.jar
How would I be able to protect the file "index.jar" from being downloaded from regular web browsers?
I've seen this done before, I just want to protect it from being accessed from web browsers, and keep it strictly java download access only.
What I mean by java download access only is, I could simply use a java downloader to download index.jar. But I can't download it via web browser.
How would I protect the file "index.jar" ?
Thanks:)
You need to think about what this requirement means specifically - from the point of view of your server, how can it tell whether an incoming request is a "java download" or not?
In short, I don't think there's a way to do exactly what you're after. The classic way to secure resources would be by requiring authentication (i.e. you need a valid username and password to get the index.jar file) but it doesn't sound like that's what you want here.
Bear in mind that Java simply sends HTTP requests (or other protocols that it knows how to speak) down a connection. Any other program could send identical requests, so there's quite simply no way to enforce this limit in the way that you've specified.
One approach that might simulate what you're after is to not have the index.jar accessible via HTTP, so browsers wouldn't be able to get at it by default, and then access it via another protocol in Java (e.g. FTP, SFTP, whatever). Though as mentioned above, any tool that can speak this new protocol would be able to download the file.
Another approach would be to look for Java-specific headers, such as the User-Agent field (assuming this is populated with something recognisable). But again - this is not secure, as any tool could send through the same headers and impersonate a java download.
If you mean in your question that you only want your files to be downloaded by a specific Java application, then things get a bit more feasible. You can distribute an application that contains some authentication (e.g. public/private key pair) and have the server challenge for this when index.jar is requested. But even then this is dubious - by definition the Java app has to contain sufficient information to authenticate as itself; and by definition you have to distribute this information publically; so it wouldn't be difficult to extract the private keys and have some other application masquerade as your Java one.
Basically, I can't see any way around this issue given the confines you've stated. If there's a narrower scope you'd be willing to entertain you may be able to come up with a viable compromise, but right now the answer is simply "no".
Technically, you can't. Whatever request HTTP Java makes, another HTTP client program can be made that makes the same.
However, you can make it slightly more difficult. You can put the file behind HTTP digest authentication and include the password in the JAR of the Java program the password or can check the user agent server-side.
See e.g. get_browser() and Apache 2 authorization and authentication.
You can make your file read only so that no one can modify actual file but there comes a problem that even java cant modify the file.
So if you need to modify the file,you need to make a new file of same extension and copy entire data from main file to new file and delete the main file and modify the new file and change the name of new file to old file name.
There is a function in java to set read only:
File newTempFile=new File("mytext.txt");
newTempFile.setReadOnly();
You could create a web application that serves your file. Here you could check for the user agent string in the request and decide on that user agent string whether to serve the file or not. For this to work, your "java downloader" must have an identifiable user agent string and your application must "know" it.
Of course any bad guy who knows this, could make his browser send you the same user agent string, so this in not really secure :-/
If your index.jar is very important, do not make it available for download in any of the methods mentioned. As soon as it is available for download and ends up on the client computer, inside java or not, it will be hacked.
Some code should only run on the server.

Access Hotmail Unread Mail Count via Java

I want to write an application using Java6 that can check a users Hotmail inbox for the 'unread message count'!
There is a Javascript API but I will not have a browser instance, and it seems that I need one to use it. (see stakoverflow question: 964392 )
I can use POP3, but since it does not support flags, I can only tell how many 'new' messages there are in the users Inbox since the last time I checked, not how many unread messages there are. ( This is my current implementation, it's not what is required, but is currently all I can achieve )
There is IMAP access, but only for 'premium users'(Hotmail users who pay).
There's also HttpMail access, but this is poorly documented, and from testing, seems it's also only for premium users.
Unfortunately, this similar question on msdn suggests this is impossible
EDIT:
All I can offer is a half-solution. You could create the html page containing the script suggested by the people on MSDN but instead of setting the value of an input box to the number of unread messages - you could use Ajax to post this number back to your application. This is, of course, not a very robust solution since it depends on the browser and may very well not be cross platform. Another thing you can do is read up on running Javascript on the JVM. I don't know how good that solution is, either, but I think it's more robust once (or rather if) you can get it to work.
One potential option could be to use the HTMLUnit Java headless web browser to make the requests. HTMLUnit has very good, but not perfect, JavaScript support to handle creating the dynamic content.

Categories

Resources