Search in sources :

Example 1 with MailReader

use of com.axelor.mail.MailReader in project axelor-open-suite by axelor.

the class MailServiceMessageImpl method fetch.

@Override
public void fetch() throws MailException {
    final EmailAccount emailAccount = mailAccountService.getDefaultReader();
    if (emailAccount == null) {
        super.fetch();
    } else {
        final MailReader reader = getMailReader(emailAccount);
        final AuditableRunner runner = Beans.get(AuditableRunner.class);
        runner.run(() -> {
            try {
                fetch(reader);
            } catch (Exception e) {
                log.error("Unable to fetch messages", e);
            }
        });
    }
}
Also used : MailReader(com.axelor.mail.MailReader) EmailAccount(com.axelor.apps.message.db.EmailAccount) AuditableRunner(com.axelor.auth.AuditableRunner) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) MailException(com.axelor.mail.MailException)

Example 2 with MailReader

use of com.axelor.mail.MailReader in project axelor-open-suite by axelor.

the class MailServiceMessageImpl method getMailReader.

private MailReader getMailReader(EmailAccount emailAccount) {
    if (readerAccount == null || !readerAccount.getId().equals(emailAccount.getId()) || !readerAccount.getVersion().equals(emailAccount.getVersion())) {
        readerAccount = emailAccount;
        reader = null;
    }
    if (reader == null) {
        MailAccount mailAccount = mailAccountService.getMailAccount(emailAccount);
        reader = new MailReader(mailAccount);
    }
    return reader;
}
Also used : MailReader(com.axelor.mail.MailReader) MailAccount(com.axelor.mail.MailAccount)

Example 3 with MailReader

use of com.axelor.mail.MailReader in project axelor-open-suite by axelor.

the class MailAccountServiceImpl method fetchEmails.

@Override
public int fetchEmails(EmailAccount mailAccount, boolean unseenOnly) throws MessagingException, IOException {
    if (mailAccount == null) {
        return 0;
    }
    log.debug("Fetching emails from host: {}, port: {}, login: {} ", mailAccount.getHost(), mailAccount.getPort(), mailAccount.getLogin());
    com.axelor.mail.MailAccount account = null;
    if (mailAccount.getServerTypeSelect().equals(EmailAccountRepository.SERVER_TYPE_IMAP)) {
        account = new ImapAccount(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), mailAccount.getPassword(), getSecurity(mailAccount));
    } else {
        account = new Pop3Account(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), mailAccount.getPassword(), getSecurity(mailAccount));
    }
    MailReader reader = new MailReader(account);
    final Store store = reader.getStore();
    final Folder inbox = store.getFolder("INBOX");
    // open as READ_WRITE to mark messages as seen
    inbox.open(Folder.READ_WRITE);
    // find all unseen messages
    final FetchProfile profile = new FetchProfile();
    javax.mail.Message[] messages;
    if (unseenOnly) {
        final FlagTerm unseen = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        messages = inbox.search(unseen);
    } else {
        messages = inbox.getMessages();
    }
    profile.add(FetchProfile.Item.ENVELOPE);
    // actually fetch the messages
    inbox.fetch(messages, profile);
    log.debug("Total emails unseen: {}", messages.length);
    int count = 0;
    for (javax.mail.Message message : messages) {
        if (message instanceof MimeMessage) {
            MailParser parser = new MailParser((MimeMessage) message);
            parser.parse();
            createMessage(mailAccount, parser, message.getSentDate());
            count++;
        }
    }
    log.debug("Total emails fetched: {}", count);
    return count;
}
Also used : FetchProfile(javax.mail.FetchProfile) ImapAccount(com.axelor.mail.ImapAccount) Pop3Account(com.axelor.mail.Pop3Account) Message(com.axelor.apps.message.db.Message) MimeMessage(javax.mail.internet.MimeMessage) IExceptionMessage(com.axelor.apps.message.exception.IExceptionMessage) MailParser(com.axelor.mail.MailParser) Store(javax.mail.Store) Flags(javax.mail.Flags) Folder(javax.mail.Folder) MailReader(com.axelor.mail.MailReader) FlagTerm(javax.mail.search.FlagTerm) MimeMessage(javax.mail.internet.MimeMessage)

Aggregations

MailReader (com.axelor.mail.MailReader)3 EmailAccount (com.axelor.apps.message.db.EmailAccount)1 Message (com.axelor.apps.message.db.Message)1 IExceptionMessage (com.axelor.apps.message.exception.IExceptionMessage)1 AuditableRunner (com.axelor.auth.AuditableRunner)1 ImapAccount (com.axelor.mail.ImapAccount)1 MailAccount (com.axelor.mail.MailAccount)1 MailException (com.axelor.mail.MailException)1 MailParser (com.axelor.mail.MailParser)1 Pop3Account (com.axelor.mail.Pop3Account)1 IOException (java.io.IOException)1 FetchProfile (javax.mail.FetchProfile)1 Flags (javax.mail.Flags)1 Folder (javax.mail.Folder)1 MessagingException (javax.mail.MessagingException)1 Store (javax.mail.Store)1 AddressException (javax.mail.internet.AddressException)1 MimeMessage (javax.mail.internet.MimeMessage)1 FlagTerm (javax.mail.search.FlagTerm)1