// This file was generated by Mendix Modeler.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package emailtemplate.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.mendix.core.Core;
import com.mendix.core.CoreException;
import com.mendix.logging.ILogNode;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import emailtemplate.mail.EmailModule;
import emailtemplate.mail.SMTPConfiguration;
import emailtemplate.proxies.Header;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;

/**
 * 
 */
public class SendEmail extends CustomJavaAction<Boolean>
{
	private String SMTPHost;
	private String SMTPUserName;
	private String SMTPPassword;
	private String CCAddresses;
	private String BCCAddresses;
	private String ToAddresses;
	private String FromAddress;
	private String HtmlBody;
	private String PlainBody;
	private String Subject;
	private Long SMTPPort;
	private java.util.List<IMendixObject> __AttachmentList;
	private java.util.List<system.proxies.FileDocument> AttachmentList;
	private java.util.List<IMendixObject> __HeaderList;
	private java.util.List<emailtemplate.proxies.Header> HeaderList;
	private Boolean UseSSL;
	private Boolean UseTLS;

	public SendEmail(IContext context, String SMTPHost, String SMTPUserName, String SMTPPassword, String CCAddresses, String BCCAddresses, String ToAddresses, String FromAddress, String HtmlBody, String PlainBody, String Subject, Long SMTPPort, java.util.List<IMendixObject> AttachmentList, java.util.List<IMendixObject> HeaderList, Boolean UseSSL, Boolean UseTLS)
	{
		super(context);
		this.SMTPHost = SMTPHost;
		this.SMTPUserName = SMTPUserName;
		this.SMTPPassword = SMTPPassword;
		this.CCAddresses = CCAddresses;
		this.BCCAddresses = BCCAddresses;
		this.ToAddresses = ToAddresses;
		this.FromAddress = FromAddress;
		this.HtmlBody = HtmlBody;
		this.PlainBody = PlainBody;
		this.Subject = Subject;
		this.SMTPPort = SMTPPort;
		this.__AttachmentList = AttachmentList;
		this.__HeaderList = HeaderList;
		this.UseSSL = UseSSL;
		this.UseTLS = UseTLS;
	}

	@Override
	public Boolean executeAction() throws Exception
	{
		this.AttachmentList = new java.util.ArrayList<system.proxies.FileDocument>();
		if (__AttachmentList != null)
			for (IMendixObject __AttachmentListElement : __AttachmentList)
				this.AttachmentList.add(system.proxies.FileDocument.initialize(getContext(), __AttachmentListElement));

		this.HeaderList = new java.util.ArrayList<emailtemplate.proxies.Header>();
		if (__HeaderList != null)
			for (IMendixObject __HeaderListElement : __HeaderList)
				this.HeaderList.add(emailtemplate.proxies.Header.initialize(getContext(), __HeaderListElement));

		// BEGIN USER CODE

		SMTPConfiguration config = new SMTPConfiguration();
		if (this._logNode.isTraceEnabled()) {
			this._logNode.trace("Setting up SMTP configuration ( host: " + this.SMTPHost + ":" + this.SMTPPort + " user: " + this.SMTPUserName + " from:" + this.FromAddress);
		}

		if (this.SMTPHost == null)
			throw new CoreException("There is no smtp server address specified.");
		// ticket #19433 - username can be null in anonymous access scenario's
		//if (this.SMTPUserName == null)
		//	throw new CoreException("There is no user given to access the SMTP server.");
		if (this.FromAddress == null)
			throw new CoreException("There is no email address configured as the sender.");

		config.setFromAddress(this.FromAddress);
		config.setSMTPHost(this.SMTPHost);
		config.setSMTPPort((this.SMTPPort != null ? this.SMTPPort.intValue() : (this.UseSSL ? 465 : 25)));
		config.setUserName((this.SMTPUserName != null ? this.SMTPUserName : ""));
		config.setUserPass((this.SMTPPassword != null ? this.SMTPPassword : ""));
		config.setUseSSLSMTP(this.UseSSL);
		config.setUseTLSSMTP(this.UseTLS);

		String separator = (String) Core.getConfiguration().getConstantValue("EmailTemplate.EmailAddressSeparator");
		if (separator == null)
			separator = ",";

		ArrayList<String> toList = null;
		if (this.ToAddresses != null && !"".equals(this.ToAddresses.trim())) {
			String[] addrArr = this.ToAddresses.split(separator);
			toList = new ArrayList<String>();
			for (String addr : addrArr)
				toList.add(addr);
		}

		ArrayList<String> ccList = null;
		if (this.CCAddresses != null && !"".equals(this.CCAddresses.trim())) {
			String[] addrArr = this.CCAddresses.split(separator);
			ccList = new ArrayList<String>();
			for (String addr : addrArr)
				ccList.add(addr);
		}

		ArrayList<String> bccList = null;
		if (this.BCCAddresses != null && !"".equals(this.BCCAddresses.trim())) {
			String[] addrArr = this.BCCAddresses.split(separator);
			bccList = new ArrayList<String>();
			for (String addr : addrArr)
				bccList.add(addr);
		}
		
		Map<String, String> headerMap = new HashMap<String, String>();
		if (this.HeaderList != null) {
			for (Header header : this.HeaderList) {
				headerMap.put(header.getName(), header.getValue());
			}
		}
		
		this._logNode.trace("Send email to: " + (toList != null ? toList.size() : "0") + " addresses in to, " + (ccList != null ? ccList.size() : "0") + " in cc, " + (bccList != null ? bccList.size() : "0") + " in bcc");

		if ((toList != null ? toList.size() : 0) == 0 && (ccList != null ? ccList.size() : 0) == 0 && (bccList != null ? bccList.size() : 0) == 0)
			throw new CoreException("There is no email address found to send this email to.");

		try {
			if (this.PlainBody == null)
				this.PlainBody = ConvertHTMLBodyToPlainText.removeHTML(this.HtmlBody);
		}
		catch (Exception e) {
			throw new CoreException("Unable to convert the Html body to plain text for email: " + this.Subject + " to: " + this.ToAddresses, e);
		}

		EmailModule.mail(config, this.HtmlBody, this.PlainBody, this.Subject, toList, ccList, bccList, this.getContext(), this.__AttachmentList, headerMap);

		return true;
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 */
	@Override
	public String toString()
	{
		return "SendEmail";
	}

	// BEGIN EXTRA CODE
	private ILogNode _logNode = Core.getLogger(this.toString());
	// END EXTRA CODE
}

@@ -4284,64 +4284,53 @@
 ng) 
-Core.getConfiguration().getConstantValue(%22EmailTemplate.
+emailtemplate.proxies.constants.Constants.get
 Emai
@@ -4346,17 +4346,17 @@
 eparator
-%22
+(
 );%0D%0A%09%09if

Update SendEmail.java