forked from Coded/SIPRP
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							162 lines
						
					
					
						
							3.2 KiB
						
					
					
				
			
		
		
	
	
							162 lines
						
					
					
						
							3.2 KiB
						
					
					
				| /*
 | |
|  * ServletAux.java
 | |
|  *
 | |
|  * Created on 10 de Março de 2005, 12:24
 | |
|  */
 | |
| 
 | |
| package siprp.pagina;
 | |
| 
 | |
| import java.io.*;
 | |
| import java.util.*;
 | |
| 
 | |
| import javax.mail.*;
 | |
| import javax.mail.internet.*;
 | |
| import javax.servlet.*;
 | |
| import javax.servlet.http.*;
 | |
| 
 | |
| import org.apache.velocity.*;
 | |
| import org.apache.velocity.app.*;
 | |
| /**
 | |
|  *
 | |
|  * @author  psantos
 | |
|  */
 | |
| public class MailerServlet extends HttpServlet
 | |
| {
 | |
| 	public void init()
 | |
| 	{
 | |
| 
 | |
| 		try
 | |
| 		{
 | |
| 			String TEMPLATE_DIR = this.getServletContext().getRealPath( "/" ) + "html/";
 | |
| 
 | |
| 			Properties props = new Properties();
 | |
| 			props.setProperty( "file.resource.loader.path", TEMPLATE_DIR );
 | |
| 			Velocity.init( props );
 | |
| 		}
 | |
| 		catch( Exception e )
 | |
| 		{
 | |
| 			e.printStackTrace();
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	protected String createContent( Hashtable parameters, String[] form_fields, String template )
 | |
| 	{
 | |
| 		VelocityContext context = new VelocityContext();
 | |
| 		StringWriter output = new StringWriter();
 | |
| 
 | |
| 		try
 | |
| 		{
 | |
| 			for( int i = 0; i < form_fields.length; i++ )
 | |
| 			{
 | |
| 				context.put( form_fields[ i ], parameters.get( form_fields[ i ] ) );
 | |
| 			}
 | |
| 
 | |
| 			Velocity.mergeTemplate( template, Velocity.ENCODING_DEFAULT, context, output );
 | |
| 
 | |
| 			return output.toString();
 | |
| 		}
 | |
| 		catch( Exception e )
 | |
| 		{
 | |
| 			e.printStackTrace();
 | |
| 		}
 | |
| 
 | |
| 		return null;
 | |
| 	}
 | |
| 	
 | |
| 	protected String showResultPage( boolean success, String success_template, String fail_template )
 | |
| 	{
 | |
| 		VelocityContext context = new VelocityContext();
 | |
| 		StringWriter output = new StringWriter();
 | |
| 		String template;
 | |
| 		if( success )
 | |
| 		{
 | |
| 			template = success_template;
 | |
| 		}
 | |
| 		else
 | |
| 		{
 | |
| 			template = fail_template;
 | |
| 		}
 | |
| 		try
 | |
| 		{
 | |
| 			Velocity.mergeTemplate( template, Velocity.ENCODING_DEFAULT, context, output );
 | |
| 			return output.toString();
 | |
| 		}
 | |
| 		catch( Exception e )
 | |
| 		{
 | |
| 			e.printStackTrace();
 | |
| 		}
 | |
| 
 | |
| 		return null;
 | |
| 	}
 | |
| 
 | |
| 	protected boolean sendMail( String from, String destination, String subject, String content, boolean html )
 | |
| 	{
 | |
| 		String smtp_server = "localhost";
 | |
| 		String mailer = "Evolute Mailer";
 | |
| 
 | |
| 		Properties props = System.getProperties();
 | |
| 		props.put( "mail.smtp.host", smtp_server );
 | |
| 
 | |
| 		String content_type = ( html ) ? "text/html" : "text/plain";
 | |
| 		
 | |
| 		Session session = Session.getInstance( props, null );
 | |
| 		Message msg = new MimeMessage( session );
 | |
| 		try
 | |
| 		{
 | |
| 			if( from != null )
 | |
| 			{
 | |
| 				msg.setFrom( new InternetAddress( from ) );
 | |
| 			}
 | |
| 			msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( destination, true ) );
 | |
| 			msg.setSubject( subject );
 | |
| 
 | |
| 			msg.setContent( content.toString(), content_type );
 | |
| 
 | |
| 			msg.setHeader( "X-Mailer", mailer );
 | |
| 			msg.setSentDate( new Date() );
 | |
| 			Transport.send( msg );
 | |
| 
 | |
| 			return true;
 | |
| 		}
 | |
| 		catch( Exception e )
 | |
| 		{
 | |
| 			e.printStackTrace();
 | |
| 		}
 | |
| 
 | |
| 		return false;
 | |
| 	}
 | |
| 
 | |
| 	private String checkParameter( String parameter )
 | |
| 	{
 | |
| 		if( parameter != null && parameter.trim().length() > 0 )
 | |
| 		{
 | |
| 			return parameter.trim();
 | |
| 		}
 | |
| 
 | |
| 		return null;
 | |
| 	}
 | |
| 
 | |
| 	protected Hashtable parseParameters( Hashtable parameters )
 | |
| 	{
 | |
| 		Hashtable tmp = new Hashtable();
 | |
| 		String[] element;
 | |
| 		String key;
 | |
| 		String new_element;
 | |
| 		for( Enumeration e = parameters.keys(); e.hasMoreElements(); )
 | |
| 		{
 | |
| 			key = ( String ) e.nextElement();
 | |
| 			element = ( String[] ) parameters.get( key );
 | |
| 			if( element != null )
 | |
| 			{
 | |
| 				new_element = checkParameter( element[ 0 ] );
 | |
| 				if( new_element != null )
 | |
| 				{
 | |
| 					tmp.put( key, new_element );
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return tmp;
 | |
| 	}
 | |
| }
 |