首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用石英发送电子邮件的工作,无法Session.getInstance

用石英发送电子邮件的工作,无法Session.getInstance
EN

Stack Overflow用户
提问于 2017-08-17 04:21:37
回答 1查看 930关注 0票数 0

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

这是我的职业:

代码语言:javascript
复制
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();
        }
    }


}

结果:

代码语言:javascript
复制
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

代码语言:javascript
复制
    <?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

代码语言:javascript
复制
# 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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-17 12:42:24

最后,它可以工作。我将库更新为Java版本1.6.0。它的工作原理是相同的代码。希望这对和我有同样问题的人有所帮助。您可以在以下链接中下载:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45726534

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档