public TextBody textBody(final String text) { return textBody(text, Charsets.DEFAULT_CHARSET); }
private TextBody createTextBody(CreationMessage newMessage) { String body = newMessage.getHtmlBody() .orElse(newMessage.getTextBody() .orElse("")); return bodyFactory.textBody(body, StandardCharsets.UTF_8); }
public TextBody buildText() throws IOException { Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET; if (this.bodyFactory != null) { if (this.text != null) { return this.bodyFactory.textBody(InputStreams.create(this.text, cs), cs.name()); } else if (this.bin != null) { return this.bodyFactory.textBody(InputStreams.create(this.bin), cs.name()); } else { return this.bodyFactory.textBody(InputStreams.create(new byte [] {}), cs.name()); } } else { if (this.text != null) { return BasicBodyFactory.INSTANCE.textBody(this.text, cs); } else if (this.bin != null) { return BasicBodyFactory.INSTANCE.textBody(this.bin, cs); } else { return BasicBodyFactory.INSTANCE.textBody(new byte [] {}, cs); } } }
public MultipartBuilder addTextPart(String text, Charset charset) throws IOException { Charset cs = charset != null ? charset : Charsets.ISO_8859_1; TextBody body = bodyFactory != null ? bodyFactory.textBody( InputStreams.create(text, cs), cs.name()) : BasicBodyFactory.INSTANCE.textBody(text, cs); BodyPart bodyPart = BodyPartBuilder.create() .setBody(body) .setContentType("text/plain", new NameValuePair("charset", cs.name())) .setContentTransferEncoding(Charsets.US_ASCII.equals(cs) ? "7bit" : "quoted-printable") .build(); return addBodyPart(bodyPart); }
/** * Sets text of this message with the given MIME subtype and charset. * * @param text * the text. * @param charset * the charset of the text. * @param subtype * the text subtype (e.g. "plain", "html" or * "xml"). */ public Builder setBody(String text, String subtype, Charset charset) throws IOException { String mimeType = "text/" + (subtype != null ? subtype : "plain"); if (charset != null) { setField(Fields.contentType(mimeType, new NameValuePair("charset", charset.name()))); } else { setField(Fields.contentType(mimeType)); } Body textBody; if (bodyFactory != null) { textBody = bodyFactory.textBody( InputStreams.create(text, charset), charset != null ? charset.name() : null); } else { textBody = BasicBodyFactory.INSTANCE.textBody(text, charset); } return setBody(textBody); }
/** * Sets text of this message with the given MIME subtype and charset. * * @param text * the text. * @param charset * the charset of the text. * @param subtype * the text subtype (e.g. "plain", "html" or * "xml"). */ public BodyPartBuilder setBody(String text, String subtype, Charset charset) throws IOException { String mimeType = "text/" + (subtype != null ? subtype : "plain"); if (charset != null) { setField(Fields.contentType(mimeType, new NameValuePair("charset", charset.name()))); } else { setField(Fields.contentType(mimeType)); } Body textBody; if (bodyFactory != null) { textBody = bodyFactory.textBody( InputStreams.create(text, charset), charset != null ? charset.name() : null); } else { textBody = BasicBodyFactory.INSTANCE.textBody(text, charset); } return setBody(textBody); }
.htmlBody("Hello <b>all<b>!") .build(); TextBody expectedTextBody = new BasicBodyFactory().textBody("Hello all!".getBytes(), StandardCharsets.UTF_8); TextBody expectedHtmlBody = new BasicBodyFactory().textBody("Hello <b>all<b>!".getBytes(), StandardCharsets.UTF_8); TextBody expectedAttachmentBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8); MessageAttachment attachment = MessageAttachment.builder() .attachment(org.apache.james.mailbox.model.Attachment.builder()
@Test public void convertToMimeShouldSetEmptyTextBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .textBody("") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); assertThat(result.getMimeType()).isEqualTo("text/plain"); }
@Test public void convertToMimeShouldSetEmptyHtmlBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); assertThat(result.getMimeType()).isEqualTo("text/html"); }
String expectedMimeType = "image/png"; String text = "123456"; TextBody expectedBody = new BasicBodyFactory().textBody(text.getBytes(), StandardCharsets.UTF_8); MessageAttachment attachment = MessageAttachment.builder() .attachment(org.apache.james.mailbox.model.Attachment.builder()
@Test public void convertToMimeShouldSetEmptyBodyWhenNoBodyProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
@Test public void convertToMimeShouldSetHtmlBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("Hello <b>all</b>!", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .htmlBody("Hello <b>all</b>!") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }
@Test public void convertToMimeShouldSetTextBodyWhenProvided() { // Given MIMEMessageConverter sut = new MIMEMessageConverter(); TextBody expected = new BasicBodyFactory().textBody("Hello all!", StandardCharsets.UTF_8); CreationMessage testMessage = CreationMessage.builder() .mailboxId("dead-bada55") .subject("subject") .from(DraftEmailer.builder().name("sender").build()) .textBody("Hello all!") .build(); // When Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry( CreationMessageId.of("user|mailbox|1"), testMessage), ImmutableList.of()); // Then assertThat(result.getBody()).isEqualToComparingOnlyGivenFields(expected, "content", "charset"); }