java - 消息驱动 bean,不根据轮询间隔获取新电子邮件

标签 java email ejb jboss-mdb inbound

我尝试使用消息驱动 bean 获取电子邮件。我的目的是捕获新电子邮件。我用过Jboss 6.1。我在 Gmail 中启用了 pop3。当我启动服务器时,能够从收件箱中获取现有电子邮件。

一旦服务器启动且应用程序运行,MailListener 就不会通知新电子邮件。我也尝试添加轮询间隔。

任何建议。

package uk.co.test.inbound;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.inject.Named;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Message.RecipientType;

import org.jboss.ejb3.annotation.ResourceAdapter;
import org.jboss.resource.adapter.mail.inflow.MailListener;


@MessageDriven(
            activationConfig = { 
            @ActivationConfigProperty(propertyName = "mailServer", propertyValue = "pop.gmail.com"),
            @ActivationConfigProperty(propertyName = "mailFolder", propertyValue = "INBOX"),
            @ActivationConfigProperty(propertyName = "storeProtocol", propertyValue = "pop3s"),
            @ActivationConfigProperty(propertyName = "userName", propertyValue = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94ececececd4f3f9f5fdf8baf7fbf9" rel="noreferrer noopener nofollow">[email protected]</a>"),
            @ActivationConfigProperty(propertyName= "password", propertyValue="xxxxx"),
            @ActivationConfigProperty(propertyName= "pollingInterval", propertyValue="100"),
            @ActivationConfigProperty(propertyName= "port", propertyValue="995"),
            @ActivationConfigProperty(propertyName= "debug", propertyValue="true"),
            @ActivationConfigProperty(propertyName= "starttls", propertyValue="true")
            })
@ResourceAdapter("mail-ra.rar")
@Named("mailListener")
public class EmailReceiver implements MailListener{

    @Override
    public void onMessage(Message msg) {
        System.out.println("One new Message Received" );
        try {

            Address[] fromAddress = msg.getFrom();
            String from = fromAddress[0].toString();
            String subject = msg.getSubject();
            String toList = parseAddresses(msg.getRecipients(RecipientType.TO));
            String ccList = parseAddresses(msg.getRecipients(RecipientType.CC));
            String sentDate = msg.getSentDate().toString();

            String contentType = msg.getContentType();
            String messageContent = "";

            if (contentType.contains("text/plain") || contentType.contains("text/html")) {
                try {
                    Object content = msg.getContent();
                    if (content != null) {
                        messageContent = content.toString();
                    }
                } catch (Exception ex) {
                    messageContent = "[Error downloading content]";
                    ex.printStackTrace();
                }
            } else { 
                try {
                    if (msg.getContent() instanceof Multipart) {
                        Multipart content = (Multipart) msg.getContent();
                        content.getCount();
                        Part part = content.getBodyPart(0);
                        InputStream is = part.getInputStream();
                        if (!(is instanceof BufferedInputStream)) {
                            is = new BufferedInputStream(is);
                        }
                        int c;
                        final StringWriter sw = new StringWriter();
                        while ((c = is.read()) != -1) {
                            sw.write(c);
                        }

                        if (!sw.toString().contains("<div>")) {
                            messageContent = sw.toString();
                        }
                    }
                } catch (IOException e) {
                    messageContent = "[Error downloading content]";
                    e.printStackTrace();
                }
            }


            System.out.println("\t Subject: " + subject);

            System.out.println("\t Message: " + messageContent);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }

    /**
     * Returns a list of addresses in String format separated by comma
     *
     * @param address an array of Address objects
     * @return a string represents a list of addresses
     */
    private String parseAddresses(Address[] address) {
        String listAddress = "";

        if (address != null) {
            for (int i = 0; i < address.length; i++) {
                listAddress += address[i].toString() + ", ";
            }
        }
        if (listAddress.length() > 1) {
            listAddress = listAddress.substring(0, listAddress.length() - 2);
        }

        return listAddress;
    }


}

最佳答案

问题可能与此错误有关:https://issues.redhat.com/browse/JBAS-8635 提供了一个“固定”的mail-ra.jar文件(不知道是否可信)。他们在评论中表示,他们只在现有的 mail-ra.jar 中添加了“setReleased”。有人测试了它并确认它有效。

工单最后评论中还有一个手动实现示例。以这种方式实现它会给你更多的控制权。

关于java - 消息驱动 bean,不根据轮询间隔获取新电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58913591/

相关文章:

ios - 如何获取特定标签的 gmail 邮件,例如仅获取聊天邮件

java - 有状态 Bean 的行为与无状态 Bean 类似

javax.swing.text.html.HTML 文档按名称获取元素

java - 为什么大多数枚举值在声明中以 BUTT 说明符结尾?

java - 可以将 Jackson 配置为从所有字符串属性中修剪前导/尾随空格吗?

php - stream_socket_enable_crypto() : Peer certificate CN =`cs723.mojohost.com' did not match expected CN =`smtp.sendgrid.net'

Java静态关键字的使用

django - 如何使用 smtp.EmailBackend 在 Django 中发送电子邮件而不通过邮件服务器进行身份验证

unit-testing - 使用嵌入式容器测试 EJB 3.1 应用程序

ejb - 在 EJB 方法/@RequestScoped 和 @Stateless 之间传递状态