You can send an email in JSP.

Source: Internet
Author: User
Tags smtpclient

1. We can send mail via any JSP Engine (such as jswdk) that supports sun.net. SMTP packages in Sun specifications.
(Warning: Use the built-in internal sun specification package, which will affect your JSPProgram.)

The following scriptlet uses the smtpclient class to send emails to JSP files.

<% @ 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. XXXXX. XXX ");
Client. From (from );
Client. To ();
Printstream message = client. startmessage ();
Message. println ("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. closeserver ();
}
Catch (ioexception e ){
System. Out. println ("error sending email:" + E );
}
%>

2. javamail is the official Java mail API. For details, refer to http://java.sun.com/products/javamail /. Although this API is richer or more complex than sun.net. SMTP. smtpclient, It is portable. Here, a new mailsender class is created, 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
Ms_debugging = debugging; // who (plural) the message is

// 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
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 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/plain ");

// And away
Transport. Send (ms_msg );
} Catch (exception ex ){
System. Out. println ("caught exception in mailsender. ms_send:" + ex );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.