home search customers contact
 
   

 

Razer Technology Solutions, Inc.

332 Gordon Drive
Exton, PA 19341
610-524-7073

  Sending Mail with Java <%@ taglib uri="/WEB-INF/rssutils.tld" prefix="rss" %>


Sending Mail and Instant Messages From Java Servlets
By: T.Anderson

Many basic web applications need to send mail or other communications to people based on some set of events that occur over the web. A classic example is sending an email confirmation to someone who has just completed a transaction on your website.

Sending mail from Java Servlets is very easy when using the Java Mail package. We'll start with Java Mail and then demonstrate an Instant Messaging integration as well.
Here is a small example that shows how to use the JavaMail method:

JavaMail Click Here To Give It a Try

// import the JavaMail packages
import javax.mail.*;
import javax.mail.internet.*;

// import the servlet packages
import javax.servlet.*;
import javax.servlet.http.*;

// import misc classes that we need
import java.util.*;
import java.io.*;

// simply add this method to your servlet definition.....
public void sendMail(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

// replace with client variables
String smtpServer = "your.mail.host.com";

try {

Properties properties = System.getProperties();
properties.put("your.mail.host.com", smtpServer);

// create a JavaMail session
Session session = Session.getInstance(properties, null);

// create a new MIME message
MimeMessage message = new MimeMessage(session);

// set the from address
Address fromAddress = new InternetAddress("replyto@address.com");
message.setFrom(fromAddress);

// set the to address
Address[] toAddress = InternetAddress.parse(email);
message.setRecipients(Message.RecipientType.TO, toAddress);
// set the subject
message.setSubject("Test eMail Form");

// set the message body
message.setText("text of email message");

// send the message
Transport.send(message);

}
catch (AddressException e) {
req.setAttribute("E30","Invalid email address");
errFlag++;
}
catch (SendFailedException e) {
req.setAttribute("E31","Sending of email failed");
errFlag++;
}
catch (MessagingException e) {
req.setAttribute("E32","General Messaging Exception");
errFlag++;
}

} // end function sendMail

Security Alerts
Copyright:

 



Corporate Press Releases (more)

- Software Articles
- x86 Hardware
- WebMaster Articles
- Recent Additions



Find Out About Our RFI Package. eMail: offerings@razertech.com


  home · services · about us · copyright · the eZine · contact · customer site
Copyright © Razer Technology Company 1999 - 2008. Legal Disclaimer Site Mod:1/2008.