@Component(value="cuba_Emailer") public class Emailer extends java.lang.Object implements EmailerAPI
Modifier and Type | Class and Description |
---|---|
protected static class |
Emailer.EmailSendTask |
protected static class |
Emailer.MessagePersistingContext |
Modifier and Type | Field and Description |
---|---|
protected Authentication |
authentication |
protected static java.lang.String |
BODY_FILE_EXTENSION |
protected java.util.concurrent.atomic.AtomicInteger |
callCount |
protected EmailerConfig |
config |
protected EmailSenderAPI |
emailSender |
protected FileStorageAPI |
fileStorage |
protected org.springframework.core.task.TaskExecutor |
mailSendTaskExecutor |
protected Metadata |
metadata |
protected Persistence |
persistence |
protected Resources |
resources |
protected TimeSource |
timeSource |
protected UserSessionSource |
userSessionSource |
NAME
Constructor and Description |
---|
Emailer() |
Modifier and Type | Method and Description |
---|---|
protected boolean |
applicationNotStartedYet() |
protected java.lang.String |
bodyTextFromByteArray(byte[] bodyContent) |
protected byte[] |
bodyTextToBytes(SendingMessage message) |
protected SendingMessage |
convertToSendingMessage(java.lang.String address,
java.lang.String from,
java.lang.String cc,
java.lang.String bcc,
java.lang.String caption,
java.lang.String body,
java.lang.String bodyContentType,
java.util.List<EmailHeader> headers,
EmailAttachment[] attachments,
java.lang.Integer attemptsCount,
java.util.Date deadline) |
protected FileDescriptor |
createAttachmentFileDescriptor(SendingAttachment attachment) |
protected FileDescriptor |
createBodyFileDescriptor(SendingMessage message,
byte[] bodyBytes) |
protected SendingMessage |
createClone(SendingMessage srcMessage) |
protected java.lang.String |
getContentBodyType(SendingMessage sendingMessage) |
protected java.lang.String |
getEmailerLogin() |
protected boolean |
isNeedToRetry(java.lang.Exception e) |
protected void |
loadBodyAndAttachments(SendingMessage message) |
java.lang.String |
loadContentText(SendingMessage sendingMessage)
Loads content text for given message.
|
protected java.util.List<SendingMessage> |
loadEmailsToSend() |
protected void |
markAsNonSent(SendingMessage sendingMessage) |
protected void |
markAsSent(SendingMessage sendingMessage) |
protected void |
migrateAttachment(EntityManager em,
SendingAttachment attachment) |
void |
migrateAttachmentsToFileStorage(java.util.List<SendingAttachment> attachments)
Migrate list of existing email attachments to be stored in file storage, in a single transaction.
|
void |
migrateEmailsToFileStorage(java.util.List<SendingMessage> messages)
Migrate list of existing messages to be stored in file storage, in a single transaction.
|
protected void |
migrateMessage(EntityManager em,
SendingMessage msg) |
protected void |
persistAndSendEmail(EmailInfo emailInfo) |
protected SendingMessage |
persistMessageIfPossible(SendingMessage sendingMessage) |
protected void |
persistMessages(java.util.List<SendingMessage> sendingMessageList,
SendingStatus status) |
protected void |
persistSendingMessage(EntityManager em,
SendingMessage message,
Emailer.MessagePersistingContext context) |
protected void |
prepareEmailInfo(EmailInfo emailInfo) |
protected void |
processBodyTemplate(EmailInfo info) |
java.lang.String |
processQueuedEmails()
Send emails added to the queue.
|
protected void |
removeOrphanFiles(Emailer.MessagePersistingContext context) |
protected void |
replaceRecipientIfNecessary(SendingMessage msg) |
protected void |
returnToQueue(SendingMessage sendingMessage) |
void |
sendEmail(EmailInfo info)
Send email synchronously.
|
void |
sendEmail(java.lang.String addresses,
java.lang.String caption,
java.lang.String body,
EmailAttachment... attachments)
|
void |
sendEmail(java.lang.String addresses,
java.lang.String caption,
java.lang.String body,
java.lang.String bodyContentType,
EmailAttachment... attachments)
Send email synchronously.
|
java.util.List<SendingMessage> |
sendEmailAsync(EmailInfo info)
Send email asynchronously.
|
java.util.List<SendingMessage> |
sendEmailAsync(EmailInfo info,
java.lang.Integer attemptsCount,
java.util.Date deadline)
Send email asynchronously, with limited number of attempts.
|
protected java.lang.String |
sendQueuedEmails() |
protected void |
sendSendingMessage(SendingMessage sendingMessage) |
void |
setConfig(Configuration configuration) |
protected boolean |
shouldMarkNotSent(SendingMessage sendingMessage) |
protected java.util.List<SendingMessage> |
splitEmail(EmailInfo info,
java.lang.Integer attemptsCount,
java.util.Date deadline) |
protected void |
submitExecutorTask(SendingMessage msg) |
protected SendingAttachment |
toSendingAttachment(EmailAttachment ea) |
void |
updateSession()
Updates
Properties for JavaMail Session |
protected static final java.lang.String BODY_FILE_EXTENSION
protected EmailerConfig config
protected java.util.concurrent.atomic.AtomicInteger callCount
protected org.springframework.core.task.TaskExecutor mailSendTaskExecutor
@Inject protected UserSessionSource userSessionSource
@Inject protected TimeSource timeSource
@Inject protected Persistence persistence
@Inject protected Metadata metadata
@Inject protected Authentication authentication
@Inject protected EmailSenderAPI emailSender
@Inject protected Resources resources
@Inject protected FileStorageAPI fileStorage
@Inject public void setConfig(Configuration configuration)
protected java.lang.String getEmailerLogin()
public void sendEmail(java.lang.String addresses, java.lang.String caption, java.lang.String body, EmailAttachment... attachments) throws EmailException
EmailerAPI
EmailerAPI.sendEmail(String, String, String, String, EmailAttachment...)
instead.
Send email synchronously.
sendEmail
in interface EmailerAPI
addresses
- comma or semicolon separated list of addressescaption
- email subjectbody
- email bodyattachments
- email attachmentsEmailException
- in case of any errorspublic void sendEmail(java.lang.String addresses, java.lang.String caption, java.lang.String body, java.lang.String bodyContentType, EmailAttachment... attachments) throws EmailException
EmailerAPI
sendEmail
in interface EmailerAPI
addresses
- comma or semicolon separated list of addressescaption
- email subjectbody
- email bodybodyContentType
- email body like "text/plain; charset=UTF-8" or "text/html; charset=UTF-8", etcattachments
- email attachmentsEmailException
- in case of any errorspublic void sendEmail(EmailInfo info) throws EmailException
EmailerAPI
sendEmail
in interface EmailerAPI
info
- email detailsEmailException
- in case of any errorspublic java.util.List<SendingMessage> sendEmailAsync(EmailInfo info)
EmailerAPI
This method creates a list of SendingMessage
instances, saves it to the database and returns immediately.
The actual sending is performed by the EmailerAPI.processQueuedEmails()
method which should be invoked by a
scheduled task.
sendEmailAsync
in interface EmailerAPI
info
- email detailsSendingMessage
spublic java.util.List<SendingMessage> sendEmailAsync(EmailInfo info, java.lang.Integer attemptsCount, java.util.Date deadline)
EmailerAPI
sendEmailAsync
in interface EmailerAPI
info
- email detailsattemptsCount
- count of attempts to send (1 attempt per scheduler tick). If not specified,
EmailerConfig.getDefaultSendingAttemptsCount()
is useddeadline
- Emailer tries to send message till deadline.
If deadline has come and message has not been sent, status of this message is changed to
SendingStatus.NOTSENT
SendingMessage
sprotected void prepareEmailInfo(EmailInfo emailInfo)
protected void processBodyTemplate(EmailInfo info)
protected java.util.List<SendingMessage> splitEmail(EmailInfo info, @Nullable java.lang.Integer attemptsCount, @Nullable java.util.Date deadline)
protected void sendSendingMessage(SendingMessage sendingMessage)
protected void persistAndSendEmail(EmailInfo emailInfo) throws EmailException
EmailException
@Nullable protected SendingMessage persistMessageIfPossible(SendingMessage sendingMessage)
protected SendingMessage createClone(SendingMessage srcMessage)
public java.lang.String processQueuedEmails()
EmailerAPI
This method should be called periodically from a scheduled task.
processQueuedEmails
in interface EmailerAPI
protected boolean applicationNotStartedYet()
protected java.lang.String sendQueuedEmails()
protected boolean shouldMarkNotSent(SendingMessage sendingMessage)
protected void submitExecutorTask(SendingMessage msg)
protected java.util.List<SendingMessage> loadEmailsToSend()
public java.lang.String loadContentText(SendingMessage sendingMessage)
EmailerAPI
loadContentText
in interface EmailerAPI
public void updateSession()
EmailerAPI
Properties
for JavaMail Session
updateSession
in interface EmailerAPI
protected void loadBodyAndAttachments(SendingMessage message)
protected void persistMessages(java.util.List<SendingMessage> sendingMessageList, SendingStatus status)
protected void removeOrphanFiles(Emailer.MessagePersistingContext context)
protected void persistSendingMessage(EntityManager em, SendingMessage message, Emailer.MessagePersistingContext context) throws FileStorageException
FileStorageException
protected FileDescriptor createAttachmentFileDescriptor(SendingAttachment attachment)
protected FileDescriptor createBodyFileDescriptor(SendingMessage message, byte[] bodyBytes)
protected void returnToQueue(SendingMessage sendingMessage)
protected void markAsSent(SendingMessage sendingMessage)
protected void markAsNonSent(SendingMessage sendingMessage)
protected SendingMessage convertToSendingMessage(java.lang.String address, java.lang.String from, java.lang.String cc, java.lang.String bcc, java.lang.String caption, java.lang.String body, java.lang.String bodyContentType, @Nullable java.util.List<EmailHeader> headers, @Nullable EmailAttachment[] attachments, @Nullable java.lang.Integer attemptsCount, @Nullable java.util.Date deadline)
protected java.lang.String getContentBodyType(SendingMessage sendingMessage)
protected void replaceRecipientIfNecessary(SendingMessage msg)
protected SendingAttachment toSendingAttachment(EmailAttachment ea)
protected byte[] bodyTextToBytes(SendingMessage message)
protected java.lang.String bodyTextFromByteArray(byte[] bodyContent)
protected boolean isNeedToRetry(java.lang.Exception e)
public void migrateEmailsToFileStorage(java.util.List<SendingMessage> messages)
EmailerAPI
migrateEmailsToFileStorage
in interface EmailerAPI
public void migrateAttachmentsToFileStorage(java.util.List<SendingAttachment> attachments)
EmailerAPI
migrateAttachmentsToFileStorage
in interface EmailerAPI
protected void migrateMessage(EntityManager em, SendingMessage msg)
protected void migrateAttachment(EntityManager em, SendingAttachment attachment)