Class MailServiceImpl
- java.lang.Object
-
- com.axelor.mail.service.MailServiceImpl
-
- All Implemented Interfaces:
MailConstants
,MailService
@Singleton public class MailServiceImpl extends Object implements MailService, MailConstants
DefaultMailService
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 Constructor Description MailServiceImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
fetch()
Fetch mails from remote mail server.protected void
fetch(MailReader reader)
Fetch email messages from the given mail store.List<javax.mail.internet.InternetAddress>
findEmails(String matching, List<String> selected, int maxResult)
Search for email addresses matching the given text.protected Model
findEntity(MailMessage message)
Find related entity.protected MailReader
getMailReader()
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(javax.mail.internet.MimeMessage email)
This method is called when a new email message received.protected void
messageSent(javax.mail.internet.MimeMessage email)
This method is called when email is sent successfully.protected Set<String>
recipients(MailMessage message, Model entity)
Get the list of recipient email addresses.Model
resolve(String email)
Resolve the given email address to an associated entity.void
send(MailMessage message)
Send a mail for the givenMailMessage
.protected void
send(MailSender sender, javax.mail.internet.MimeMessage email)
protected String
template(MailMessage message, Model entity)
Apply a template to prepare message content.
-
-
-
Method Detail
-
getMailSender
protected MailSender getMailSender(MailMessage message, Model entity)
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
protected MailReader 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
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 requiredentity
- 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 sendentity
- 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 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
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 givenMailMessage
.- Specified by:
send
in interfaceMailService
- Parameters:
message
- the message to send- Throws:
MailException
- on failure
-
send
protected void send(MailSender sender, javax.mail.internet.MimeMessage email) throws Exception
- Throws:
Exception
-
messageSent
protected void messageSent(javax.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(javax.mail.internet.MimeMessage email) throws javax.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:
javax.mail.MessagingException
- if there is any error accessing the given messageIOException
- if there is any error accessing message content
-
fetch
protected void fetch(MailReader reader) throws javax.mail.MessagingException, IOException
Fetch email messages from the given mail store.- Parameters:
reader
- the mail reader to fetch message from- Throws:
javax.mail.MessagingException
- if unable to parse messageIOException
- 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 interfaceMailService
- 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
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<javax.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
-
-