hi experts,

Here there is a requirement in my project, like sending a automatic mail to a particular person,at particular time.

for examples:

If there is an expiry date for a customer's account, a week before the expiry date a mail should be send to the customer as a remainder.
But, at present I use to send mails manually.

If there is any suggestions or replies on this concept are appreciated.


Votes + Comments
and jaf
11 Years
Discussion Span
Last Post by flakita227

not something you'd use JSP for anyway. A background process started from your crontab would be the most logical choice, or alternatively an autostarting servlet running a background thread if the functionality is an integral part of a web application.


here is some example code , i use it to send google mail.

public void smtp(String receiver, String content) throws MessagingException {
if (smtpHost == null)
throw new MessagingException("smtpHost not found");
if (user == null)
throw new MessagingException("user not found");
if (password == null)
throw new MessagingException("password not found");
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost); //set smtp server
properties.put("mail.smtp.auth", "true"); //use smtp authen properties.put("mail.transport.protocol", "smtp");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(properties,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
MimeMessage mimeMsg = new MimeMessage(session);
if (sender != null) {
mimeMsg.setFrom(new InternetAddress(sender));
if (receiver != null) {
mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));
if (subject != null) {
mimeMsg.setSubject(subject, "GBK");
MimeBodyPart part = new MimeBodyPart();
part.setText(content == null ? "" : content, "GBK");

part.setContent(content.toString(), "text/html;charset=GBK");
Multipart multipart = new MimeMultipart();
mimeMsg.setSentDate(new Date());

Edited by happygeek: fixed formatting


Thanks for your response, the sample code given by you is to send a mail using javamail API and my problem is, that mail should be send in a scheduled time.

for example: every, week end. (or) by date.

any idea, please suggest.

Edited by happygeek: fixed formatting


that is simple ,if you use a j2ee container such as tomcat ,
here is a example

this is a ServletContextListener  you should config it in web.xml so it will run auto when tomcat starts.

this is the web.xml config

you may need to change some field to fill your condition

public class ServletListenerTimmer implements ServletContextListener {
private java.util.Timer timer = null;
private Log mylogger = LogFactory.getLog(this.getClass());
public void contextInitialized(ServletContextEvent event) {
timer = new java.util.Timer(true);mylogger.info("timmer is started");
timer.schedule(new SendReport(event.getServletContext()), 0, 60 * 60 * 1000);mylogger.info("add sendmail task");
public void contextDestroyed(ServletContextEvent event) {
timer.cancel();mylogger.info("timmer destoryed");

public class SendReport extends TimerTask {
private static final int C_SCHEDULE_HOUR = 22;
private static boolean isRunning = false;
private ServletContext context = null;
public SendReport(ServletContext context) {
this.context = context;
public void run() {
Calendar cal = Calendar.getInstance();
if (!isRunning) {
System.out.println("cal.get(Calendar.HOUR_OF_DAY) = " + cal.get(Calendar.HOUR_OF_DAY));
if (C_SCHEDULE_HOUR == cal.get(Calendar.HOUR_OF_DAY)) {
isRunning = true;
doReportTask();// you can write your sendmail task there
isRunning = false;
} else {
context.log("the prevenient task is still running!");

Edited by happygeek: fixed formatting


Thanks for your reply, well its working fine.and thanks for spending your precious time to give the solution.


Edited by happygeek: fixed formatting


hi, i'm a student currently doing my project. looking for send mail code. i have look through those codes which u guys hav discussed above but can't work. can someone help me? Thanks , i urgently need help. thank you.


I want to send a mail in my jsp page , but i want that address of the mail to whom i have to send that address is to be send by another jsp page .i am attaching both my jsp
Properties props = new Properties();
props.put("mail.smtp.host", "");
Session s = Session.getInstance(props,null);
MimeMessage message = new MimeMessage(s);
InternetAddress from = new InternetAddress(" ")

InternetAddress to = new InternetAddress("to ");

message.addRecipient(Message.RecipientType.TO, to);

message.setText(" hello how r u");
In "to" i want address from other jsp , what should i try, i had use session


Kids, do your own homework.

Someone already posted a complete working solution (a really stupid thing to do as now people don't have to think anymore), and you still can't get it?


Actually i dont know the name of my smtp host. should i write there localhost or my IP address... Please help me out..



if you don't know the mailserver you're talking to, you can't talk to it so you can't send mail.

It would be like wanting to make a phonecall without knowing the name of the person you're calling nor his number.


Hi Techkar,
I have the same requirement as u have. In my project i have to send a automatic email to that users who are idle i mean not updated their data in our data base for some times. or some days. If u finally got the solution then pls send me the code .My email id is-subhrajit.sahoo@gmail.com

Thanx in advance

Votes + Comments
Work on your assignmetns, don't expect people to email you everything!

Hi All...

I need execute a servlet any time of the date.
The example in this page... execute a task when the tomcat starts... but i have execute task in a date, hour, minute, second, it's read of the database mysql of an especific column table...
If any body have idea... tell me please...
Take care...
Good luck...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.