(window.webpackJsonp=window.webpackJsonp||[]).push([[125],{551:function(e,a,t){"use strict";t.r(a);var n=t(56),s=Object(n.a)({},(function(){var e=this,a=e.$createElement,t=e._self._c||a;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"mail-support"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#mail-support"}},[e._v("#")]),e._v(" Mail Support")]),e._v(" "),t("h2",{attrs:{id:"mail-support-2"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#mail-support-2"}},[e._v("#")]),e._v(" Mail Support")]),e._v(" "),t("p",[e._v("This section describes how to work with mail messages in Spring Integration.")]),e._v(" "),t("p",[e._v("You need to include this dependency into your project:")]),e._v(" "),t("p",[e._v("Maven")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n org.springframework.integration\n spring-integration-mail\n 5.5.9\n\n")])])]),t("p",[e._v("Gradle")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('compile "org.springframework.integration:spring-integration-mail:5.5.9"\n')])])]),t("p",[e._v("The "),t("code",[e._v("javax.mail:javax.mail-api")]),e._v(" must be included via vendor-specific implementation.")]),e._v(" "),t("h3",{attrs:{id:"mail-sending-channel-adapter"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#mail-sending-channel-adapter"}},[e._v("#")]),e._v(" Mail-sending Channel Adapter")]),e._v(" "),t("p",[e._v("Spring Integration provides support for outbound email with the "),t("code",[e._v("MailSendingMessageHandler")]),e._v(".\nIt delegates to a configured instance of Spring’s "),t("code",[e._v("JavaMailSender")]),e._v(", as the following example shows:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v(' JavaMailSender mailSender = context.getBean("mailSender", JavaMailSender.class);\n\n MailSendingMessageHandler mailSendingHandler = new MailSendingMessageHandler(mailSender);\n')])])]),t("p",[t("code",[e._v("MailSendingMessageHandler")]),e._v(" has various mapping strategies that use Spring’s "),t("code",[e._v("MailMessage")]),e._v(" abstraction.\nIf the received message’s payload is already a "),t("code",[e._v("MailMessage")]),e._v(" instance, it is sent directly.\nTherefore, we generally recommend that you precede this consumer with a transformer for non-trivial "),t("code",[e._v("MailMessage")]),e._v(" construction requirements.\nHowever, Spring Integration supports a few simple message mapping strategies.\nFor example, if the message payload is a byte array, that is mapped to an attachment.\nFor simple text-based emails, you can provide a string-based message payload.\nIn that case, a "),t("code",[e._v("MailMessage")]),e._v(" is created with that "),t("code",[e._v("String")]),e._v(" as the text content.\nIf you work with a message payload type whose "),t("code",[e._v("toString()")]),e._v(" method returns appropriate mail text content, consider adding Spring Integration’s "),t("code",[e._v("ObjectToStringTransformer")]),e._v(" prior to the outbound mail adapter (see the example in "),t("RouterLink",{attrs:{to:"/en/spring-integration/transformer.html#transformer-namespace"}},[e._v("Configuring a Transformer with XML")]),e._v(" for more detail).")],1),e._v(" "),t("p",[e._v("You can also configure the outbound "),t("code",[e._v("MailMessage")]),e._v(" with certain values from "),t("code",[e._v("MessageHeaders")]),e._v(".\nIf available, values are mapped to the outbound mail’s properties, such as the recipients (To, Cc, and BCc), the from, the reply-to, and the subject.\nThe header names are defined by the following constants:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v(" MailHeaders.SUBJECT\n MailHeaders.TO\n MailHeaders.CC\n MailHeaders.BCC\n MailHeaders.FROM\n MailHeaders.REPLY_TO\n")])])]),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[t("code",[e._v("MailHeaders")]),e._v(" also lets you override corresponding "),t("code",[e._v("MailMessage")]),e._v(" values."),t("br"),e._v("For example, if "),t("code",[e._v("MailMessage.to")]),e._v(" is set to '"),t("a",{attrs:{href:"/cdn-cgi/l/email-protection#d0a4b8b9beb7e190a4b8b9beb7a3feb3bfbd"}},[e._v("[email protected]")]),e._v("' and the "),t("code",[e._v("MailHeaders.TO")]),e._v(" message header is provided, it takes precedence and overrides the corresponding value in "),t("code",[e._v("MailMessage")]),e._v(".")])])]),e._v(" "),t("tbody")]),e._v(" "),t("h3",{attrs:{id:"mail-receiving-channel-adapter"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#mail-receiving-channel-adapter"}},[e._v("#")]),e._v(" Mail-receiving Channel Adapter")]),e._v(" "),t("p",[e._v("Spring Integration also provides support for inbound email with the "),t("code",[e._v("MailReceivingMessageSource")]),e._v(".\nIt delegates to a configured instance of Spring Integration’s own "),t("code",[e._v("MailReceiver")]),e._v(" interface.\nThere are two implementations: "),t("code",[e._v("Pop3MailReceiver")]),e._v(" and "),t("code",[e._v("ImapMailReceiver")]),e._v(".\nThe easiest way to instantiate either of these is by passing the 'uri' for a mail store to the receiver’s constructor, as the following example shows:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('MailReceiver receiver = new Pop3MailReceiver("pop3://usr:[email protected]/INBOX");\n')])])]),t("p",[e._v("Another option for receiving mail is the IMAP "),t("code",[e._v("idle")]),e._v(" command (if supported by your mail server).\nSpring Integration provides the "),t("code",[e._v("ImapIdleChannelAdapter")]),e._v(", which is itself a message-producing endpoint.\nIt delegates to an instance of the "),t("code",[e._v("ImapMailReceiver")]),e._v(" but enables asynchronous reception of mail messages.\nThe next section has examples of configuring both types of inbound channel adapter with Spring Integration’s namespace support in the 'mail' schema.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("Normally, when the "),t("code",[e._v("IMAPMessage.getContent()")]),e._v(" method is called, certain headers as well as the body are rendered (for a simple text email), as the following example shows:")])])]),e._v(" "),t("tbody")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("To: [email protected]\nFrom: [email protected]\nSubject: Test Email\n\nsomething\n")])])]),t("p",[e._v("With a simple "),t("code",[e._v("MimeMessage")]),e._v(", "),t("code",[e._v("getContent()")]),e._v(" returns the mail body ("),t("code",[e._v("something")]),e._v(" in the preceding example).")]),e._v(" "),t("p",[e._v("Starting with version 2.2, the framework eagerly fetches IMAP messages and exposes them as an internal subclass of "),t("code",[e._v("MimeMessage")]),e._v(".\nThis had the undesired side effect of changing the "),t("code",[e._v("getContent()")]),e._v(" behavior.\nThis inconsistency was further exacerbated by the "),t("a",{attrs:{href:"#mail-mapping"}},[e._v("Mail Mapping")]),e._v(" enhancement introduced in version 4.3, because, when a header mapper was provided, the payload was rendered by the "),t("code",[e._v("IMAPMessage.getContent()")]),e._v(" method.\nThis meant that the IMAP content differed, depending on whether or not a header mapper was provided.")]),e._v(" "),t("p",[e._v("Starting with version 5.0, messages originating from an IMAP source render the content in accordance with "),t("code",[e._v("IMAPMessage.getContent()")]),e._v(" behavior, regardless of whether a header mapper is provided.\nIf you do not use a header mapper and you wish to revert to the previous behavior of rendering only the body, set the "),t("code",[e._v("simpleContent")]),e._v(" boolean property on the mail receiver to "),t("code",[e._v("true")]),e._v(".\nThis property now controls the rendering regardless of whether a header mapper is used.\nIt now allows body-only rendering when a header mapper is provided.")]),e._v(" "),t("p",[e._v("Starting with version 5.2, the "),t("code",[e._v("autoCloseFolder")]),e._v(" option is provided on the mail receiver.\nSetting it to "),t("code",[e._v("false")]),e._v(" doesn’t close the folder automatically after a fetch, but instead an "),t("code",[e._v("IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE")]),e._v(" header (see "),t("RouterLink",{attrs:{to:"/en/spring-integration/message.html#message-header-accessor"}},[t("code",[e._v("MessageHeaderAccessor")]),e._v(" API")]),e._v(" for more information) is populated into every message to producer from the channel adapter.\nThis does not work with "),t("code",[e._v("Pop3MailReceiver")]),e._v(" as it relies on opening and closing the folder to get new messages.\nIt is the target application’s responsibility to call the "),t("code",[e._v("close()")]),e._v(" on this header whenever it is necessary in the downstream flow:")],1),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("Closeable closeableResource = StaticMessageHeaderAccessor.getCloseableResource(mailMessage);\nif (closeableResource != null) {\n closeableResource.close();\n}\n")])])]),t("p",[e._v("Keeping the folder open is useful in cases where communication with the server is needed during parsing multipart content of the email with attachments.\nThe "),t("code",[e._v("close()")]),e._v(" on the "),t("code",[e._v("IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE")]),e._v(" header delegates to the "),t("code",[e._v("AbstractMailReceiver")]),e._v(" to close the folder with "),t("code",[e._v("expunge")]),e._v(" option if "),t("code",[e._v("shouldDeleteMessages")]),e._v(" is configured respectively on the "),t("code",[e._v("AbstractMailReceiver")]),e._v(".")]),e._v(" "),t("p",[e._v("Starting with version 5.4, it is possible now to return a "),t("code",[e._v("MimeMessage")]),e._v(" as is without any conversion or eager content loading.\nThis functionality is enabled with this combination of options: no "),t("code",[e._v("headerMapper")]),e._v(" provided, the "),t("code",[e._v("simpleContent")]),e._v(" property is "),t("code",[e._v("false")]),e._v(" and the "),t("code",[e._v("autoCloseFolder")]),e._v(" property is "),t("code",[e._v("false")]),e._v(".\nThe "),t("code",[e._v("MimeMessage")]),e._v(" is present as the payload of the Spring message produced.\nIn this case, the only header populated is the above mentioned "),t("code",[e._v("IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE")]),e._v(" for the folder which must be closed when processing of the "),t("code",[e._v("MimeMessage")]),e._v(" is complete.")]),e._v(" "),t("h3",{attrs:{id:"inbound-mail-message-mapping"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#inbound-mail-message-mapping"}},[e._v("#")]),e._v(" Inbound Mail Message Mapping")]),e._v(" "),t("p",[e._v("By default, the payload of messages produced by the inbound adapters is the raw "),t("code",[e._v("MimeMessage")]),e._v(".\nYou can use that object to interrogate the headers and content.\nStarting with version 4.3, you can provide a "),t("code",[e._v("HeaderMapper")]),e._v(" to map the headers to "),t("code",[e._v("MessageHeaders")]),e._v(".\nFor convenience, Spring Integration provides a "),t("code",[e._v("DefaultMailHeaderMapper")]),e._v(" for this purpose.\nIt maps the following headers:")]),e._v(" "),t("ul",[t("li",[t("p",[t("code",[e._v("mail_from")]),e._v(": A "),t("code",[e._v("String")]),e._v(" representation of the "),t("code",[e._v("from")]),e._v(" address.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_bcc")]),e._v(": A "),t("code",[e._v("String")]),e._v(" array containing the "),t("code",[e._v("bcc")]),e._v(" addresses.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_cc")]),e._v(": A "),t("code",[e._v("String")]),e._v(" array containing the "),t("code",[e._v("cc")]),e._v(" addresses.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_to")]),e._v(": A "),t("code",[e._v("String")]),e._v(" array containing the "),t("code",[e._v("to")]),e._v(" addresses.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_replyTo")]),e._v(": A "),t("code",[e._v("String")]),e._v(" representation of the "),t("code",[e._v("replyTo")]),e._v(" address.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_subject")]),e._v(": The mail subject.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_lineCount")]),e._v(": A line count (if available).")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_receivedDate")]),e._v(": The received date (if available).")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_size")]),e._v(": The mail size (if available).")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_expunged")]),e._v(": A boolean indicating if the message is expunged.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_raw")]),e._v(": A "),t("code",[e._v("MultiValueMap")]),e._v(" containing all the mail headers and their values.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("mail_contentType")]),e._v(": The content type of the original mail message.")])]),e._v(" "),t("li",[t("p",[t("code",[e._v("contentType")]),e._v(": The payload content type (see below).")])])]),e._v(" "),t("p",[e._v("When message mapping is enabled, the payload depends on the mail message and its implementation.\nEmail contents are usually rendered by a "),t("code",[e._v("DataHandler")]),e._v(" within the "),t("code",[e._v("MimeMessage")]),e._v(".")]),e._v(" "),t("p",[e._v("For a "),t("code",[e._v("text/*")]),e._v(" email, the payload is a "),t("code",[e._v("String")]),e._v(" and the "),t("code",[e._v("contentType")]),e._v(" header is the same as "),t("code",[e._v("mail_contentType")]),e._v(".")]),e._v(" "),t("p",[e._v("For a messages with embedded "),t("code",[e._v("javax.mail.Part")]),e._v(" instances, the "),t("code",[e._v("DataHandler")]),e._v(" usually renders a "),t("code",[e._v("Part")]),e._v(" object.\nThese objects are not "),t("code",[e._v("Serializable")]),e._v(" and are not suitable for serialization with alternative technologies such as "),t("code",[e._v("Kryo")]),e._v(".\nFor this reason, by default, when mapping is enabled, such payloads are rendered as a raw "),t("code",[e._v("byte[]")]),e._v(" containing the "),t("code",[e._v("Part")]),e._v(" data.\nExamples of "),t("code",[e._v("Part")]),e._v(" are "),t("code",[e._v("Message")]),e._v(" and "),t("code",[e._v("Multipart")]),e._v(".\nThe "),t("code",[e._v("contentType")]),e._v(" header is "),t("code",[e._v("application/octet-stream")]),e._v(" in this case.\nTo change this behavior and receive a "),t("code",[e._v("Multipart")]),e._v(" object payload, set "),t("code",[e._v("embeddedPartsAsBytes")]),e._v(" to "),t("code",[e._v("false")]),e._v(" on "),t("code",[e._v("MailReceiver")]),e._v(".\nFor content types that are unknown to the "),t("code",[e._v("DataHandler")]),e._v(", the contents are rendered as a "),t("code",[e._v("byte[]")]),e._v(" with a "),t("code",[e._v("contentType")]),e._v(" header of "),t("code",[e._v("application/octet-stream")]),e._v(".")]),e._v(" "),t("p",[e._v("When you do not provide a header mapper, the message payload is the "),t("code",[e._v("MimeMessage")]),e._v(" presented by "),t("code",[e._v("javax.mail")]),e._v(".\nThe framework provides a "),t("code",[e._v("MailToStringTransformer")]),e._v(" that you can use to convert the message by using a strategy to convert the mail contents to a "),t("code",[e._v("String")]),e._v(":")]),e._v(" "),t("p",[e._v("Java DSL")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v(" ...\n .transform(Mail.toStringTransformer())\n ...\n")])])]),t("p",[e._v("Java")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('@Bean\n@Transformer(inputChannel="...", outputChannel="...")\npublic Transformer transformer() {\n return new MailToStringTransformer();\n}\n')])])]),t("p",[e._v("Kotlin")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v(" ...\n transform(Mail.toStringTransformer())\n ...\n")])])]),t("p",[e._v("XML")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n")])])]),t("p",[e._v("Starting with version 4.3, the transformer handles embedded "),t("code",[e._v("Part")]),e._v(" instances (as well as "),t("code",[e._v("Multipart")]),e._v(" instances, which were handled previously).\nThe transformer is a subclass of "),t("code",[e._v("AbstractMailTransformer")]),e._v(" that maps the address and subject headers from the preceding list.\nIf you wish to perform some other transformation on the message, consider subclassing "),t("code",[e._v("AbstractMailTransformer")]),e._v(".")]),e._v(" "),t("p",[e._v("Starting with version 5.4, when no "),t("code",[e._v("headerMapper")]),e._v(" is provided, "),t("code",[e._v("autoCloseFolder")]),e._v(" is "),t("code",[e._v("false")]),e._v(" and "),t("code",[e._v("simpleContent")]),e._v(" is "),t("code",[e._v("false")]),e._v(", the "),t("code",[e._v("MimeMessage")]),e._v(" is returned as-is in the payload of the Spring message produced.\nThis way, the content of the "),t("code",[e._v("MimeMessage")]),e._v(" is loaded on demand when referenced, later in the flow.\nAll of the mentioned above transformations are still valid.")]),e._v(" "),t("h3",{attrs:{id:"mail-namespace-support"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#mail-namespace-support"}},[e._v("#")]),e._v(" Mail Namespace Support")]),e._v(" "),t("p",[e._v("Spring Integration provides a namespace for mail-related configuration.\nTo use it, configure the following schema locations:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n\n')])])]),t("p",[e._v("To configure an outbound channel adapter, provide the channel from which to receive and the MailSender, as the following example shows:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n')])])]),t("p",[e._v("Alternatively, you can provide the host, username, and password, as the following example shows:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n')])])]),t("p",[e._v("Starting with version 5.1.3, the "),t("code",[e._v("host")]),e._v(", "),t("code",[e._v("username")]),e._v(" ane "),t("code",[e._v("mail-sender")]),e._v(" can be omitted, if "),t("code",[e._v("java-mail-properties")]),e._v(" is provided.\nHowever the "),t("code",[e._v("host")]),e._v(" and "),t("code",[e._v("username")]),e._v(" has to be configured with appropriate Java mail properties, e.g. for SMTP:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("[email protected]\nmail.smtp.host=smtp.gmail.com\nmail.smtp.port=587\n")])])]),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("As with any outbound Channel Adapter, if the referenced channel is a "),t("code",[e._v("PollableChannel")]),e._v(", you should provide a "),t("code",[e._v("")]),e._v(" element (see "),t("RouterLink",{attrs:{to:"/en/spring-integration/endpoint.html#endpoint-namespace"}},[e._v("Endpoint Namespace Support")]),e._v(").")],1)])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("When you use the namespace support, you can also use a "),t("code",[e._v("header-enricher")]),e._v(" message transformer.\nDoing so simplifies the application of the headers mentioned earlier to any message prior to sending to the mail outbound channel adapter.")]),e._v(" "),t("p",[e._v("The following example assumes the payload is a Java bean with appropriate getters for the specified properties, but you can use any SpEL expression:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n\t\n\t\n\t\n\t\n\t\n\t\n\n')])])]),t("p",[e._v("Alternatively, you can use the "),t("code",[e._v("value")]),e._v(" attribute to specify a literal.\nYou also can specify "),t("code",[e._v("default-overwrite")]),e._v(" and individual "),t("code",[e._v("overwrite")]),e._v(" attributes to control the behavior with existing headers.")]),e._v(" "),t("p",[e._v("To configure an inbound channel adapter, you have the choice between polling or event-driven (assuming your mail server supports IMAP "),t("code",[e._v("idle")]),e._v(" — if not, then polling is the only option).\nA polling channel adapter requires the store URI and the channel to which to send inbound messages.\nThe URI may begin with "),t("code",[e._v("pop3")]),e._v(" or "),t("code",[e._v("imap")]),e._v(".\nThe following example uses an "),t("code",[e._v("imap")]),e._v(" URI:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n \n\n')])])]),t("p",[e._v("If you do have IMAP "),t("code",[e._v("idle")]),e._v(" support, you may want to configure the "),t("code",[e._v("imap-idle-channel-adapter")]),e._v(" element instead.\nSince the "),t("code",[e._v("idle")]),e._v(" command enables event-driven notifications, no poller is necessary for this adapter.\nIt sends a message to the specified channel as soon as it receives the notification that new mail is available.\nThe following example configures an IMAP "),t("code",[e._v("idle")]),e._v(" mail channel:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n')])])]),t("p",[e._v("You can provide "),t("code",[e._v("javaMailProperties")]),e._v(" by creating and populating a regular "),t("code",[e._v("java.utils.Properties")]),e._v(" object — for example, by using the "),t("code",[e._v("util")]),e._v(" namespace provided by Spring.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("If your username contains the '@' character, use '%40' instead of '@' to avoid parsing errors from the underlying JavaMail API.")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("The following example shows how to configure a "),t("code",[e._v("java.util.Properties")]),e._v(" object:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n javax.net.ssl.SSLSocketFactory\n false\n imaps\n false\n\n')])])]),t("p",[e._v("By default, the "),t("code",[e._v("ImapMailReceiver")]),e._v(" searches for messages based on the default "),t("code",[e._v("SearchTerm")]),e._v(", which is all mail messages that:")]),e._v(" "),t("ul",[t("li",[t("p",[e._v("Are RECENT (if supported)")])]),e._v(" "),t("li",[t("p",[e._v("Are NOT ANSWERED")])]),e._v(" "),t("li",[t("p",[e._v("Are NOT DELETED")])]),e._v(" "),t("li",[t("p",[e._v("Are NOT SEEN")])]),e._v(" "),t("li",[t("p",[e._v("hHave not been processed by this mail receiver (enabled by the use of the custom USER flag or simply NOT FLAGGED if not supported)")])])]),e._v(" "),t("p",[e._v("The custom user flag is "),t("code",[e._v("spring-integration-mail-adapter")]),e._v(", but you can configure it.\nSince version 2.2, the "),t("code",[e._v("SearchTerm")]),e._v(" used by the "),t("code",[e._v("ImapMailReceiver")]),e._v(" is fully configurable with "),t("code",[e._v("SearchTermStrategy")]),e._v(", which you can inject by using the "),t("code",[e._v("search-term-strategy")]),e._v(" attribute.\nA "),t("code",[e._v("SearchTermStrategy")]),e._v(" is a strategy interface with a single method that lets you create an instance of the "),t("code",[e._v("SearchTerm")]),e._v(" used by the "),t("code",[e._v("ImapMailReceiver")]),e._v(".\nThe following listing shows the "),t("code",[e._v("SearchTermStrategy")]),e._v(" interface:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("public interface SearchTermStrategy {\n\n SearchTerm generateSearchTerm(Flags supportedFlags, Folder folder);\n\n}\n")])])]),t("p",[e._v("The following example relies "),t("code",[e._v("TestSearchTermStrategy")]),e._v(" rather than the default "),t("code",[e._v("SearchTermStrategy")]),e._v(":")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n\n\n')])])]),t("p",[e._v("See "),t("a",{attrs:{href:"#imap-seen"}},[e._v("Marking IMAP Messages When "),t("code",[e._v("\\Recent")]),e._v(" Is Not Supported")]),e._v(" for information about message flagging.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("Important: IMAP PEEK"),t("br"),t("br"),e._v("Starting with version 4.1.1, the IMAP mail receiver uses the "),t("code",[e._v("mail.imap.peek")]),e._v(" or "),t("code",[e._v("mail.imaps.peek")]),e._v(" JavaMail property, if specified."),t("br"),e._v("Previously, the receiver ignored the property and always set the "),t("code",[e._v("PEEK")]),e._v(" flag."),t("br"),e._v("Now, if you explicitly set this property to "),t("code",[e._v("false")]),e._v(", the message ise marked as "),t("code",[e._v("\\Seen")]),e._v(" regardless of the setting of "),t("code",[e._v("shouldMarkMessagesRead")]),e._v("."),t("br"),e._v("If not specified, the previous behavior is retained (peek is "),t("code",[e._v("true")]),e._v(").")])])]),e._v(" "),t("tbody")]),e._v(" "),t("h4",{attrs:{id:"imap-idle-and-lost-connections"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#imap-idle-and-lost-connections"}},[e._v("#")]),e._v(" IMAP "),t("code",[e._v("idle")]),e._v(" and Lost Connections")]),e._v(" "),t("p",[e._v("When using an IMAP "),t("code",[e._v("idle")]),e._v(" channel adapter, connections to the server may be lost (for example, through network failure) and, since the JavaMail documentation explicitly states that the actual IMAP API is experimental, it is important to understand the differences in the API and how to deal with them when configuring IMAP "),t("code",[e._v("idle")]),e._v(" adapters.\nCurrently, Spring Integration mail adapters were tested with JavaMail 1.4.1 and JavaMail 1.4.3.\nDepending on which one is used, you must pay special attention to some of the JavaMail properties that need to be set with regard to auto-reconnect.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("The following behavior was observed with Gmail but should provide you with some tips on how to solve re-connect issue with other providers."),t("br"),e._v("However feedback is always welcome."),t("br"),e._v("Again, the following notes are based on Gmail.")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("With JavaMail 1.4.1, if you set the "),t("code",[e._v("mail.imaps.timeout")]),e._v(" property to a relatively short period of time (approximately 5 min in our testing), "),t("code",[e._v("IMAPFolder.idle()")]),e._v(" throws "),t("code",[e._v("FolderClosedException")]),e._v(" after this timeout.\nHowever, if this property is not set (it should be indefinite) the "),t("code",[e._v("IMAPFolder.idle()")]),e._v(" method never returns and never throws an exception.\nIt does, however, reconnect automatically if the connection was lost for a short period of time (under 10 min in our testing).\nHowever, if the connection was lost for a long period of time (over 10 min), "),t("code",[e._v("IMAPFolder.idle()")]),e._v(", does not throw "),t("code",[e._v("FolderClosedException")]),e._v(" and does not re-establish the connection, and remains in the blocked state indefinitely, thus leaving you no possibility to reconnect without restarting the adapter.\nConsequently, the only way to make re-connecting work with JavaMail 1.4.1 is to set the "),t("code",[e._v("mail.imaps.timeout")]),e._v(" property explicitly to some value, but it also means that such value should be relatively short (under 10 min) and the connection should be re-established relatively quickly.\nAgain, it may be different with providers other than Gmail.\nWith JavaMail 1.4.3 introduced significant improvements to the API, ensuring that there is always a condition that forces the "),t("code",[e._v("IMAPFolder.idle()")]),e._v(" method to return "),t("code",[e._v("StoreClosedException")]),e._v(" or "),t("code",[e._v("FolderClosedException")]),e._v(" or to simply return, thus letting you proceed with auto-reconnecting.\nCurrently auto-reconnecting runs infinitely making attempts to reconnect every ten seconds.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("In both configurations, "),t("code",[e._v("channel")]),e._v(" and "),t("code",[e._v("should-delete-messages")]),e._v(" are required attributes."),t("br"),e._v("You should understand why "),t("code",[e._v("should-delete-messages")]),e._v(" is required."),t("br"),e._v("The issue is with the POP3 protocol, which does not have any knowledge of messages that were read."),t("br"),e._v("It can only know what has been read within a single session."),t("br"),e._v("This means that, when your POP3 mail adapter runs, emails are successfully consumed as as they become available during each poll and no single email message is delivered more then once."),t("br"),e._v("However, as soon as you restart your adapter and begin a new session, all the email messages that might have been retrieved in the previous session are retrieved again."),t("br"),e._v("That is the nature of POP3."),t("br"),e._v("Some might argue that "),t("code",[e._v("should-delete-messages")]),e._v(" should be "),t("code",[e._v("true")]),e._v(" by default."),t("br"),e._v("In other words, there are two valid and mutually exclusive use that make it very hard to pick a single best default."),t("br"),e._v("You may want to configure your adapter as the only email receiver, in which case you want to be able to restart your adapter without fear that previously delivered messages are not delivered again."),t("br"),e._v("In this case, setting "),t("code",[e._v("should-delete-messages")]),e._v(" to "),t("code",[e._v("true")]),e._v(" would make the most sense."),t("br"),e._v("However, you may have another use case where you may want to have multiple adapters monitor email servers and their content."),t("br"),e._v("In other words, you want to 'peek but not touch'."),t("br"),e._v("Then setting "),t("code",[e._v("should-delete-messages")]),e._v(" to "),t("code",[e._v("false")]),e._v(" is much more appropriate."),t("br"),e._v("So since it is hard to choose what should be the right default value for the "),t("code",[e._v("should-delete-messages")]),e._v(" attribute, we made it a required attribute to be set by you."),t("br"),e._v("Leaving it up to you also means that you are less likely to end up with unintended behavior.")])])]),e._v(" "),t("tbody")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("When configuring a polling email adapter’s "),t("code",[e._v("should-mark-messages-as-read")]),e._v(" attribute, you should be aware of the protocol you are configuring to retrieve messages."),t("br"),e._v("For example, POP3 does not support this flag, which means setting it to either value has no effect, as messages are not marked as read.")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("In the case of a silently dropped connection, an idle cancel task is run in the background periodically (a new IDLE will usually immediately be processed).\nTo control this interval, a "),t("code",[e._v("cancelIdleInterval")]),e._v(" option is provided; default 120 (2 minutes).\nRFC 2177 recommends an interval no larger than 29 minutes.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("You should understand that that these actions (marking messages read and deleting messages) are performed after the messages are received but before they are processed."),t("br"),e._v("This can cause messages to be lost."),t("br"),t("br"),e._v("You may wish to consider using transaction synchronization instead."),t("br"),e._v("See "),t("a",{attrs:{href:"#mail-tx-sync"}},[e._v("Transaction Synchronization")]),e._v(".")])])]),e._v(" "),t("tbody")]),e._v(" "),t("p",[e._v("The "),t("code",[e._v("")]),e._v(" also accepts the 'error-channel' attribute.\nIf a downstream exception is thrown and an 'error-channel' is specified, a "),t("code",[e._v("MessagingException")]),e._v(" message containing the failed message and the original exception is sent to this channel.\nOtherwise, if the downstream channels are synchronous, any such exception is logged as a warning by the channel adapter.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("Beginning with the 3.0 release, the IMAP "),t("code",[e._v("idle")]),e._v(" adapter emits application events (specifically "),t("code",[e._v("ImapIdleExceptionEvent")]),e._v(" instances) when exceptions occur."),t("br"),e._v("This allows applications to detect and act on those exceptions."),t("br"),e._v("You can obtain the events by using an "),t("code",[e._v("")]),e._v(" or any "),t("code",[e._v("ApplicationListener")]),e._v(" configured to receive an "),t("code",[e._v("ImapIdleExceptionEvent")]),e._v(" or one of its super classes.")])])]),e._v(" "),t("tbody")]),e._v(" "),t("h3",{attrs:{id:"marking-imap-messages-when-recent-is-not-supported"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#marking-imap-messages-when-recent-is-not-supported"}},[e._v("#")]),e._v(" Marking IMAP Messages When "),t("code",[e._v("\\Recent")]),e._v(" Is Not Supported")]),e._v(" "),t("p",[e._v("If "),t("code",[e._v("shouldMarkMessagesAsRead")]),e._v(" is true, the IMAP adapters set the "),t("code",[e._v("\\Seen")]),e._v(" flag.")]),e._v(" "),t("p",[e._v("In addition, when an email server does not support the "),t("code",[e._v("\\Recent")]),e._v(" flag, the IMAP adapters mark messages with a user flag (by default, "),t("code",[e._v("spring-integration-mail-adapter")]),e._v("), as long as the server supports user flags.\nIf not, "),t("code",[e._v("Flag.FLAGGED")]),e._v(" is set to "),t("code",[e._v("true")]),e._v(".\nThese flags are applied regardless of the "),t("code",[e._v("shouldMarkMessagesRead")]),e._v(" setting.")]),e._v(" "),t("p",[e._v("As discussed in "),t("a",{attrs:{href:"#search-term"}},[e._v("[search-term]")]),e._v(", the default "),t("code",[e._v("SearchTermStrategy")]),e._v(" ignore messages that are so flagged.")]),e._v(" "),t("p",[e._v("Starting with version 4.2.2, you can set the name of the user flag by using "),t("code",[e._v("setUserFlag")]),e._v(" on the "),t("code",[e._v("MailReceiver")]),e._v(".\nDoing so lets multiple receivers use a different flag (as long as the mail server supports user flags).\nThe "),t("code",[e._v("user-flag")]),e._v(" attribute is available when configuring the adapter with the namespace.")]),e._v(" "),t("h3",{attrs:{id:"email-message-filtering"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#email-message-filtering"}},[e._v("#")]),e._v(" Email Message Filtering")]),e._v(" "),t("p",[e._v("Very often, you may encounter a requirement to filter incoming messages (for example, you want to read only emails that have 'Spring Integration' in the "),t("code",[e._v("Subject")]),e._v(" line).\nYou can accomplish this by connecting an inbound mail adapter with an expression-based "),t("code",[e._v("Filter")]),e._v(".\nAlthough it would work, there is a downside to this approach.\nSince messages would be filtered after going through the inbound mail adapter, all such messages would be marked as read ("),t("code",[e._v("SEEN")]),e._v(") or unread (depending on the value of "),t("code",[e._v("should-mark-messages-as-read")]),e._v(" attribute).\nHowever, in reality, it be more useful to mark messages as "),t("code",[e._v("SEEN")]),e._v(" only if they pass the filtering criteria.\nThis is similar to looking at your email client while scrolling through all the messages in the preview pane, but only flagging messages that were actually opened and read as "),t("code",[e._v("SEEN")]),e._v(".")]),e._v(" "),t("p",[e._v("Spring Integration 2.0.4 introduced the "),t("code",[e._v("mail-filter-expression")]),e._v(" attribute on "),t("code",[e._v("inbound-channel-adapter")]),e._v(" and "),t("code",[e._v("imap-idle-channel-adapter")]),e._v(".\nThis attribute lets you provide an expression that is a combination of SpEL and a regular expression.\nFor example if you would like to read only emails that contain 'Spring Integration' in the subject line, you would configure the "),t("code",[e._v("mail-filter-expression")]),e._v(" attribute like as follows: "),t("code",[e._v('mail-filter-expression="subject matches \'(?i).**Spring Integration.**"')]),e._v(".")]),e._v(" "),t("p",[e._v("Since "),t("code",[e._v("javax.mail.internet.MimeMessage")]),e._v(" is the root context of the SpEL evaluation context, you can filter on any value available through "),t("code",[e._v("MimeMessage")]),e._v(", including the actual body of the message.\nThis one is particularly important, since reading the body of the message typically results in such messages being marked as "),t("code",[e._v("SEEN")]),e._v(" by default.\nHowever, since we now set the "),t("code",[e._v("PEEK")]),e._v(" flag of every incoming message to 'true', only messages that were explicitly marked as "),t("code",[e._v("SEEN")]),e._v(" are marked as read.")]),e._v(" "),t("p",[e._v("So, in the following example, only messages that match the filter expression are output by this adapter and only those messages are marked as read:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n')])])]),t("p",[e._v("In the preceding example, thanks to the "),t("code",[e._v("mail-filter-expression")]),e._v(" attribute, only messages that contain 'Spring Integration' in the subject line are produced by this adapter.")]),e._v(" "),t("p",[e._v("Another reasonable question is what happens on the next poll or idle event or what happens when such an adapter is restarted.\nCan there be duplication of massages to be filtered? In other words, if, on the last retrieval where you had five new messages and only one passed the filter, what would happen with the other four?\nWould they go through the filtering logic again on the next poll or idle?\nAfter all, they were not marked as "),t("code",[e._v("SEEN")]),e._v(".\nThe answer is no.\nThey would not be subject to duplicate processing due to another flag ("),t("code",[e._v("RECENT")]),e._v(") that is set by the email server and is used by the Spring Integration mail search filter.\nFolder implementations set this flag to indicate that this message is new to this folder.\nThat is, it has arrived since the last time this folder was opened.\nIn other words, while our adapter may peek at the email, it also lets the email server know that such email was touched and should therefore be marked as "),t("code",[e._v("RECENT")]),e._v(" by the email server.")]),e._v(" "),t("h3",{attrs:{id:"transaction-synchronization"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#transaction-synchronization"}},[e._v("#")]),e._v(" Transaction Synchronization")]),e._v(" "),t("p",[e._v("Transaction synchronization for inbound adapters lets you take different actions after a transaction commits or rolls back.\nYou can enable transaction synchronization by adding a "),t("code",[e._v("")]),e._v(" element to the poller for the polled "),t("code",[e._v("")]),e._v(" or to the "),t("code",[e._v("")]),e._v(".\nEven if there is no 'real' transaction involved, you can still enable this feature by using a "),t("code",[e._v("PseudoTransactionManager")]),e._v(" with the "),t("code",[e._v("")]),e._v(" element.\nFor more information, see "),t("RouterLink",{attrs:{to:"/en/spring-integration/transactions.html#transaction-synchronization"}},[e._v("Transaction Synchronization")]),e._v(".")],1),e._v(" "),t("p",[e._v("Because of the many different mail servers and specifically the limitations that some have, at this time we provide only a strategy for these transaction synchronizations.\nYou can send the messages to some other Spring Integration components or invoke a custom bean to perform some action.\nFor example, to move an IMAP message to a different folder after the transaction commits, you might use something similar to the following:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('\n \n\n\n\n \n\n\n\n')])])]),t("p",[e._v("The following example shows what the "),t("code",[e._v("Mover")]),e._v(" class might look like:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('public class Mover {\n\n public void process(MimeMessage message) throws Exception{\n Folder folder = message.getFolder();\n folder.open(Folder.READ_WRITE);\n String messageId = message.getMessageID();\n Message[] messages = folder.getMessages();\n FetchProfile contentsProfile = new FetchProfile();\n contentsProfile.add(FetchProfile.Item.ENVELOPE);\n contentsProfile.add(FetchProfile.Item.CONTENT_INFO);\n contentsProfile.add(FetchProfile.Item.FLAGS);\n folder.fetch(messages, contentsProfile);\n // find this message and mark for deletion\n for (int i = 0; i < messages.length; i++) {\n if (((MimeMessage) messages[i]).getMessageID().equals(messageId)) {\n messages[i].setFlag(Flags.Flag.DELETED, true);\n break;\n }\n }\n\n Folder somethingFolder = store.getFolder("SOMETHING"));\n somethingFolder.appendMessages(new MimeMessage[]{message});\n folder.expunge();\n folder.close(true);\n somethingFolder.close(false);\n }\n}\n')])])]),t("table",[t("thead",[t("tr",[t("th"),e._v(" "),t("th",[e._v("For the message to be still available for manipulation after the transaction, "),t("em",[e._v("should-delete-messages")]),e._v(" must be set to 'false'.")])])]),e._v(" "),t("tbody")]),e._v(" "),t("h3",{attrs:{id:"configuring-channel-adapters-with-the-java-dsl"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#configuring-channel-adapters-with-the-java-dsl"}},[e._v("#")]),e._v(" Configuring channel adapters with the Java DSL")]),e._v(" "),t("p",[e._v("To configure mail mail component in Java DSL, the framework provides a "),t("code",[e._v("o.s.i.mail.dsl.Mail")]),e._v(" factory, which can be used like this:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v('@SpringBootApplication\npublic class MailApplication {\n\n public static void main(String[] args) {\n new SpringApplicationBuilder(MailApplication.class)\n .web(false)\n .run(args);\n }\n\n @Bean\n public IntegrationFlow imapMailFlow() {\n return IntegrationFlows\n .from(Mail.imapInboundAdapter("imap://user:[email protected]:port/INBOX")\n .searchTermStrategy(this::fromAndNotSeenTerm)\n .userFlag("testSIUserFlag")\n .simpleContent(true)\n .javaMailProperties(p -> p.put("mail.debug", "false")),\n e -> e.autoStartup(true)\n .poller(p -> p.fixedDelay(1000)))\n .channel(MessageChannels.queue("imapChannel"))\n .get();\n }\n\n @Bean\n public IntegrationFlow sendMailFlow() {\n return IntegrationFlows.from("sendMailChannel")\n .enrichHeaders(Mail.headers()\n .subjectFunction(m -> "foo")\n .from("[email protected]")\n .toFunction(m -> new String[] { "[email protected]" }))\n .handle(Mail.outboundAdapter("gmail")\n .port(smtpServer.getPort())\n .credentials("user", "pw")\n .protocol("smtp")),\n e -> e.id("sendMailEndpoint"))\n .get();\n }\n}\n')])])])])}),[],!1,null,null,null);a.default=s.exports}}]);