Java邮件如何在条件下自动发送电子邮件 [英] Java mail how to send automatically an email on condition

查看:177
本文介绍了Java邮件如何在条件下自动发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前从未使用过Java邮件。

Never used Java mail before.

在我的JSF网络应用程序中,我有一个实体(followUp),其属性为 private Date checkDate; ,对应于 Animal 实体。 (动物有很多后续记录)。否则:

In my JSF web app, I have an entity (followUp) with a property private Date checkDate; that corresponds to an Animal entity. (An Animal has many followup records). Otherwise:

用户必须每3个月在 {followUp} 中创建一条新记录并将其标记为已选中且提供他的行动日期,即checkDate。但由于用户非常懒惰,他只为少数动物做到了这一点。
所以他实际上希望通过电子邮件收到未检查超过3个月的动物的警报。
示例:我在2011年1月1日创建了动物'A'的 followUp 记录,然后在大约01/04/2011,用户收到一封电子邮件警告他去检查动物B的后续行动。

The user must each 3 months create a new record in {followUp} and mark it as checked and supply the date of his action which is "checkDate". But since the user is so lazy, he does that for only few Animals. So he actually wants to be alerted by email for Animals that have not been checked for more than 3 months. Example: I create a followUp record for Animal 'A' on 01/01/2011, then on approximatively 01/04/2011, the user receives an Email alerting him to go check Animal B followup.

网络应用程序在本地企业网络上运行。

The web application is running on Local enterprise Network.

全部我知道的是那个片段:

All I Know is that snippet :

import javax.mail.*;
import javax.mail.internet.*;

import java.util.Properties;

class SimpleMail {
    public static void main(String[] args) throws Exception{
      Properties props = new Properties();
      props.setProperty("mail.transport.protocol", "smtp");
      props.setProperty("mail.host", "mymail.server.org");
      props.setProperty("mail.user", "emailuser");
      props.setProperty("mail.password", "");

      Session mailSession = Session.getDefaultInstance(props, null);
      Transport transport = mailSession.getTransport();

      MimeMessage message = new MimeMessage(mailSession);
      message.setSubject("Testing javamail plain");
      message.setContent("This is a test", "text/plain");
      message.addRecipient(Message.RecipientType.TO,
           new InternetAddress("elvis@presley.org"));

      transport.connect();
      transport.sendMessage(message,
          message.getRecipients(Message.RecipientType.TO));
      transport.close();
    }
}

我应该创建一个Servlet过滤器,一个监听器,为此目的,应用程序Scoped支持bean?在 followUp 上循环的查询记录并返回最后一条记录的checkDate并将其与今日日期进行比较?

Should I create, a Servlet Filter, A Listener, An Application Scoped backing beans, for that purpose? A query that loops on followUp records and returns the last record's checkDate and compares it to Today date?

任何帮助会做。问候。

推荐答案

根据您的问题历史,我知道您正在使用Glassfish 3(带有EJB 3.1的Java EE 6),所以我建议创建一个 @使用 <的Singleton EJB code> @Schedule 方法,该方法以指定的时间间隔在后台执行,例如每天午夜(00:00:00)。

Based on your question history I know that you're using Glassfish 3 (Java EE 6 with EJB 3.1), so I'd suggest to create a @Singleton EJB with @Schedule method which executes in the background at specified intervals, for example daily at midnight (00:00:00).

@Singleton
public class MailReminder {

    @Schedule(hour="0", minute="0", second="0", persistent=false)
    public void run() {
        // Do your check and mail job here.
    }

}

就是这样。无需进一步配置。出于测试目的,您可以使用

That's it. No further configuration is necessary. For testing purposes you could use

    @Schedule(hour="*", minute="*/1", second="0", persistent=false)

让它每分钟运行一次。

  • Java EE 6 tutorial - Using timer service

这篇关于Java邮件如何在条件下自动发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆