Class MailServiceImpl
- All Implemented Interfaces:
MailConstants
,MailService
MailService
implementation.-
Field Summary
Fields inherited from interface com.axelor.mail.MailConstants
CHANNEL_SSL, CHANNEL_STARTTLS, DEFAULT_TIMEOUT, MESSAGE_TYPE_COMMENT, MESSAGE_TYPE_EMAIL, MESSAGE_TYPE_NOTIFICATION, PROTOCOL_IMAP, PROTOCOL_IMAPS, PROTOCOL_POP3
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
fetch()
Fetch mails from remote mail server.protected void
fetch
(MailReader reader) Fetch email messages from the given mail store.List
<jakarta.mail.internet.InternetAddress> findEmails
(String matching, List<String> selected, int maxResult) Search for email addresses matching the given text.protected final Model
findEntity
(MailMessage message) Find related entity.protected MailReader
GetMailReader
to use sending the given message.protected MailSender
getMailSender
(MailMessage message, Model entity) GetMailSender
to use sending the given message.protected String
getSubject
(MailMessage message, Model entity) Get the subject line for the message.protected MailMessage
messageReceived
(jakarta.mail.internet.MimeMessage email) This method is called when a new email message received.protected void
messageSent
(jakarta.mail.internet.MimeMessage email) This method is called when email is sent successfully.recipients
(MailMessage message, Model entity) Get the list of recipient email addresses.Resolve the given email address to an associated entity.void
send
(MailMessage message) Send a mail for the givenMailMessage
.protected void
send
(MailSender sender, jakarta.mail.internet.MimeMessage email) protected String
template
(MailMessage message, Model entity) Apply a template to prepare message content.
-
Constructor Details
-
MailServiceImpl
public MailServiceImpl()
-
-
Method Details
-
getMailSender
GetMailSender
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 senderentity
- the related entity, can be null if there is no related record- Returns:
- a
MailSender
, null if not configured
-
getMailReader
GetMailReader
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
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 requiredentity
- the related entity, can be null if there is no related record- Returns:
- subject line
-
recipients
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 sendentity
- the related entity, can be null if there is no related record- Returns:
- set of email addresses
-
template
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 sendentity
- 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
Find related entity.- Parameters:
message
- the message- Returns:
- related entity or null if there is no related record
-
send
Description copied from interface:MailService
Send a mail for the givenMailMessage
.- Specified by:
send
in interfaceMailService
- Parameters:
message
- the message to send- Throws:
MailException
- on failure
-
send
- 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 ofMailMessage
, thefetch(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 messageIOException
- if there is any error accessing message content
-
fetch
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 messageIOException
- if unable to load message content
-
fetch
Description copied from interface:MailService
Fetch mails from remote mail server.- Specified by:
fetch
in interfaceMailService
- Throws:
MailException
- on failure
-
resolve
Description copied from interface:MailService
Resolve the given email address to an associated entity.Generally, it should resolve to the
User
,Contact
,Partner
orCustomer
that represents a contact.The default implementation resolves to the
User
records.- Specified by:
resolve
in interfaceMailService
- 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 interfaceMailService
- Parameters:
matching
- the match textselected
- already selected email addressesmaxResult
- maximum number of items to return- Returns:
- list of
InternetAddress
-