i am new to quartz and javamail but i need to send email by batch job. I tried to 1. send email with single java class: success 2. run job with quarts (only println): success 3. run job with quarts (to send email): failed
this is my job class:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import org.quartz.*;
public class EmailJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
EmailSender es = new EmailSender();
es.sendBillingEmail();
System.out.println("Job Email Runnning");
final String username = "abcde@mydomain.com";
final String password = "xxxxxxxxxx";
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.mydomain.com");//smtp.gmail.com
props.put("mail.smtp.port", "587");//587
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
System.out.println("Job Email Running 2");
// Sending email
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abcde@mydomain.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("myemail@gmail.com"));
message.setSubject("Test Email");
String body = "Dear Bapak Recipients"
+ "<br>Just test email";
message.setContent(body, "text/html; charset=utf-8");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
the result:
Job Email Running (i got this message in console)
Job Email Running 2 (i code this but didnt see in console,
my suspect theres an error when creating session)
quartz.xml
<?xml version="1.0" encoding="UTF-8"?>
<job-scheduling-data
xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
version="1.8">
<pre-processing-commands>
<delete-jobs-in-group>*</delete-jobs-in-group> <!-- clear all jobs in scheduler -->
<delete-triggers-in-group>*</delete-triggers-in-group> <!-- clear all triggers in scheduler -->
</pre-processing-commands>
<processing-directives>
<overwrite-existing-data>true</overwrite-existing-data>
<ignore-duplicates>false</ignore-duplicates>
</processing-directives>
<schedule>
<job>
<name>EmailJob</name>
<job-class>jobs.EmailJob</job-class>
</job>
<trigger>
<cron>
<name>EveryWorkDay</name>
<job-name>EmailJob</job-name>
<cron-expression>0 0/2 * 1/1 * ? *</cron-expression><!-- 0 0 20 ? * MON-FRI * -->
</cron>
</trigger>
</schedule>
</job-scheduling-data>
quartz.properties
# Generic configuration - probably not needed, most of this is just the defaults
org.quartz.scheduler.instanceName = MyScheduler
org.quartz.scheduler.instanceId = 1
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 20
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
# Configure it to look in the quartz.xml for the job schedules
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz.xml
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 120
there is no error in console. i have no idea where is the mistake. Need help from u guys.
finally, it can work. I updated library to Java Mail version 1.6.0.. and its work with the same code. Hope this help to other with same problem with me. You can download it in this link: