Mail in Java

Status
Not open for further replies.

hariharan

Journeyman
Hi,

I have a requirement in my project, I need to run a check every day on a database and send a report of that database to a certain group of people. Now my problem is how to send an email using the JSP and servlet. I just need to send a plain text mail and i have the list of ppl and the subject and body to be sent.

Please suggest the simplest solution for this. along with the code. I googled and found that there are two approaches one is SMTP client and another Java mail. Which is used for which and what are the requirements for each?

If necessary I can setup a SMTP host here in my organization and a dedicated ID for the same purpose.

Do send the code...

Please let me know on this..
 
OP
H

hariharan

Journeyman
JMS looks a bit complicated.

I need a simple solution and if the process needs to be automated, does it need to be run as a service? if so how?
 

ToyTowner

Right off the assembly line
Have you looked into JavaMail? I use that and is pretty easy to do.

And JMS is not complicated. Pretty easy stuff.

Service? No, you don't need any service. It depends on your automation requirement (regular scheduled, event based, etc.). Much depends on your application - what is it? Please elaborate - a standalone application. A 24x7 running server based app? If so, which platform? JBoss, WebLogic?

Regards,
TT.
 
OP
H

hariharan

Journeyman
Have you looked into JavaMail? I use that and is pretty easy to do.

And JMS is not complicated. Pretty easy stuff.

Service? No, you don't need any service. It depends on your automation requirement (regular scheduled, event based, etc.). Much depends on your application - what is it? Please elaborate - a standalone application. A 24x7 running server based app? If so, which platform? JBoss, WebLogic?

Regards,
TT.


Ok I shall have a look at JMS again.

I need to check up a database and send a mail to concerned ppl 45 days before a date field column in the database. This mail needs to be sent only when the 45 days limit is reached and this i guess i think i should be checking up every day whether 45 days limit is reached. The web portal runs 24 x 7 and the platform i have used is JBoss.
 

ToyTowner

Right off the assembly line
Good. Most of the problems solved.

You must be aware of a startup servlet.

Register a startup servlet in the web.xml. In this, start a thread. Check the current date in this thread. Query the DB and compare if its -45 from the DB date, if it is, then do the mail sending part. If it is not, let the thread sleep for 24 hours.

For the mail part, look into the javax.mail.* and javax.mail.internet.* packages / docs.

Code should be pretty simple and short.

Create a properties object, fill in the properties - protocol, host, port, from email id, etc.
Get a javax.mail.session instance using these properties
Construct a MimeMessage using the session and do a javax.mail.Transport.send() - pretty simple. You can find many examples using this process.

Note that you need not dwelve into JMS. Its not necessary for such a trivial activity as yours.
 
OP
H

hariharan

Journeyman
Good. Most of the problems solved.

You must be aware of a startup servlet.

Register a startup servlet in the web.xml. In this, start a thread. Check the current date in this thread. Query the DB and compare if its -45 from the DB date, if it is, then do the mail sending part. If it is not, let the thread sleep for 24 hours.

Thanx a lot.

I am pretty new to JSP. Can you please tell me more about that? I got how to do that in the web.xml file.

I am struck up with that in the servlet code, querying to the database is also fine with me, my problem is making the thread to sleep and then running it as a loop. how to do this. A sample code will help me.. Thanks again..
 

ToyTowner

Right off the assembly line
Since you figured out the startup Servlet part, you should already be having a method something like:

Code:
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}

All you need is yet another class that implements Runnable.
The run method checks the current time and the DB time, compares, if does not match the criteria (-45), then goes to sleep (for a duration of 45-x). If it is time (=45), then does the email sending part. and goes to sleep again for 45.

In the servlets constructor in the above code block, all you need to do is create an instance of your Runnable class:

Code:
Thread myThread = new Thread(myRunnableClass);
myThread.start();
 
OP
H

hariharan

Journeyman
^^^Thanx a lot..

can you pls tell how to use the

<run-at/> tag to run the startup servlet and is it a better solution to my problem?

All i need is a mail to be sent 45 days before a deadline, and that date when to send the mail i would be getting from a database..

so if i can use this <run-at> tag and set it to a particular time of the day, would it run the servlet at that time and query frm the db and send out the mail?



please help me on this....
 

Bandu

Journeyman
I did not know about the <run-at> before reading your post.
I looked it up on the internet and seems that this won't be useful for your requirement.

It allows the following formats:

Code:
<servlet servlet-name='test.HelloWorld'>
  <run-at>0:00, 6:00, 12:00, 18:00</run-at>
</servlet>

and
Code:
<servlet servlet-name='test.HelloWorld'>
  <run-at>:00, :15, :30, :45</run-at>
</servlet>

So, it seems that this can be useful for you.
All you have to do is register your class as the servlet in the XML, have a time value for run-at, say, 0:00 (i.e. midnight) and in the code, pick up your database date, see if its due for sending the emails, and if it is, fire the emails. If not, just don't do anything. Your class will be pinged again on the next mid-night.

Regards,
Bandu.
 
Status
Not open for further replies.
Top Bottom