Send email in JSP

Source: Internet
Author: User
Tags mail smtpclient
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);
}
}




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.