// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package jodd.mail;

import jodd.io.StreamUtil;
import jodd.util.StringPool;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

/**
 * Developer-friendly class for parsing EML files.
 */
public class EMLParser {

	/**
	 * Starts EML parsing with provided EML content.
	 */
	public static EMLParser loadEML(String emlContent, String charset) throws UnsupportedEncodingException {
		byte[] bytes = emlContent.getBytes(charset);
		return loadEML(bytes);
	}

	/**
	 * Starts EML parsing with provided EML content, assuming UTF-8 charset.
	 */
	public static EMLParser loadEML(String emlContent) throws UnsupportedEncodingException {
		return loadEML(emlContent, StringPool.UTF_8);
	}

	/**
	 * Starts EML parsing with provided EML content.
	 */
	public static EMLParser loadEML(byte[] content) {
		return new EMLParser(new ByteArrayInputStream(content));
	}
	/**
	 * Starts EML parsing with provided EML file.
	 */
	public static EMLParser loadEML(File emlFile) throws FileNotFoundException {
		return new EMLParser(new FileInputStream(emlFile));
	}

	protected final InputStream emlContentInputStream;
	protected Session session;

	protected EMLParser(InputStream emlContent) {
		this.emlContentInputStream = emlContent;
	}

	/**
	 * Sets the custom session.
	 */
	public EMLParser session(Session session) {
		this.session = session;
		return this;
	}

	/**
	 * Creates new session with given properties.
	 */
	public EMLParser session(Properties properties) {
		this.session = defaultSession(properties);
		return this;
	}

	/**
	 * Sets session property. If session is not defined, default
	 * session will be created.
	 */
	public EMLParser set(String name, String value) {
		if (session == null) {
			session = defaultSession(null);
		}
		this.session.getProperties().setProperty(name, value);
		return this;
	}

	/**
	 * Parses the EML content. If session is not created, default one
	 * will be used.
	 */
	public ReceivedEmail parse() throws MessagingException {
		if (session == null) {
			session = defaultSession(null);
		}

		Message message;
		try {
			message = new MimeMessage(session, emlContentInputStream);
		} finally {
			StreamUtil.close(emlContentInputStream);
		}

		return new ReceivedEmail(message);
	}

	/**
	 * Creates default session with or without custom properties.
	 */
	protected Session defaultSession(Properties properties) {
		if (properties == null) {
			properties = System.getProperties();
		}

		return Session.getDefaultInstance(properties, null);
	}

}
@@ -3168,49 +3168,312 @@
 n = 
-defaultSession(properties);%0A%09%09return this
+createSession(properties);%0A%09%09return this;%0A%09%7D%0A%0A%09/**%0A%09 * Uses default session instance with given properties.%0A%09 */%0A%09public EMLParser defaultSession(Properties properties) %7B%0A%09%09this.session = getDefaultSession(properties);%0A%09%09return this;%0A%09%7D%0A%0A%09public EMLParser defaultSession() %7B%0A%09%09return defaultSession(null)
 ;%0A%09%7D
@@ -3656,39 +3656,38 @@
  %7B%0A%09%09%09session = 
-default
+create
 Session(null);%0A%09
@@ -3956,23 +3956,22 @@
 ssion = 
-default
+create
 Session(
@@ -4192,23 +4192,19 @@
 Creates 
-default
+new
  session
@@ -4263,23 +4263,22 @@
 Session 
-default
+create
 Session(
@@ -4399,23 +4399,16 @@
 .get
-Default
 Instance
 (pro
@@ -4403,16 +4403,173 @@
 Instance
+(properties);%0A%09%7D%0A%09/**%0A%09 * Creates default session with or without custom properties.%0A%09 */%0A%09protected Session getDefaultSession(Properties properties) %7B%0A%09%09if 
 (propert
@@ -4575,15 +4575,106 @@
 ties
-,
+ ==
  null)
+ %7B%0A%09%09%09properties = new Properties();%0A%09%09%7D%0A%0A%09%09return Session.getDefaultInstance(properties)
 ;%0A%09%7D

Make difference between default and new session instance.