use of com.axelor.mail.ImapAccount 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;
}
use of com.axelor.mail.ImapAccount in project axelor-open-suite by axelor.
the class MailAccountServiceImpl method getMailAccount.
@Override
public com.axelor.mail.MailAccount getMailAccount(EmailAccount mailAccount) {
Integer serverType = mailAccount.getServerTypeSelect();
String port = mailAccount.getPort() <= 0 ? null : mailAccount.getPort().toString();
com.axelor.mail.MailAccount account;
if (serverType == EmailAccountRepository.SERVER_TYPE_SMTP) {
account = new SmtpAccount(mailAccount.getHost(), port, mailAccount.getLogin(), getDecryptPassword(mailAccount.getPassword()), getSecurity(mailAccount));
} else if (serverType == EmailAccountRepository.SERVER_TYPE_IMAP) {
account = new ImapAccount(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), getDecryptPassword(mailAccount.getPassword()), getSecurity(mailAccount));
} else {
account = new Pop3Account(mailAccount.getHost(), mailAccount.getPort().toString(), mailAccount.getLogin(), getDecryptPassword(mailAccount.getPassword()), getSecurity(mailAccount));
}
Properties props = account.getSession().getProperties();
if (mailAccount.getFromAddress() != null && !"".equals(mailAccount.getFromAddress())) {
props.setProperty("mail.smtp.from", mailAccount.getFromAddress());
}
if (mailAccount.getFromName() != null && !"".equals(mailAccount.getFromName())) {
props.setProperty("mail.smtp.from.personal", mailAccount.getFromName());
}
account.setConnectionTimeout(CHECK_CONF_TIMEOUT);
return account;
}
Aggregations