jueves, 13 de octubre de 2011

Ver procesos del sistema con JAVA

      Como podemos ver el "taskmgr" desde JAVA para efectuar alguna operación con un específico proceso(en mi caso el javaw.exe)


codigo:
 
 
public class Test {

    public static void main(String[] args) throws IOException {
        boolean exito = false;
        
        Process p = Runtime.getRuntime().exec(new String[]{"cmd.exe", "/C", "tasklist /FI \"STATUS eq RUNNING\""});
            DataInputStream dis = new DataInputStream(p.getInputStream());
            String x = dis.readLine();
            while (x != null) {
                if (x.startsWith("javaw.exe")) {
                    exito = true;
                    break;
                }
                x = dis.readLine();
            }
            
    }
}

martes, 11 de octubre de 2011

Enviar correo desde JAVA con JAVAMAIL

librerias:
    mail.jar
   
Mail.java

/*IMPORTAMOS LIBRERIAS*/
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author bein
 */
public class Mail {
   
    /**
     * main de prueba
     * @param args Se ignoran.
     */
    public static void main(String[] args)
    {
        try
        {
            /* PROPIEDADES DE LA CONEXIÓN */
            Properties props = new Properties();
            props.setProperty("mail.smtp.host", "smtp.grupopakar.com.mx");
            props.setProperty("mail.smtp.starttls.enable", "false");
            props.setProperty("mail.smtp.port", "25");
            props.setProperty("mail.smtp.user", "bein@<dominio>.com.mx");
            props.setProperty("mail.smtp.auth", "true");

            /* PREPARAMOS LA SESIÓN CON LAS PROPIEDADES QUE DEFINIMOS*/
            Session session = Session.getDefaultInstance(props);

            /* CONSTRUIMOS EL MENSAJE A PARTIR DE LA SESIÓN QUE CREAMOS*/
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("bein@<dominio>.com.mx"));
            message.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress("<destinatario>@<dominio>.com"));
            message.setSubject("<subject>");
            message.setText(
                "BEIN says:\n"
                + "thank you for testing this code, if you are very incognite about\n"
                + "funtionality, please check API java"
                + "thank you!!!"
                + "http://www.beinjavatomcat.blogspot.com"
                + "\n\n Atte. bein \n from: México");

            /* Lo enviamos. */
            Transport t = session.getTransport("smtp");
            t.connect("bein@<dominio>.com.mx", "<password>");
            t.sendMessage(message, message.getAllRecipients());

            /* Cierre. */
            t.close();
            System.out.println("termine, revisa tu bandeja de correo:");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}