⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0037.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
    <p align="center"><big><strong>在jsp中发送email</strong></big></p>

<div align="right">(编译/Blueski)</div>
一、我们可以通过任何支持sun规范中的sun.net.smtp包的JSP引擎(如JSWDK)发送mail。 <br>
(警告:使用内置的internal Sun规范包,这将影响到你的jsp程序的可移植性。) <br>
<br>
以下scriptlet利用SmtpClient类在jsp文件中发送email。 <br>
<br>
&lt;%@ page import=&quot;sun.net.smtp.SmtpClient, java.io.*&quot; %&gt;<br>
&lt;%<br>
String from=&quot;gseshadri@hotmail.com&quot;;<br>
String to=&quot;govind@jguru.com, govi@bigfoot.com&quot;;<br>
try{<br>
SmtpClient client = new SmtpClient(&quot;mail.xxxxx.xxx&quot;);<br>
client.from(from);<br>
client.to(to);<br>
PrintStream message = client.startMessage();<br>
message.println(&quot;To: &quot; + to);<br>
message.println(&quot;Subject: Sending email from JSP!&quot;);<br>
message.println(&quot;This was sent from a JSP page!&quot;);<br>
message.println();<br>
message.println(&quot;Cool beans! :-)&quot;);<br>
message.println();<br>
message.println(&quot;Govind Seshadri&quot;);<br>
message.println(&quot;jGuru.com&quot;);<br>
message.println();<br>
client.closeServer();<br>
}<br>
catch (IOException e){ <br>
System.out.println(&quot;ERROR SENDING EMAIL:&quot;+e);<br>
}<br>
%&gt;<br>
<br>
<br>
二、 JavaMail是官方的 Java mail API,可参考 http://java.sun.com/products/javamail/。虽然该API比 sun.net.smtp.SmtpClient更丰富或者说更复杂,但它是可移植的。这里重新创建了一个 MailSender类,它包含了 JavaMail API。如下所示:<br>
<br>
<br>
// ms_ prefix is for MailSender class variables<br>
// str prefix is for String<br>
// astr prefix is for array of Strings<br>
// strbuf prefix is for StringBuffers, etc.<br>
public MailSender(<br>
String strFrom, // sender<br>
String[] astrTo, // recipient(s)<br>
String[] astrBCC, // bcc recipient(s), optional<br>
String strSubject, // subject<br>
boolean debugging)<br>
{<br>
ms_strFrom = strFrom; // who the message is from<br>
ms_astrTo = astrTo; // who (plural) the message is to<br>
ms_debugging = debugging; // who (plural) the message is to<br>
<br>
// set the host<br>
Properties props = new Properties();<br>
props.put(&quot;mail.smtp.host&quot;, ms_strSMTPHost);<br>
<br>
// create some properties and get the default Session<br>
Session session = Session.getDefaultInstance(props, null);<br>
session.setDebug(ms_debugging);<br>
<br>
try {<br>
// create a message<br>
ms_msg = new MimeMessage(session);<br>
<br>
// set the from<br>
InternetAddress from = new InternetAddress(strFrom);<br>
ms_msg.setFrom(from);<br>
<br>
// set the to<br>
InternetAddress[] address = new InternetAddress[astrTo.length];<br>
for (int i = 0; i astrTo.length; ++i)<br>
{<br>
address[i] = new InternetAddress(astrTo[i]);<br>
}<br>
ms_msg.setRecipients(Message.RecipientType.TO, address);<br>
<br>
// set the bcc recipients<br>
if (astrBCC != null)<br>
{<br>
address = new InternetAddress[astrBCC.length];<br>
for (int i = 0; i astrBCC.length; ++i)<br>
{<br>
eh.dbg(&quot;astrBCC[&quot; + i + &quot;] is: '&quot; + astrBCC[i] + &quot;'&quot;);<br>
address[i] = new InternetAddress(astrBCC[i]);<br>
}<br>
ms_msg.setRecipients(Message.RecipientType.BCC, address);<br>
}<br>
<br>
// set the subject<br>
ms_msg.setSubject(strSubject);<br>
<br>
// set up the string buffer which will hold the message<br>
ms_strbufMsg = new StringBuffer();<br>
<br>
} catch (MessagingException mex) {<br>
mex.printStackTrace(System.err);<br>
} catch (Exception ex) {<br>
ex.printStackTrace(System.err);<br>
}<br>
}<br>
<br>
public void ms_add(String strText)<br>
{<br>
ms_strbufMsg.append(strText);<br>
}<br>
<br>
public void ms_send()<br>
{<br>
try {<br>
// set the content as plain text<br>
ms_msg.setContent(new String(ms_strbufMsg), &quot;text/plain&quot;);<br>
<br>
// and away<br>
Transport.send(ms_msg);<br>
} catch (Exception ex) {<br>
System.out.println(&quot;Caught exception in MailSender.ms_send: &quot; + ex);<br>
}<br>
}

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -