This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I want to incorporate an autoreply email system in my web based application made in jsp and servlets running on apache tomcat. Now in it when a user sends a mail with the code as subject, the application reads it and queries the database and has to send the information which on web is generated in jsp. The problem is (1) At the moment, when I load the servlet in web browser then only the mails are received. I want to know whether anything can be done where as soon as the mail is received, the servlet reads it and sends the reply. (2) How do I send the page on which information is there regards
I think after much typing and erasing on my part, I finally get what you're wanting... you want two interfaces to a web app. One in the normal fashion, where someone can make requests and receive information. They click links, say for a product brochure. The request might be something like: "http://www.myfoo.com/prods/show?pid=345" which would show us product number 345.
And in addition, you want to enable someone to access this same page of information, by emailing the system like so: to: firstname.lastname@example.org subject: 345
If this is so, then I think there might be a gap in your understanding of web apps and email, and I apologize in advance if I'm not correct.
1) At the moment, when I load the servlet in web browser then only the mails are received.
How is a servlet "receiving" email? What's sending it?
Your web app listens on port 80 for http requests. The email is being sent to port 25, and an email server is listening to that port. The email server doesn't even have to be on the same physical machine as your web server.. they have nothing to do with one another.
So you need to bridge the two.
In order for your webapp to 'receive' this email, you need to use JavaMail, and have a background process check a specified mailbox (in our example, the 'autoresponder' inbox) every 'n' seconds for emails. Upon finding an email in the inbox, it needs to retrieve it, parse out the subject (all very easy, through JavaMail) and after that, do its processing and database querying based on the code in the subject.
As for 2)... Again, you use JavaMail. You can have your background process 'reply' to the email it is currently processing, and using various methods of that API, send a plain-text or HTML response to the email.
How to actually "get" the JSP page that normally displays the page when a link is clicked, so that you can place that HTML output into an email? You can use an java.net.HttpUrlConnection to emulate an actual request.
Joined: Jun 24, 2002
Thanks for the reply Mike. I have already implemented java mail and receiving the mails and can also send the replies but only when I call the servlet from my browser. I want it to be done automatically something like Apache can call my servlet to be executed and I donot have to call it explicitly from my browser. One more thing I want to do if it can be incorporated is that whenever a new mail is received then only the servlet is invoked and it does not check the mail server every second when there is no received mail. thanks