在jsp中发送email
一、我们可以通过任何支持sun规范中的sun.net.smtp包的JSP引擎(如JSWDK)发送mail。
(警告:使用内置的internal Sun规范包,这将影响到你的jsp程序的可移植性。)
以下scriptlet利用SmtpClient类在jsp文件中发送email。
二、 JavaMail是官方的 Java mail API,可参考 http://java.sun.com/products/javamail/。虽然该API比 sun.net.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, // 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(\%26quot;mail.smtp.host\%26quot;, 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(\%26quot;astrBCC[\%26quot; + i + \%26quot;] is: \'\%26quot; + astrBCC[i] + \%26quot;\'\%26quot;);
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), \%26quot;text/plain\%26quot;);
// and away
Transport.send(ms_msg);
} catch (Exception ex) {
System.out.println(\%26quot;Caught exception in MailSender.ms_send: \%26quot; + ex);
}
}
在jsp中发送email
2001-05-16 10:35:42 作者
相关文章
- · 用JavaMail发送带附件的Email
- · Java中使用XML创建EMAIL模板
- · 引用一个能发email的JavaMail的例子
- · 从outlook导入email地址
- · 怎样在APPLET中发EMAIL
- · 用java实现简单的email正则表达式判断
- · email address 生成图片程序
- · 很实用的一个完整email发送程序
- · 全面測試email的有效性
- · 抓取动网论坛Email地址的一段代码
- · 常用Email组件发送函数
- · 表单递交合法性检测-Email
- · 验证email地址是否合法完整实例
- · 怎样写你自己的EMAIL组件(原理)
- · 用vbscript判断email地址的合法性
- · 转换文本为超联和Email格式的代码
- · 简单的检查输入email是否合法程序
- · 检查Email地址的比较完善的正则表达式函数
- · 怎 样 在 APPLET 中 发 EMAIL
- · 用java开发Email工具之发送邮件
