在jsp中发送email
网络编程 2021-07-05 11:23www.168986.cn编程入门
在jsp中发送email
一、我们可以通过任何支持sun规范中的sun..smtp包的JSP引擎(如JSWDK)发送mail。
(警告:使用内置的internal Sun规范包,这将影响到你的jsp程序的可移植性。)
以下scriptlet利用SmtpClient类在jsp文件中发送email。
二、 JavaMail是官方的 Java mail API,可参考 http://java.sun./products/javamail/。虽然该API比 sun..smtp.SmtpClient更丰富或者说更复杂,但它是可移植的。这里重新创建了一个 MailSender类,它包含了 JavaMail API。如下所示
// 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, // b 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
InterAddress from = new InterAddress(strFrom);
ms_msg.setFrom(from);
// set the to
InterAddress[] address = new InterAddress[astrTo.length];
for (int i = 0; i astrTo.length; ++i)
{
address[i] = new InterAddress(astrTo[i]);
}
ms_msg.setRecipients(Message.RecipientType.TO, address);
// set the b recipients
if (astrBCC != null)
{
address = new InterAddress[astrBCC.length];
for (int i = 0; i astrBCC.length; ++i)
{
eh.dbg(\"astrBCC[\" + i + \"] is: \'\" + astrBCC[i] + \"\'\");
address[i] = new InterAddress(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);
}
}
一、我们可以通过任何支持sun规范中的sun..smtp包的JSP引擎(如JSWDK)发送mail。
(警告:使用内置的internal Sun规范包,这将影响到你的jsp程序的可移植性。)
以下scriptlet利用SmtpClient类在jsp文件中发送email。
二、 JavaMail是官方的 Java mail API,可参考 http://java.sun./products/javamail/。虽然该API比 sun..smtp.SmtpClient更丰富或者说更复杂,但它是可移植的。这里重新创建了一个 MailSender类,它包含了 JavaMail API。如下所示
// 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, // b 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
InterAddress from = new InterAddress(strFrom);
ms_msg.setFrom(from);
// set the to
InterAddress[] address = new InterAddress[astrTo.length];
for (int i = 0; i astrTo.length; ++i)
{
address[i] = new InterAddress(astrTo[i]);
}
ms_msg.setRecipients(Message.RecipientType.TO, address);
// set the b recipients
if (astrBCC != null)
{
address = new InterAddress[astrBCC.length];
for (int i = 0; i astrBCC.length; ++i)
{
eh.dbg(\"astrBCC[\" + i + \"] is: \'\" + astrBCC[i] + \"\'\");
address[i] = new InterAddress(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);
}
}
上一篇:让你的IIS服务器支持JSP
下一篇:JDBC 入门(一)
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程