Class MailServiceImpl

java.lang.Object
com.axelor.mail.service.MailServiceImpl
All Implemented Interfaces:
MailConstants, MailService

@Singleton public class MailServiceImpl extends Object implements MailService, MailConstants
Default MailService implementation.
  • Constructor Details

    • MailServiceImpl

      public MailServiceImpl()
  • Method Details

    • getMailSender

      protected MailSender getMailSender(MailMessage message, Model entity)
      Get MailSender to use sending the given message.

      Can be overridden to provide different MailSender for different messages and object depending on the business requirements.

      Parameters:
      message - the message to send with the sender
      entity - the related entity, can be null if there is no related record
      Returns:
      a MailSender, null if not configured
    • getMailReader

      protected MailReader getMailReader()
      Get MailReader to use sending the given message.

      Can be overridden to provide MailReader configured differently (e.g. from database config).

      Returns:
      a MailSender, null if not configured
    • getSubject

      protected String getSubject(MailMessage message, Model entity)
      Get the subject line for the message.

      Custom implementation can overrider this method to prepare good subject line.

      Parameters:
      message - the message for which subject line is required
      entity - the related entity, can be null if there is no related record
      Returns:
      subject line
    • recipients

      protected Set<String> recipients(MailMessage message, Model entity)
      Get the list of recipient email addresses.

      The default implementation returns email addresses of all followers. Custom implementation may include more per business requirement. For example, including customer email on sale order is confirmed.

      Parameters:
      message - the message to send
      entity - the related entity, can be null if there is no related record
      Returns:
      set of email addresses
    • template

      protected String template(MailMessage message, Model entity) throws IOException
      Apply a template to prepare message content.

      The default implementation uses very basic template. Custom implementations can apply different templates depending on message type and related entity or even current customer.

      Parameters:
      message - the message to send
      entity - the related entity, can be null if there is no related record
      Returns:
      final message body text
      Throws:
      IOException - if there is any error applying template
    • findEntity

      protected final Model findEntity(MailMessage message)
      Find related entity.
      Parameters:
      message - the message
      Returns:
      related entity or null if there is no related record
    • send

      public void send(MailMessage message) throws MailException
      Description copied from interface: MailService
      Send a mail for the given MailMessage.
      Specified by:
      send in interface MailService
      Parameters:
      message - the message to send
      Throws:
      MailException - on failure
    • send

      protected void send(MailSender sender, jakarta.mail.internet.MimeMessage email) throws Exception
      Throws:
      Exception
    • messageSent

      protected void messageSent(jakarta.mail.internet.MimeMessage email)
      This method is called when email is sent successfully.

      This method is called by send(MailSender, MimeMessage) which is running under a transaction with super user access.

      Parameters:
      email - the email message sent
    • messageReceived

      protected MailMessage messageReceived(jakarta.mail.internet.MimeMessage email) throws jakarta.mail.MessagingException, IOException
      This method is called when a new email message received.

      This method is called by fetch(MailReader) which is running under a database transaction. If this method returns an instance of MailMessage, the fetch(MailReader) will persist the record.

      The default implementation will parse the message and check if it's a reply to a message sent by the system.

      Parameters:
      email - the incoming email message
      Returns:
      an instance of MailMessage to save in database or null to ignore
      Throws:
      jakarta.mail.MessagingException - if there is any error accessing the given message
      IOException - if there is any error accessing message content
    • fetch

      protected void fetch(MailReader reader) throws jakarta.mail.MessagingException, IOException
      Fetch email messages from the given mail store.
      Parameters:
      reader - the mail reader to fetch message from
      Throws:
      jakarta.mail.MessagingException - if unable to parse message
      IOException - if unable to load message content
    • fetch

      public void fetch() throws MailException
      Description copied from interface: MailService
      Fetch mails from remote mail server.
      Specified by:
      fetch in interface MailService
      Throws:
      MailException - on failure
    • resolve

      public Model resolve(String email)
      Description copied from interface: MailService
      Resolve the given email address to an associated entity.

      Generally, it should resolve to the User, Contact, Partner or Customer that represents a contact.

      The default implementation resolves to the User records.

      Specified by:
      resolve in interface MailService
      Parameters:
      email - the email address to resolve
      Returns:
      associated entity or null if can't be resolved
    • findEmails

      public List<jakarta.mail.internet.InternetAddress> findEmails(String matching, List<String> selected, int maxResult)
      Description copied from interface: MailService
      Search for email addresses matching the given text.
      Specified by:
      findEmails in interface MailService
      Parameters:
      matching - the match text
      selected - already selected email addresses
      maxResult - maximum number of items to return
      Returns:
      list of InternetAddress