package org.apache.maven.announcement;

import java.io.PrintWriter;
import java.util.StringTokenizer;

import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.commons.net.smtp.SimpleSMTPHeader;

/* ====================================================================
 *   Copyright 2003-2004 The Apache Software Foundation.
 *
 *   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.
 * ====================================================================
 */

/**
 * Send an email message using Jakarta Commons Net
 */
public class MailUtils {
	
	private static final String ADRESSES_SEPARATOR = ",";
	
	public static void sendMail( String server, String from, String to, String cc, String subject, String msg )
	throws Exception {
		
		// check arguments
		if (server == null) {
			throw new IllegalArgumentException("no smtp server configured");
		}	
		if (to == null) {
			throw new IllegalArgumentException("no to address specified");
		}
		if (from == null) {
			throw new IllegalArgumentException("no from address specified");
		}
		if (msg== null) {
			throw new IllegalArgumentException("no message specified");
		}
		if (subject== null) {
			throw new IllegalArgumentException("no subject specified");
		}
		
		// create a SMTP connection
		SMTPClient client = new SMTPClient();
		System.out.println("connecting to SMTP Server " + server);
		client.connect(server);
		// checks the server reply
		int reply = client.getReplyCode();
		if (!SMTPReply.isPositiveCompletion(reply)) {
			client.disconnect();
			throw new IllegalArgumentException("SMTP server " + server
					+ " refused connection.");
		}
		// Set the sender and recipient(s)
		client.setSender(from);
		// parse out the to addresses
		StringTokenizer st = new StringTokenizer(to.toString(), ADRESSES_SEPARATOR);
		while (st.hasMoreTokens()) {
			client.addRecipient(st.nextToken().trim());
		}
		
		System.out.println("Sending message from " + from + " to " + to );
		
		// Use the SimpleSMTPHeader class to build the header
		PrintWriter writer = new PrintWriter(client.sendMessageData());
		SimpleSMTPHeader header = new SimpleSMTPHeader( from, to, subject );
		
		// parse out the cc addresses
		if (cc != null) { 
			st = new StringTokenizer(cc, ADRESSES_SEPARATOR); 
			while (st.hasMoreTokens()) {
				header.addCC( st.nextToken() ); 
			} 
		}
		
		
		//NOTE: if would be nice to add some Maven info here
		//            header.addHeaderField("Organization", "xxx");
		
		// Write the header to the SMTP Server
		writer.write(header.toString());
		
		// Write the body of the message
		writer.write( msg );
		
		// Close the writer
		writer.close();
		if (!client.completePendingCommand()) { // failure
			throw new RuntimeException("could not send the SMTP message"); 
		}
		
		// Logout from the e-mail server (QUIT)
		client.logout();
		
		// Close the connection
		client.disconnect();	
	}
	
}

