1. We can send Mail by any JSP engine (such as JSWDK) that supports Sun.Net.smtp package in the Sun specification. (Warning: Using the built-in INTERNAL SUN specification package, this 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 seeshadri "); 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, // senderString [] astrTo, // recipient (s) String [] astrBCC, // bcc recipient (s), optionalString strSubject, // subjectboolean debugging) {ms_strFrom = strFrom; // who the message is fromms_astrTo = astrTo; // who (plural) the message is toms_debugging = Debugging; // WHO (PLURAL) The Message IS To
// set the hostproperties props = new profment (); props.put ("mail.smtp.host", ms_strsmtphost);
// create some properties and get the default SessionSession session = Session.getDefaultInstance (props, null); session.setDebug (ms_debugging); try {// create a messagems_msg = new MimeMessage (session);
// set the frominternetAddress from = new internetdress (strfrom); ms_msg.setfrom (from);
// set the tointernetaddress [] address = new internetdress [asterto.Length]; for (int i = 0; i astength; i) {address [i] = new internetdress (ASTRTO [i]);} MS_MSG .SetRecipients (Message.RecipientType.to, Address);
// set the bcc Recipientsif (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 subjectms_msg.setsubject (strsubject);
// set up The string buffer which will hold the messagems_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 (new string (ms_strbuff), "text / place");
// and awaytransport.send (MS_MSG);} catch (exception ex) {system.out.println ("Caught Exception In Mailsender.ms_send:" EX);}}