0

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

这是我的工作课程:

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)

石英.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>

石英属性

# 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

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

4

1 回答 1

0

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

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

于 2017-08-17T12:42:24.923 回答