-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks