Class AbstractMailReceiver
java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.mail.AbstractMailReceiver
- All Implemented Interfaces:
Aware
,BeanFactoryAware
,BeanNameAware
,DisposableBean
,InitializingBean
,ApplicationContextAware
,ExpressionCapable
,MailReceiver
,NamedComponent
- Direct Known Subclasses:
ImapMailReceiver
,Pop3MailReceiver
public abstract class AbstractMailReceiver extends IntegrationObjectSupport implements MailReceiver, DisposableBean
Base class for
MailReceiver
implementations.- Author:
- Arjen Poutsma, Jonas Partner, Mark Fisher, Iwein Fuld, Oleg Zhurakousky, Gary Russell, Artem Bilan
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_SI_USER_FLAG
Default user flag for marking messages as seen by this receiver: "spring-integration-mail-adapter".Fields inherited from class org.springframework.integration.context.IntegrationObjectSupport
EXPRESSION_PARSER, logger
-
Constructor Summary
Constructors Constructor Description AbstractMailReceiver()
AbstractMailReceiver(String url)
AbstractMailReceiver(javax.mail.URLName urlName)
-
Method Summary
Modifier and Type Method Description protected void
closeFolder()
protected void
deleteMessages(javax.mail.Message[] messages)
Deletes the given messages from this receiver's folder.void
destroy()
protected void
fetchMessages(javax.mail.Message[] messages)
Fetches the specified messages from this receiver's folder.protected javax.mail.Folder
getFolder()
protected int
getFolderOpenMode()
protected Properties
getJavaMailProperties()
protected String
getUserFlag()
protected void
onInit()
Subclasses may implement this for initialization logic.protected void
openFolder()
Object[]
receive()
protected abstract javax.mail.Message[]
searchForNewMessages()
Subclasses must implement this method to return new mail messages.protected void
setAdditionalFlags(javax.mail.Message message)
Optional method allowing you to set additional flags.void
setAutoCloseFolder(boolean autoCloseFolder)
Configure aboolean
flag to close the folder automatically after a fetch (default) or populate an additionalIntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE
message header instead.void
setEmbeddedPartsAsBytes(boolean embeddedPartsAsBytes)
When a header mapper is provided determine whether an embeddedPart
(e.gMessage
orMultipart
content is rendered as a byte[] in the payload.void
setHeaderMapper(HeaderMapper<javax.mail.internet.MimeMessage> headerMapper)
Set the header mapper; if a header mapper is not provided, the message payload is aMimeMessage
, when provided, the headers are mapped and the payload is theMimeMessage
content.void
setJavaMailAuthenticator(javax.mail.Authenticator javaMailAuthenticator)
Optional, sets the Authenticator to be used to obtain a session.void
setJavaMailProperties(Properties javaMailProperties)
A newSession
will be created with these properties (and the JavaMailAuthenticator if provided).void
setMaxFetchSize(int maxFetchSize)
Specify the maximum number of Messages to fetch per call toreceive()
.void
setProtocol(String protocol)
void
setSelectorExpression(Expression selectorExpression)
void
setSession(javax.mail.Session session)
Set theSession
.void
setShouldDeleteMessages(boolean shouldDeleteMessages)
Specify whether mail messages should be deleted after retrieval.void
setSimpleContent(boolean simpleContent)
MimeMessage.getContent()
returns just the email body.void
setUserFlag(String userFlag)
Set the name of the flag to use to flag messages when the server does not support \Recent but supports user flags; default "spring-integration-mail-adapter".protected boolean
shouldDeleteMessages()
Indicates whether the mail messages should be deleted after being received.String
toString()
Methods inherited from class org.springframework.integration.context.IntegrationObjectSupport
afterPropertiesSet, extractTypeIfPossible, generateId, getApplicationContext, getApplicationContextId, getBeanDescription, getBeanFactory, getBeanName, getChannelResolver, getComponentName, getComponentType, getConversionService, getExpression, getIntegrationProperties, getIntegrationProperty, getMessageBuilderFactory, getTaskScheduler, isInitialized, setApplicationContext, setBeanFactory, setBeanName, setChannelResolver, setComponentName, setConversionService, setMessageBuilderFactory, setPrimaryExpression, setTaskScheduler
-
Field Details
-
DEFAULT_SI_USER_FLAG
Default user flag for marking messages as seen by this receiver: "spring-integration-mail-adapter".- See Also:
- Constant Field Values
-
-
Constructor Details
-
AbstractMailReceiver
public AbstractMailReceiver() -
AbstractMailReceiver
public AbstractMailReceiver(javax.mail.URLName urlName) -
AbstractMailReceiver
-
-
Method Details
-
setSelectorExpression
-
setProtocol
-
setSession
public void setSession(javax.mail.Session session)Set theSession
. Otherwise, the Session will be created by invocation ofSession.getInstance(Properties)
orSession.getInstance(Properties, Authenticator)
.- Parameters:
session
- The session.- See Also:
setJavaMailProperties(Properties)
,setJavaMailAuthenticator(Authenticator)
-
setJavaMailProperties
A newSession
will be created with these properties (and the JavaMailAuthenticator if provided). Use either this method orsetSession(javax.mail.Session)
, but not both.- Parameters:
javaMailProperties
- The javamail properties.- See Also:
setJavaMailAuthenticator(Authenticator)
,setSession(Session)
-
getJavaMailProperties
-
setJavaMailAuthenticator
public void setJavaMailAuthenticator(javax.mail.Authenticator javaMailAuthenticator)Optional, sets the Authenticator to be used to obtain a session. This will not be used ifsetSession(javax.mail.Session)
has been used to configure theSession
directly.- Parameters:
javaMailAuthenticator
- The javamail authenticator.- See Also:
setSession(Session)
-
setMaxFetchSize
public void setMaxFetchSize(int maxFetchSize)Specify the maximum number of Messages to fetch per call toreceive()
.- Parameters:
maxFetchSize
- The max fetch size.
-
setShouldDeleteMessages
public void setShouldDeleteMessages(boolean shouldDeleteMessages)Specify whether mail messages should be deleted after retrieval.- Parameters:
shouldDeleteMessages
- true to delete messages.
-
shouldDeleteMessages
protected boolean shouldDeleteMessages()Indicates whether the mail messages should be deleted after being received.- Returns:
- true when messages will be deleted.
-
getUserFlag
-
setUserFlag
Set the name of the flag to use to flag messages when the server does not support \Recent but supports user flags; default "spring-integration-mail-adapter".- Parameters:
userFlag
- the flag.- Since:
- 4.2.2
-
setHeaderMapper
Set the header mapper; if a header mapper is not provided, the message payload is aMimeMessage
, when provided, the headers are mapped and the payload is theMimeMessage
content.- Parameters:
headerMapper
- the header mapper.- Since:
- 4.3
- See Also:
setEmbeddedPartsAsBytes(boolean)
-
setEmbeddedPartsAsBytes
public void setEmbeddedPartsAsBytes(boolean embeddedPartsAsBytes)When a header mapper is provided determine whether an embeddedPart
(e.gMessage
orMultipart
content is rendered as a byte[] in the payload. Otherwise, leave as aPart
. These objects are not suitable for downstream serialization. Default: true.This has no effect if there is no header mapper, in that case the payload is the
MimeMessage
.- Parameters:
embeddedPartsAsBytes
- the embeddedPartsAsBytes to set.- Since:
- 4.3
- See Also:
setHeaderMapper(HeaderMapper)
-
setSimpleContent
public void setSimpleContent(boolean simpleContent)MimeMessage.getContent()
returns just the email body.foo
Some subclasses, such asIMAPMessage
return some headers with the body.To: foo@bar From: bar@baz Subject: Test Email foo
Starting with version 5.0, messages emitted by mail receivers will render the content in the same way as theMimeMessage
implementation returned by javamail. In versions 2.2 through 4.3, the content was always just the body, regardless of the underlying message type (unless a header mapper was provided, in which case the payload was rendered by the underlyingMimeMessage
.To revert to the previous behavior, set this flag to true. In addition, even if a header mapper is provided, the payload will just be the email body.
- Parameters:
simpleContent
- true to render simple content.- Since:
- 5.0
-
setAutoCloseFolder
public void setAutoCloseFolder(boolean autoCloseFolder)Configure aboolean
flag to close the folder automatically after a fetch (default) or populate an additionalIntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE
message header instead. It is the downstream flow's responsibility to obtain this header and call itsclose()
whenever it is necessary.Keeping the folder open is useful in cases where communication with the server is needed when parsing multipart content of the email with attachments.
The
setSimpleContent(boolean)
andsetHeaderMapper(HeaderMapper)
options are not affected by this flag.- Parameters:
autoCloseFolder
-false
do not close the folder automatically after a fetch.- Since:
- 5.2
-
getFolder
protected javax.mail.Folder getFolder() -
getFolderOpenMode
protected int getFolderOpenMode() -
searchForNewMessages
protected abstract javax.mail.Message[] searchForNewMessages() throws javax.mail.MessagingExceptionSubclasses must implement this method to return new mail messages.- Returns:
- An array of messages.
- Throws:
javax.mail.MessagingException
- Any MessagingException.
-
openFolder
protected void openFolder() throws javax.mail.MessagingException- Throws:
javax.mail.MessagingException
-
receive
- Specified by:
receive
in interfaceMailReceiver
- Throws:
javax.mail.MessagingException
-
closeFolder
protected void closeFolder() -
fetchMessages
protected void fetchMessages(javax.mail.Message[] messages) throws javax.mail.MessagingExceptionFetches the specified messages from this receiver's folder. Default implementationfetches
everyFetchProfile.Item
.- Parameters:
messages
- the messages to fetch- Throws:
javax.mail.MessagingException
- in case of JavaMail errors
-
deleteMessages
protected void deleteMessages(javax.mail.Message[] messages) throws javax.mail.MessagingExceptionDeletes the given messages from this receiver's folder.- Parameters:
messages
- the messages to delete- Throws:
javax.mail.MessagingException
- in case of JavaMail errors
-
setAdditionalFlags
protected void setAdditionalFlags(javax.mail.Message message) throws javax.mail.MessagingExceptionOptional method allowing you to set additional flags. Currently only implemented in IMapMailReceiver.- Parameters:
message
- The message.- Throws:
javax.mail.MessagingException
- A MessagingException.
-
destroy
public void destroy()- Specified by:
destroy
in interfaceDisposableBean
-
onInit
protected void onInit()Description copied from class:IntegrationObjectSupport
Subclasses may implement this for initialization logic.- Overrides:
onInit
in classIntegrationObjectSupport
-
toString
- Overrides:
toString
in classIntegrationObjectSupport
-