In the jsp (SUN enterprise applications preferred) to send email

分类:Java 2010-07-12 来源:CodeWeblog.com 人气:175

In the jsp (SUN enterprise applications preferred) to send email

First, any support we can regulate the sun.net.smtp sun package jsp (SUN enterprise application of choice) engine (such as JSWDK) to send mail.

(Warning: Use the built-in internal Sun specification package, which will affect your jsp (SUN enterprise application of choice) program portability.)

The following scriptlet using SmtpClient class in jsp (SUN enterprise application of choice) file to send email.

Second, JavaMail is the official Java mail API, refer to http://java.sun.com/products/javamail/. Although the API than sun.net.smtp.SmtpClient richer or more complex, but it is portable. Here re-creates 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; / / who (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 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);

)

)

分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.383 (s). 14 q(s)