RSS Forums RSS
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 16007 | Replies: 14
Reply
Join Date: Feb 2006
Posts: 18
Reputation: techkar is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
techkar's Avatar
techkar techkar is offline Offline
Newbie Poster

Question Automatic mail using jsp and javamail

  #1  
Apr 24th, 2006
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,
techkar
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Mumbai, India
Posts: 351
Reputation: aniseed is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 5
aniseed's Avatar
aniseed aniseed is offline Offline
Posting Whiz

Re: Automatic mail using jsp and javamail

  #2  
May 5th, 2006
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Automatic mail using jsp and javamail

  #3  
May 12th, 2006
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Jun 2006
Posts: 5
Reputation: lifayan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lifayan lifayan is offline Offline
Newbie Poster

Re: Automatic mail using jsp and javamail

  #4  
Jun 7th, 2006
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);
}
Reply With Quote  
Join Date: Feb 2006
Posts: 18
Reputation: techkar is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
techkar's Avatar
techkar techkar is offline Offline
Newbie Poster

Re: Automatic mail using jsp and javamail

  #5  
Jun 7th, 2006
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.
techkar
Reply With Quote  
Join Date: Jun 2006
Posts: 5
Reputation: lifayan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
lifayan lifayan is offline Offline
Newbie Poster

Re: Automatic mail using jsp and javamail

  #6  
Jun 7th, 2006
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!");
}
}
]
Reply With Quote  
Join Date: Feb 2006
Posts: 18
Reputation: techkar is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 1
techkar's Avatar
techkar techkar is offline Offline
Newbie Poster

Re: Automatic mail using jsp and javamail

  #7  
Jun 13th, 2006
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.
techkar
Reply With Quote  
Join Date: Jul 2006
Posts: 1
Reputation: loan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
loan loan is offline Offline
Newbie Poster

Re: Automatic mail using jsp and javamail

  #8  
Jul 27th, 2006
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.
Reply With Quote  
Join Date: Aug 2006
Posts: 1
Reputation: nitin saxena is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
nitin saxena nitin saxena is offline Offline
Newbie Poster

Help Re: Automatic mail using jsp and javamail

  #9  
Aug 24th, 2006
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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 19
Solved Threads: 200
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Automatic mail using jsp and javamail

  #10  
Aug 27th, 2006
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?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:47 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC