org.simplejavamail.converter.internal.mimemessage.MimeMessageProducerHelper Maven / Gradle / Ivy
Show all versions of simple-java-mail Show documentation
/*
* Copyright © 2009 Benny Bottema ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.simplejavamail.converter.internal.mimemessage;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import jakarta.mail.internet.MimeMessage;
import org.simplejavamail.api.email.Email;
import org.simplejavamail.email.internal.InternalEmail;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
/**
* Finds a compatible {@link SpecializedMimeMessageProducer} for a given Email and produces a MimeMessage accordingly.
*
* This way, a MimeMessage structure will always be as succinct as possible, so that email clients will never get confused due to missing parts (such
* as no attachments in a "mixed" multipart or no embedded images in a "related" multipart).
*
* Also see issue #144
*/
public final class MimeMessageProducerHelper {
private static final List mimeMessageProducers = Arrays.asList(
new MimeMessageProducerSimple(),
new MimeMessageProducerAlternative(),
new MimeMessageProducerRelated(),
new MimeMessageProducerMixed(),
new MimeMessageProducerMixedRelated(),
new MimeMessageProducerMixedAlternative(),
new MimeMessageProducerRelatedAlternative(),
new MimeMessageProducerMixedRelatedAlternative()
);
private MimeMessageProducerHelper() {
}
@SuppressWarnings("deprecation")
public static MimeMessage produceMimeMessage(Email email, Session session) throws UnsupportedEncodingException, MessagingException {
assert email instanceof InternalEmail;
((InternalEmail) email).verifyDefaultsAndOverridesApplied();
for (SpecializedMimeMessageProducer mimeMessageProducer : mimeMessageProducers) {
if (mimeMessageProducer.compatibleWithEmail(email)) {
return mimeMessageProducer.populateMimeMessage(email, session);
}
}
throw new IllegalStateException("no compatible SpecializedMimeMessageProducer found for email");
}
}