943,712 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 29531
  • JSP RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 24th, 2006
0

Automatic mail using jsp and javamail

Expand Post »
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,
Reputation Points: 10
Solved Threads: 1
Newbie Poster
techkar is offline Offline
18 posts
since Feb 2006
May 5th, 2006
0

Re: Automatic mail using jsp and javamail

Reputation Points: 48
Solved Threads: 7
Posting Whiz
aniseed is offline Offline
353 posts
since Apr 2006
May 12th, 2006
0

Re: Automatic mail using jsp and javamail

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Jun 7th, 2006
0

Re: Automatic mail using jsp and javamail

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);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lifayan is offline Offline
5 posts
since Jun 2006
Jun 7th, 2006
0

Re: Automatic mail using jsp and javamail

Quote originally posted by lifayan ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
techkar is offline Offline
18 posts
since Feb 2006
Jun 7th, 2006
0

Re: Automatic mail using jsp and javamail

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!");
}
}
]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lifayan is offline Offline
5 posts
since Jun 2006
Jun 13th, 2006
0

Re: Automatic mail using jsp and javamail

Quote originally posted by lifayan ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
techkar is offline Offline
18 posts
since Feb 2006
Jul 27th, 2006
0

Re: Automatic mail using jsp and javamail

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
loan is offline Offline
1 posts
since Jul 2006
Aug 24th, 2006
0

Re: Automatic mail using jsp and javamail

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
Last edited by nitin saxena; Aug 24th, 2006 at 12:51 am. Reason: wrong code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nitin saxena is offline Offline
1 posts
since Aug 2006
Aug 27th, 2006
0

Re: Automatic mail using jsp and javamail

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?
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: How to send request parameter to included jsp
Next Thread in JSP Forum Timeline: Display Xml In Jsp





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC