You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Kamil Paśko <ka...@antologic.com> on 2019/06/04 21:03:22 UTC
How to read text/html part of multipart/alternative Email?
Dear Camel user list,
I have such route:
{code}
import java.util.Map;
import java.util.UUID;
import javax.mail.internet.MimeMessage;
import org.apache.camel.Attachment;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.AdviceWithRouteBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.apache.http.client.utils.URIBuilder;
import org.junit.Test;
public class EmailRouteTest extends CamelTestSupport {
protected static final String HOST = "<SET_ME>";
protected static final String LOGIN = "<SET_ME>";
protected static final String PASSWORD = "<SET_ME>";
protected static final String ROUTE_ID = UUID.randomUUID().toString();
@EndpointInject(uri = "mock:email")
private MockEndpoint mockEndpoint;
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
final String routeUri = new URIBuilder()
.setScheme("imaps")
.setHost(HOST)
.setPort(993)
.addParameter("username", LOGIN)
.addParameter("password", PASSWORD)
.addParameter("debugMode", "false")
.addParameter("delete", "false")
.addParameter("unseen", "true")
.addParameter("consumer.delay", "600000")
.addParameter("contentType", "text/html")
.addParameter("mapMailMessage", "true") //<-- change this
.toString();
from(routeUri)
.id(ROUTE_ID)
.marshal().mimeMultipart()
.process(new Processor() {
@Override
public void process(final Exchange exchange) throws Exception {
final Message in = exchange.getIn();
final String contentType =
exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
final String stringBody = in.getBody(String.class);
final Map<String, Attachment> attachmentObjects =
in.getAttachmentObjects();
//Uncomment for mapMailMessage=true
final MimeMessage message = in.getBody(MimeMessage.class);
//Uncomment for mapMailMessage=false
// javax.mail.Message message =
in.getBody(javax.mail.Message.class);
System.out.println("----");
System.out.println(String.format("Body Content-Type: %s",
contentType));
System.out.println(String.format("String Body:\n%s",
stringBody));
System.out.println(String.format("Body:\n%s", message));
System.out.println(String.format("Attachments:\n%s",
attachmentObjects));
System.out.println("----");
assertNotNull(message);
}
});
}
};
}
@Override
public void setUp() throws Exception {
super.setUp();
final CamelContext camelContext = context();
camelContext.getRouteDefinition(ROUTE_ID)
.adviceWith(camelContext, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
weaveAddLast()
.to(mockEndpoint);
}
});
camelContext.start();
}
@Override
public boolean isUseAdviceWith() {
return true;
}
@Test
public void shouldConvertToMailMessage() throws Exception {
mockEndpoint.expectedMessageCount(1);
// mockEndpoint.expectedMessagesMatches(ex -> ex.getIn().getBody()
instanceof javax.mail.Message);
mockEndpoint.assertIsSatisfied();
}
}
{code}
When `mapMailMessage` is set to `true`, no matter what I do, I can't get
text/html part.
`final String stringBody = in.getBody(String.class);` gives text/plain part
of the message, while `final MimeMessage mimeMessage =
in.getBody(MimeMessage.class);` and `final MimeMultipart message =
in.getBody(MimeMultipart.class);` both return null.
When `mapMailMessage` is set to `false`
`javax.mail.Message message = in.getBody(javax.mail.Message.class);` also
returns null.
Is there any possibility to map HTML part of the email to some Camel
Message ?
Kind regards,
Kamil
Re: How to read text/html part of multipart/alternative Email?
Posted by Łukasz Dywicki <lu...@code-house.org>.
Hey Kamil,
Regular MimeMessage might be of type MimeMultipart which has multiple
content parts. See below code which worked with javax.mail types directly:
MimeMessage message = msg.getMimeMessage();
MimeMultipart content = (MimeMultipart) message.getContent();
for (int index = 0; index < content.getCount(); index++) {
BodyPart bodyPart = content.getBodyPart(index);
if (bodyPart.getContentType().startsWith(MediaType.TEXT_PLAIN)) {
System.out.println(bodyPart.getContentType() + "\n" +
bodyPart.getContent());
// process text plain message
}
}
Powodzenia,
Łukasz
--
Code-House: http://code-house.org
On 04.06.2019 23:03, Kamil Paśko wrote:
> Dear Camel user list,
>
> I have such route:
> {code}
> import java.util.Map;
> import java.util.UUID;
>
> import javax.mail.internet.MimeMessage;
>
> import org.apache.camel.Attachment;
> import org.apache.camel.CamelContext;
> import org.apache.camel.EndpointInject;
> import org.apache.camel.Exchange;
> import org.apache.camel.Message;
> import org.apache.camel.Processor;
> import org.apache.camel.RoutesBuilder;
> import org.apache.camel.builder.AdviceWithRouteBuilder;
> import org.apache.camel.builder.RouteBuilder;
> import org.apache.camel.component.mock.MockEndpoint;
> import org.apache.camel.test.junit4.CamelTestSupport;
> import org.apache.http.client.utils.URIBuilder;
> import org.junit.Test;
>
> public class EmailRouteTest extends CamelTestSupport {
>
> protected static final String HOST = "<SET_ME>";
> protected static final String LOGIN = "<SET_ME>";
> protected static final String PASSWORD = "<SET_ME>";
>
> protected static final String ROUTE_ID = UUID.randomUUID().toString();
>
> @EndpointInject(uri = "mock:email")
> private MockEndpoint mockEndpoint;
>
> @Override
> protected RoutesBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
>
> @Override
> public void configure() throws Exception {
> final String routeUri = new URIBuilder()
> .setScheme("imaps")
> .setHost(HOST)
> .setPort(993)
> .addParameter("username", LOGIN)
> .addParameter("password", PASSWORD)
> .addParameter("debugMode", "false")
> .addParameter("delete", "false")
> .addParameter("unseen", "true")
> .addParameter("consumer.delay", "600000")
> .addParameter("contentType", "text/html")
> .addParameter("mapMailMessage", "true") //<-- change this
> .toString();
>
> from(routeUri)
> .id(ROUTE_ID)
> .marshal().mimeMultipart()
> .process(new Processor() {
>
> @Override
> public void process(final Exchange exchange) throws Exception {
> final Message in = exchange.getIn();
> final String contentType =
> exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
> final String stringBody = in.getBody(String.class);
> final Map<String, Attachment> attachmentObjects =
> in.getAttachmentObjects();
>
> //Uncomment for mapMailMessage=true
> final MimeMessage message = in.getBody(MimeMessage.class);
>
> //Uncomment for mapMailMessage=false
> // javax.mail.Message message =
> in.getBody(javax.mail.Message.class);
>
> System.out.println("----");
> System.out.println(String.format("Body Content-Type: %s",
> contentType));
> System.out.println(String.format("String Body:\n%s",
> stringBody));
> System.out.println(String.format("Body:\n%s", message));
> System.out.println(String.format("Attachments:\n%s",
> attachmentObjects));
> System.out.println("----");
>
> assertNotNull(message);
> }
> });
> }
> };
> }
>
> @Override
> public void setUp() throws Exception {
> super.setUp();
> final CamelContext camelContext = context();
> camelContext.getRouteDefinition(ROUTE_ID)
> .adviceWith(camelContext, new AdviceWithRouteBuilder() {
> @Override
> public void configure() throws Exception {
> weaveAddLast()
> .to(mockEndpoint);
> }
> });
> camelContext.start();
> }
>
> @Override
> public boolean isUseAdviceWith() {
> return true;
> }
>
> @Test
> public void shouldConvertToMailMessage() throws Exception {
> mockEndpoint.expectedMessageCount(1);
> // mockEndpoint.expectedMessagesMatches(ex -> ex.getIn().getBody()
> instanceof javax.mail.Message);
> mockEndpoint.assertIsSatisfied();
> }
> }
> {code}
>
> When `mapMailMessage` is set to `true`, no matter what I do, I can't get
> text/html part.
>
> `final String stringBody = in.getBody(String.class);` gives text/plain part
> of the message, while `final MimeMessage mimeMessage =
> in.getBody(MimeMessage.class);` and `final MimeMultipart message =
> in.getBody(MimeMultipart.class);` both return null.
>
> When `mapMailMessage` is set to `false`
> `javax.mail.Message message = in.getBody(javax.mail.Message.class);` also
> returns null.
>
> Is there any possibility to map HTML part of the email to some Camel
> Message ?
>
> Kind regards,
> Kamil
>