All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbasics.net.mail.MimeMessageBuilder Maven / Gradle / Ivy

Go to download

jBasics is a collection of useful utility classes for Java. This includes helper for XML, mathematic functions, restful web services helper, pattern oriented programming interfaces and more. Currently Java7 and up is supported. Version 1.0 will required at leaset Java8.

There is a newer version: 2.0.0p3
Show newest version
/*
 * Copyright (c) 2009-2015
 * 	IT-Consulting Stephan Schloepke (http://www.schloepke.de/)
 * 	klemm software consulting Mirko Klemm (http://www.klemm-scs.com/)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jbasics.net.mail;

import org.jbasics.checker.ContractCheck;
import org.jbasics.exception.DelegatedException;
import org.jbasics.pattern.builder.Builder;
import org.jbasics.pattern.delegation.Delegate;
import org.jbasics.pattern.factory.Factory;
import org.jbasics.types.delegates.LazySoftReferenceDelegate;
import org.jbasics.types.delegates.UnmodifiableDelegate;
import org.jbasics.types.tuples.Pair;

import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

public class MimeMessageBuilder implements Builder {
	private final Delegate mailSessionDelegate;
	private final Set> recipients = new LinkedHashSet>();
	private InternetAddress from;
	private String subject;
	private String messageType;
	private Object messageContent;
	private Date sentDate;

	public MimeMessageBuilder(final Session session) {
		this(new UnmodifiableDelegate(ContractCheck.mustNotBeNull(session, "session"))); //$NON-NLS-1$
	}

	public MimeMessageBuilder(final Delegate sessionDelegate) {
		this.mailSessionDelegate = ContractCheck.mustNotBeNull(sessionDelegate, "sessionDelegate"); //$NON-NLS-1$
	}

	public MimeMessageBuilder(final Factory sessionFactory) {
		this(new LazySoftReferenceDelegate(ContractCheck.mustNotBeNull(sessionFactory, "sessionFactory"))); //$NON-NLS-1$
	}

	public MimeMessageBuilder setFrom(final String address) {
		try {
			this.from = new InternetAddress(address, true);
			return this;
		} catch (final AddressException e) {
			throw DelegatedException.delegate(e);
		}
	}

	public MimeMessageBuilder addToRecipients(final String... addresses) {
		for (final String address : addresses) {
			addRecipient(RecipientType.TO, address);
		}
		return this;
	}

	public MimeMessageBuilder addRecipient(final RecipientType type, final String address) {
		try {
			this.recipients.add(new Pair(type, new InternetAddress(address)));
			return this;
		} catch (final AddressException e) {
			throw DelegatedException.delegate(e);
		}
	}

	public MimeMessageBuilder addCcRecipient(final String... addresses) {
		for (final String address : addresses) {
			addRecipient(RecipientType.CC, address);
		}
		return this;
	}

	public MimeMessageBuilder addBccRecipient(final String... addresses) {
		for (final String address : addresses) {
			addRecipient(RecipientType.BCC, address);
		}
		return this;
	}

	public MimeMessageBuilder setSubject(final String subject) {
		this.subject = subject;
		return this;
	}

	public MimeMessageBuilder setSimpleContent(final Object content) {
		return setSimpleContent(null, content);
	}

	public MimeMessageBuilder setSimpleContent(final String type, final Object content) {
		this.messageType = type;
		this.messageContent = content;
		return this;
	}

	@Override
	public void reset() {
		this.from = null;
		this.recipients.clear();
		this.subject = null;
		this.messageContent = null;
		this.messageType = null;
		this.sentDate = null;
	}

	@Override
	public MimeMessage build() {
		try {
			final MimeMessage message = new MimeMessage(this.mailSessionDelegate.delegate());
			if (this.from != null) {
				message.setFrom(this.from);
			} else {
				message.setFrom();
			}
			for (final Pair recipient : this.recipients) {
				if (recipient.left() != null) {
					message.addRecipient(recipient.left(), recipient.right());
				}
			}
			message.setSubject(this.subject);
			if (this.messageType == null) {
				if (this.messageContent != null) {
					message.setContent(this.messageContent.toString(), "text/plain"); //$NON-NLS-1$
				}
			} else if (this.messageContent != null) {
				message.setContent(this.messageContent, this.messageType);
			}
			if (this.sentDate == null) {
				message.setSentDate(new Date());
			} else {
				message.setSentDate((this.sentDate));
			}
			return message;
		} catch (final MessagingException e) {
			throw DelegatedException.delegate(e);
		}
	}

	public MimeMessageBuilder buildAndSend() {
		try {
			return buildAndSendWithMessageExeption();
		} catch (final MessagingException e) {
			throw DelegatedException.delegate(e);
		}
	}

	public MimeMessageBuilder buildAndSendWithMessageExeption() throws MessagingException {
		Transport.send(build());
		return this;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy