E-mail from Java


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: E-mail from Java

  1. #1
    JAB Guest

    E-mail from Java


    I would like to know if there is a way to send an e-mail alert from a Java
    application? I am writing a little routine that checks to see of our remote
    servers are up and running as well as our Oracle database. What I would
    like to do is have certain individuals e-mail (via our pagers) if either
    the server(s) or database is down?

    Thanks
    JAB

  2. #2
    Venkat Ramana Guest

    Re: E-mail from Java


    "JAB" <brunoj@netcarrier.com> wrote:
    >
    >I would like to know if there is a way to send an e-mail alert from a Java
    >application? I am writing a little routine that checks to see of our remote
    >servers are up and running as well as our Oracle database. What I would
    >like to do is have certain individuals e-mail (via our pagers) if either
    >the server(s) or database is down?
    >
    >Thanks
    >JAB


    Hi,
    Attached is the code for sending an email from java.
    You need JavaMail package to be installed and your CLASSPATH should
    be containing mail.jar.
    This will run on Unix machine/not tested on NT...
    /*VENKAT*/
    --------
    import java.io.*;
    import java.net.InetAddress;
    import java.util.Properties;
    import java.util.Date;

    import javax.mail.*;
    import javax.mail.internet.*;

    public class Mail {

    public static boolean send(String to, String subject, String from, String
    message) {
    boolean retValue = false;
    String mailer = "venkat";
    boolean debug = false;
    try {
    Properties props = System.getProperties();
    // ### - could use Session.getTransport() and Transport.connect()
    // ### - assume we're using SMTP
    props.put("mail.smtp.host", "localhost");

    // Get a Session object
    Session session = Session.getDefaultInstance(props, null);

    // construct the message
    Message msg = new MimeMessage(session);
    if (from != null)
    msg.setFrom(new InternetAddress(from));
    else
    msg.setFrom();

    msg.setRecipients(Message.RecipientType.TO,
    InternetAddress.parse(to, false));
    /*
    if (cc != null)
    msg.setRecipients(Message.RecipientType.CC,
    InternetAddress.parse(cc, false));
    if (bcc != null)
    msg.setRecipients(Message.RecipientType.BCC,
    InternetAddress.parse(bcc, false));
    */
    msg.setSubject(subject);
    msg.setText(message);

    msg.setHeader("X-Mailer", mailer);
    msg.setSentDate(new Date());

    // send the thing off
    Transport.send(msg);

    System.out.println("\nMail was sent successfully.");
    retValue = true;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return retValue;
    }

    public static void main(String[] args)
    {
    if(args.length == 0)
    {
    System.out.println("Usage: java Mail <to> <subject> <from> <msg>\n");
    System.exit(-1);
    }
    Mail.send(args[0], args[1],args[2], args[3]);
    }
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center