1. We can send Mail by any JSP engine (such as JSWDK) that supports Sun.Net.smtp package in the Sun specification.
(Warning: Use the built-in Internal Sun Specification package, which will affect your portability of your JSP program.)
The following Scriptlet uses the SMTPCLIENT class to send email in the JSP file.
<% @ page import = "Sun.net.smtp.smtpclient, java.io. *"%>
<%
String from = "gseshadri@hotmail.com";
String to = "govind@jguru.com, govi@bigfoot.com";
Try {
SMTPCLIENT Client = New SMTPCLIENT ("mail.xxxx.xxx");
Client.from (from);
Client.to (to);
PRINTSTREAM Message = Client.StartMessage ();
Message.println ("TO:" to);
Message.println ("Subject: Sending Email from JSP!");
Message.println ("This Was Sent From A JSP Page!");
Message.println ();
Message.println ("Cool Beans! :-));
Message.println ();
Message.Println ("Govind Seshadri");
Message.println ("JGuru.com");
Message.println ();
Client.closserver ();
}
Catch (IOException E) {
System.out.println ("Error Sending Email: E);
}
%>
Second, JavaMail is the official Java Mail API, you can refer to http://java.sun.com/products/javamail/. Although the API is more richer or more complicated than Sun.Net.smtp.smtpClient, it is portable. Here I recreated a Mailsender class, which contains the JavaMail API. As follows:
// ms_ prefix is for mailsender class variables
// Str prefix is for string
// astr prefix is for array of strings
// strbuf prefix is for StringBuffers, ETC.
Public Mailsender
String strfrom, // sender
String [] ASTRTO, // Recipient (s)
String [] astrbcc, // BCC Recipient (s), Optional
String strsubject, // Subject
Boolean Debugging)
{
MS_STRFROM = strfrom; //who the message is from
MS_ASTRTO = ASTRTO; // WHO (PLURAL) THE message is to
MS_Debugging = debugging; // w (plural) The message is to
// set the host
Properties PROPS = New Properties (); Props.Put ("mail.smtp.host", ms_strsmtphost);
// Create Some Properties and Get The Default Session
Session session = session.getDefaultInstance (Props, NULL);
Session.SetDebug (MS_Debugging);
Try {
// Create a message
MS_MSG = New MimeMessage (session);
// set the from
InternetAddress from = New InternetAddress (strfrom);
MS_MSG.SetFrom (from);
// set the to
InternetAddress [] address = new internetdress [asterto.length];
For (int i = 0; i asterto.length; i)
{
Address [i] = new internetdress (ASTRTO [i]);
}
MS_MSG.SetRecipients (Message. RecipientType.to, Address);
// set the BCC Recipients
IF (astrbcc! = null)
{
Address = new internetdress [astrbcc.length];
For (int i = 0; i astrbcc.length; i)
{
Eh.dbg ("ASTRBCC [" i "] IS: '" astrbcc [i] "" ");
Address [i] = new internetdress (astrbcc [i]);
}
MS_MSG.SetRecipients (Message.RecipientType.bcc, address);
}
// set the Subject
MS_MSG.SETSUBJECT (STRSUBJECT);
// set up the string buffer Which Will Hold The Message
MS_STRBUFMSG = New StringBuffer ();
} catch (messagingException mex) {
MEX.PrintStackTrace (System.err);
} catch (exception ex) {
EX.PrintStackTrace (System.err);
}
}
Public void ms_add (String Strtext)
{
MS_STRBUFMSG.APpend (strText);
}
Public void ms_send ()
{
Try {
// set the content as plain text
MS_MSG.SetContent (new string (ms_strbufmsg), "text / place");
// and away
Transport.send (MS_MSG);
} catch (exception ex) {
System.out.println ("Caught Exception In Mailsender.ms_send:" EX);
}
}