Js
We can send mail through any JSP engine (such as JSWDK) that supports the SUN.NET.SMTP package in the Sun specification.
(Warning: Use the built-in internal Sun specification package, which will affect the portability of your JSP programs.) )
The following scriptlet use the SmtpClient class to send email in a JSP file.
Second, JavaMail is the official Java mail API, can refer to http://java.sun.com/products/javamail/. Although the API is richer or more complex than sun.net.smtp.SmtpClient, it is portable. A MailSender class has been recreated here, which contains the JavaMail API. As shown below:
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's from
Ms_astrto = Astrto; The Who (plural)
ms_debugging = debugging; The Who (plural)
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 Internetaddress[astrto.length];
for (int i = 0; I astrto.length ++i)
{
Address[i] = new internetaddress (Astrto[i]);
}
Ms_msg.setrecipients (Message.RecipientType.TO, address);
Set the BCC recipients
if (ASTRBCC!= null)
{
Address = new Internetaddress[astrbcc.length];
for (int i = 0; I astrbcc.length ++i)
{
eh.dbg (\ "astrbcc[\" + i + \ "] is: \" + astrbcc[i] + \ "\");
Address[i] = new internetaddress (Astrbcc[i]);
}
Ms_msg.setrecipients (Message.RecipientType.BCC, address);
}
Set the subject
Ms_msg.setsubject (Strsubject);
Set up the string buffer which'll 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/plain\");
And Away
Transport.send (MS_MSG);
catch (Exception ex) {
System.out.println (\ "Caught exception in Mailsender.ms_send: \" + ex);
}
}