作业发送电子邮件与石英,无法Session.getInstance

时间:2017-08-17 04:21:38

标签: java cron javamail quartz

我是石英和javamail的新手,但我需要通过批处理作业发送电子邮件。我试过了 1.发送单个java类的电子邮件:success 2.用夸脱(只有println)运行工作:成功 3.用夸脱运行工作(发送电子邮件):失败

这是我的工作班:

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();
        }
    }


}

结果:

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

控制台中没有错误。我不知道哪里出错了。需要你们的帮助。

1 个答案:

答案 0 :(得分:0)

最后,它可以工作。我将库更新到Java Mail版本1.6.0 ..并使用相同的代码工作。希望这对我有同样问题的其他帮助。您可以通过以下链接下载:

https://github.com/javaee/javamail/releases

相关问题