import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
import java.io.*; 
import java.util.Properties; 
import java.sql.*;

public class Send
{ 
	private BancoInfo bi;
	private ResultSet resultSet;

	public void sendMail(String mailServer, String from, String to, String cc, String bcc,
					 String subject, String messageBody, String[] attachments) 
					 throws MessagingException, AddressException 
	{ 
		String url = "jdbc:odbc:MAIL";
		bi = new BancoInfo(url);
	
		// Setup servidor
		Properties props = System.getProperties(); 
		props.put("mail.smtp.host", mailServer);
	
		// Cria a sessão
		Session session = Session.getDefaultInstance(props, null); 
	
		// Definir nova mensagem
		Message message = new MimeMessage(session); 
		message.setFrom(new InternetAddress(from)); 
		message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false)); 
		//if(cc.equals("null"))
		//	cc = null;
		//if(bcc.equals("null"))
		//	bcc = null;
				
		if(cc != null && !cc.equals("null"))
		{
			message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false)); 
		}
		if(bcc != null && !bcc.equals("null"))
		{
			message.addRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); 
		}
		message.setSubject(subject);
		
		message.setSentDate(new java.util.Date());
		BodyPart messageBodyPart = new MimeBodyPart(); 
		messageBodyPart.setText(messageBody); 
		
		//Usa-se o MimeMultipart para adicionar anexos ao bodycorpo
		Multipart multipart = new MimeMultipart(); 
		
		//adiciona o corpo da mansagem no MIME
		multipart.addBodyPart(messageBodyPart); 
		
		// Adicionando anexos ao arquivo
		addAtachments(attachments, multipart); 
		
		// Adicionar tuda na mensagem
		message.setContent(multipart); 
		
		// Enviar mensagem
		Transport.send(message); 
				
		resultSet = bi.getQuery("SELECT MAX(id) from CX_ENVIADAS");
		int id = 0;
		try
		{
			if(resultSet.next())
			{
				id = resultSet.getInt(1);
			}
		}
		catch(Exception f)
		{}
		java.util.Vector vec = new java.util.Vector();
		for(int i = 0; i < attachments.length; i++)
		{
			vec.addElement(attachments[i]);
		}
			
		java.util.Date k = new java.util.Date();
		String hh = "" + k.getHours() + ":" + k.getMinutes() + ":" + k.getSeconds();
		java.sql.Date  dh  = new java.sql.Date(k.getTime());
		//if(cc.equals(""))
		//	cc = null;
		//if(bcc.equals(""))
		//	bcc = null;
		bi.getUpdate("INSERT into CX_ENVIADAS values(" + (id+1) + ", \'" + to + "\', \'" + 
					                                   cc + "\', \'" + bcc + "\', \'" + 
                                        			   subject + "\', \'" + messageBody + "\', \'" + 
                                        			   vec.toString() + "\', \'" + dh + " " + hh + "\')");
	} 
	
	protected void addAtachments(String[] attachments, Multipart multipart) 
								 throws MessagingException, AddressException 
	{ 
		for(int i = 0; i<= attachments.length -1; i++) 
		{ 
			String filename = attachments[i]; 
			MimeBodyPart attachmentBodyPart = new MimeBodyPart(); 
	
			//Usa-se JAF para definir o tipo MIME
			DataSource source = new FileDataSource(filename); 
			
			attachmentBodyPart.setDataHandler(new DataHandler(source)); 
	
			//Assumimos que o nome do arquivo será o mesmo do diretório atual
			attachmentBodyPart.setFileName(source.getName()); 

			//Adiciona o anexo
			multipart.addBodyPart(attachmentBodyPart); 
		} 
	} 
}
