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.

Thanks,

razanah.razak commented: and jaf +0

Recommended Answers

All 14 Replies

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();
multipart.addBodyPart(part);
mimeMsg.setContent(multipart);
mimeMsg.setSentDate(new Date());
Transport.send(mimeMsg);
}

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.

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
<listener>
<listener-class>com.hecheng.util.ServletListenerTimmer</listener-class>
</listener>


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!");
}
}
]

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

Regards.

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(" ")
message.setFrom(from);
out.println("Kq");

InternetAddress to = new InternetAddress("to ");

out.println("K");
message.addRecipient(Message.RecipientType.TO, to);

message.setSubject("hi");
message.setText(" hello how r u");
Transport.send(message);
%>
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..

Thanks

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

commented: Work on your assignmetns, don't expect people to email you everything! +0

You too are too lazy to look back a few messages?

And do NOT ask for personal help, it's extremely RUDE.

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...
Thanks...
Take care...
Good luck...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.