MailMessage.java 3.37 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.util;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.mail.internet.InternetAddress;

import fi.codecrew.moya.model.User;

public class MailMessage implements Serializable {
	/**
     * 
     */
	private static final long serialVersionUID = -4769468394850407109L;

	private static final String DEFAULT_MAIL_CHARSET = "ISO-8859-1";

	private String subject;
	private String fromName;
	private String fromAddress;
	private String toName;
	private String toAddress;
	private String message;
	private String charset = DEFAULT_MAIL_CHARSET;

	private List<MailMessageAttachment> attachmentList;

	public InternetAddress getTo() throws UnsupportedEncodingException {
		return new InternetAddress(toAddress, toName, getCharset());
	}

	public InternetAddress getFrom() throws UnsupportedEncodingException {
		return new InternetAddress(fromAddress, fromName, getCharset());
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getFromName() {
		return fromName;
	}

	public void setFromName(String fromName) {
		this.fromName = fromName;
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getToName() {
		return toName;
	}

	public void setToName(String toName) {
		this.toName = toName;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(Object... msgpart) {
		StringBuilder msg = new StringBuilder();
		for (Object o : msgpart) {
			msg.append(o);
		}
		message = msg.toString();
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public static String getDefaultMailCharset() {
		return DEFAULT_MAIL_CHARSET;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getCharset() {
		return charset;
	}

	public List<MailMessageAttachment> getAttachmentList() {
		return attachmentList;
	}

	public void setAttachmentList(List<MailMessageAttachment> attachmentList) {
		this.attachmentList = attachmentList;
	}

	public void setTo(User user) {
		setToName(user.getWholeName());
		setToAddress(user.getEmail());

	}

	@Override
	public String toString() {
		return new StringBuilder("fi.codecrew.moya.util.MailMessage[to=").append(toAddress).append("]").toString();
	}

	public void addAttachment(MailMessageAttachment attachment) {
		if(attachmentList == null)
			attachmentList = new ArrayList<>();

		attachmentList.add(attachment);
	}
}