use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.
the class MimeMessageHelper method setReplyTo.
/**
* Fills the {@link Message} instance with reply-to address.
*
* @param email The message in which the recipients are defined.
* @param message The javax message that needs to be filled with reply-to address.
* @throws UnsupportedEncodingException See {@link InternetAddress#InternetAddress(String, String)}.
* @throws MessagingException See {@link Message#setReplyTo(Address[])}
*/
static void setReplyTo(final Email email, final Message message) throws UnsupportedEncodingException, MessagingException {
final Recipient replyToRecipient = email.getReplyToRecipient();
if (replyToRecipient != null) {
final InternetAddress replyToAddress = new InternetAddress(replyToRecipient.getAddress(), replyToRecipient.getName(), CHARACTER_ENCODING);
message.setReplyTo(new Address[] { replyToAddress });
}
}
use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.
the class MimeMessageHelper method setHeaders.
/**
* Sets all headers on the {@link Message} instance. Since we're not using a high-level JavaMail method, the JavaMail library says we need to do
* some encoding and 'folding' manually, to get the value right for the headers (see {@link MimeUtility}.
* <p>
* Furthermore sets the notification flags <code>Disposition-Notification-To</code> and <code>Return-Receipt-To</code> if provided. It used
* JavaMail's built in method for producing an RFC compliant email address (see {@link InternetAddress#toString()}).
*
* @param email The message in which the headers are defined.
* @param message The {@link Message} on which to set the raw, encoded and folded headers.
* @throws UnsupportedEncodingException See {@link MimeUtility#encodeText(String, String, String)}
* @throws MessagingException See {@link Message#addHeader(String, String)}
* @see MimeUtility#encodeText(String, String, String)
* @see MimeUtility#fold(int, String)
*/
static void setHeaders(final Email email, final Message message) throws UnsupportedEncodingException, MessagingException {
// add headers (for raw message headers we need to 'fold' them using MimeUtility
for (final Map.Entry<String, Collection<String>> header : email.getHeaders().entrySet()) {
for (final String headerValue : header.getValue()) {
final String headerName = header.getKey();
final String headerValueEncoded = MimeUtility.encodeText(headerValue, CHARACTER_ENCODING, null);
final String foldedHeaderValue = MimeUtility.fold(headerName.length() + 2, headerValueEncoded);
message.addHeader(header.getKey(), foldedHeaderValue);
}
}
if (email.isUseDispositionNotificationTo()) {
final Recipient dispositionTo = checkNonEmptyArgument(email.getDispositionNotificationTo(), "dispositionNotificationTo");
final Address address = new InternetAddress(dispositionTo.getAddress(), dispositionTo.getName(), CHARACTER_ENCODING);
message.setHeader("Disposition-Notification-To", address.toString());
}
if (email.isUseReturnReceiptTo()) {
final Recipient returnReceiptTo = checkNonEmptyArgument(email.getReturnReceiptTo(), "returnReceiptTo");
final Address address = new InternetAddress(returnReceiptTo.getAddress(), returnReceiptTo.getName(), CHARACTER_ENCODING);
message.setHeader("Return-Receipt-To", address.toString());
}
}
use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.
the class MimeMessageHelper method setRecipients.
/**
* Fills the {@link Message} instance with recipients from the {@link Email}.
*
* @param email The message in which the recipients are defined.
* @param message The javax message that needs to be filled with recipients.
* @throws UnsupportedEncodingException See {@link InternetAddress#InternetAddress(String, String)}.
* @throws MessagingException See {@link Message#addRecipient(Message.RecipientType, Address)}
*/
static void setRecipients(final Email email, final Message message) throws UnsupportedEncodingException, MessagingException {
for (final Recipient recipient : email.getRecipients()) {
final Address address = new InternetAddress(recipient.getAddress(), recipient.getName(), CHARACTER_ENCODING);
message.addRecipient(recipient.getType(), address);
}
}
use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.
the class EmailConverterTest method testOutlookUnsentDraft.
@Test
public void testOutlookUnsentDraft() {
final Recipient time2talk = new Recipient("time2talk@online-convert.com", "time2talk@online-convert.com", TO);
@NotNull Email msg = EmailConverter.outlookMsgToEmail(new File(RESOURCE_TEST_MESSAGES + "/unsent draft.msg"));
EmailAssert.assertThat(msg).hasFromRecipient(new Recipient(null, "donotreply@unknown-from-address.net", null));
EmailAssert.assertThat(msg).hasSubject("MSG Test File");
EmailAssert.assertThat(msg).hasOnlyRecipients(time2talk);
EmailAssert.assertThat(msg).hasNoAttachments();
assertThat(msg.getPlainText()).isNotEmpty();
assertThat(normalizeNewlines(msg.getHTMLText())).isNotEmpty();
}
use of org.simplejavamail.api.email.Recipient in project simple-java-mail by bbottema.
the class EmailConverterTest method testOutlookBasicConversions.
@Test
public void testOutlookBasicConversions() {
final Recipient elias = new Recipient("Elias Laugher", "elias.laugher@gmail.com", null);
final Recipient sven = new Recipient("Sven Sielenkemper", "sielenkemper@otris.de", TO);
final Recipient niklas = new Recipient("niklas.lindson@gmail.com", "niklas.lindson@gmail.com", CC);
@NotNull Email msg = EmailConverter.outlookMsgToEmail(new File(RESOURCE_TEST_MESSAGES + "/simple email with TO and CC.msg"));
EmailAssert.assertThat(msg).hasFromRecipient(elias);
EmailAssert.assertThat(msg).hasSubject("Test E-Mail");
EmailAssert.assertThat(msg).hasOnlyRecipients(sven, niklas);
EmailAssert.assertThat(msg).hasNoAttachments();
assertThat(msg.getPlainText()).isNotEmpty();
assertThat(normalizeNewlines(msg.getHTMLText())).isEqualTo("<div dir=\"auto\">Just a test to get an email with one cc recipient.</div>\n");
assertThat(normalizeNewlines(msg.getPlainText())).isEqualTo("Just a test to get an email with one cc recipient.\n");
}
Aggregations