Class AbstractMailReceiver

java.lang.Object
org.springframework.integration.context.IntegrationObjectSupport
org.springframework.integration.mail.AbstractMailReceiver
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.integration.context.ExpressionCapable, MailReceiver, org.springframework.integration.support.context.NamedComponent
Direct Known Subclasses:
ImapMailReceiver, Pop3MailReceiver

public abstract class AbstractMailReceiver extends org.springframework.integration.context.IntegrationObjectSupport implements MailReceiver, org.springframework.beans.factory.DisposableBean
Base class for MailReceiver implementations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    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(jakarta.mail.URLName urlName)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
     
    protected void
    deleteMessages(jakarta.mail.Message[] messages)
    Deletes the given messages from this receiver's folder.
    void
     
    protected void
    fetchMessages(jakarta.mail.Message[] messages)
    Fetches the specified messages from this receiver's folder.
    protected jakarta.mail.Folder
     
    protected int
     
    protected Properties
     
    protected String
     
    protected void
     
    protected void
     
     
    protected abstract jakarta.mail.Message[]
    Subclasses must implement this method to return new mail messages.
    protected void
    setAdditionalFlags(jakarta.mail.Message message)
    Optional method allowing you to set additional flags.
    void
    setAutoCloseFolder(boolean autoCloseFolder)
    Configure a boolean flag to close the folder automatically after a fetch (default) or populate an additional IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE message header instead.
    void
    setEmbeddedPartsAsBytes(boolean embeddedPartsAsBytes)
    When a header mapper is provided determine whether an embedded Part (e.g Message or Multipart content is rendered as a byte[] in the payload.
    void
    setHeaderMapper(org.springframework.integration.mapping.HeaderMapper<jakarta.mail.internet.MimeMessage> headerMapper)
    Set the header mapper; if a header mapper is not provided, the message payload is a MimeMessage, when provided, the headers are mapped and the payload is the MimeMessage content.
    void
    setJavaMailAuthenticator(jakarta.mail.Authenticator javaMailAuthenticator)
    Optional, sets the Authenticator to be used to obtain a session.
    void
    setJavaMailProperties(Properties javaMailProperties)
    A new Session 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 to receive().
    void
    setProtocol(String protocol)
     
    void
    setSelectorExpression(org.springframework.expression.Expression selectorExpression)
     
    void
    setSession(jakarta.mail.Session session)
    Set the Session.
    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
    Indicates whether the mail messages should be deleted after being received.
     

    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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_SI_USER_FLAG

      public static final String DEFAULT_SI_USER_FLAG
      Default user flag for marking messages as seen by this receiver: "spring-integration-mail-adapter".
      See Also:
  • Constructor Details

    • AbstractMailReceiver

      public AbstractMailReceiver()
    • AbstractMailReceiver

      public AbstractMailReceiver(jakarta.mail.URLName urlName)
    • AbstractMailReceiver

      public AbstractMailReceiver(String url)
  • Method Details

    • setSelectorExpression

      public void setSelectorExpression(org.springframework.expression.Expression selectorExpression)
    • setProtocol

      public void setProtocol(String protocol)
    • setSession

      public void setSession(jakarta.mail.Session session)
      Set the Session. Otherwise, the Session will be created by invocation of Session.getInstance(Properties) or Session.getInstance(Properties, Authenticator).
      Parameters:
      session - The session.
      See Also:
    • setJavaMailProperties

      public void setJavaMailProperties(Properties javaMailProperties)
      A new Session will be created with these properties (and the JavaMailAuthenticator if provided). Use either this method or setSession(jakarta.mail.Session), but not both.
      Parameters:
      javaMailProperties - The javamail properties.
      See Also:
    • getJavaMailProperties

      protected Properties getJavaMailProperties()
    • setJavaMailAuthenticator

      public void setJavaMailAuthenticator(jakarta.mail.Authenticator javaMailAuthenticator)
      Optional, sets the Authenticator to be used to obtain a session. This will not be used if setSession(jakarta.mail.Session) has been used to configure the Session directly.
      Parameters:
      javaMailAuthenticator - The javamail authenticator.
      See Also:
    • setMaxFetchSize

      public void setMaxFetchSize(int maxFetchSize)
      Specify the maximum number of Messages to fetch per call to receive().
      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

      protected String getUserFlag()
    • setUserFlag

      public 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".
      Parameters:
      userFlag - the flag.
      Since:
      4.2.2
    • setHeaderMapper

      public void setHeaderMapper(org.springframework.integration.mapping.HeaderMapper<jakarta.mail.internet.MimeMessage> headerMapper)
      Set the header mapper; if a header mapper is not provided, the message payload is a MimeMessage, when provided, the headers are mapped and the payload is the MimeMessage content.
      Parameters:
      headerMapper - the header mapper.
      Since:
      4.3
      See Also:
    • setEmbeddedPartsAsBytes

      public void setEmbeddedPartsAsBytes(boolean embeddedPartsAsBytes)
      When a header mapper is provided determine whether an embedded Part (e.g Message or Multipart content is rendered as a byte[] in the payload. Otherwise, leave as a Part. 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:
    • setSimpleContent

      public void setSimpleContent(boolean simpleContent)
      MimeMessage.getContent() returns just the email body.
       foo
       
      Some subclasses, such as IMAPMessage 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 the MimeMessage 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 underlying MimeMessage.

      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 a boolean flag to close the folder automatically after a fetch (default) or populate an additional IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE message header instead. It is the downstream flow's responsibility to obtain this header and call its close() 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) and setHeaderMapper(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 jakarta.mail.Folder getFolder()
    • getFolderOpenMode

      protected int getFolderOpenMode()
    • searchForNewMessages

      protected abstract jakarta.mail.Message[] searchForNewMessages() throws jakarta.mail.MessagingException
      Subclasses must implement this method to return new mail messages.
      Returns:
      An array of messages.
      Throws:
      jakarta.mail.MessagingException - Any MessagingException.
    • openFolder

      protected void openFolder() throws jakarta.mail.MessagingException
      Throws:
      jakarta.mail.MessagingException
    • receive

      public Object[] receive() throws jakarta.mail.MessagingException
      Specified by:
      receive in interface MailReceiver
      Throws:
      jakarta.mail.MessagingException
    • closeFolder

      protected void closeFolder()
    • fetchMessages

      protected void fetchMessages(jakarta.mail.Message[] messages) throws jakarta.mail.MessagingException
      Fetches the specified messages from this receiver's folder. Default implementation fetches every FetchProfile.Item.
      Parameters:
      messages - the messages to fetch
      Throws:
      jakarta.mail.MessagingException - in case of JavaMail errors
    • deleteMessages

      protected void deleteMessages(jakarta.mail.Message[] messages) throws jakarta.mail.MessagingException
      Deletes the given messages from this receiver's folder.
      Parameters:
      messages - the messages to delete
      Throws:
      jakarta.mail.MessagingException - in case of JavaMail errors
    • setAdditionalFlags

      protected void setAdditionalFlags(jakarta.mail.Message message) throws jakarta.mail.MessagingException
      Optional method allowing you to set additional flags. Currently only implemented in IMapMailReceiver.
      Parameters:
      message - The message.
      Throws:
      jakarta.mail.MessagingException - A MessagingException.
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • onInit

      protected void onInit()
      Overrides:
      onInit in class org.springframework.integration.context.IntegrationObjectSupport
    • toString

      public String toString()
      Overrides:
      toString in class org.springframework.integration.context.IntegrationObjectSupport