本文共 2891 字,大约阅读时间需要 9 分钟。
Operator System: 2000 server 2
Application server: 3.0-4.03
: 8.17
: jdk1.31
Server 1.3
首先好Jboss3.0-Tomcat4.03我安装在E:Jboss3.0_Tomcat4.03,打开E:Jboss3.0_Tomcat4.03serverdefaultdeploy目录下的-service.其内容如下
<?xml version="1.0" encoding="UTF-8"?>
archives="mail.jar, activation.jar, mail-plugin.jar"/>
name="jboss:service=Mail">
nobody
word">password
domain.com"/>
bug" value="false"/>
我用的是的免费,它是要经过SMTP的,所以这里是必填的
nobody
password
修改的如下
<?xml version="1.0" encoding="UTF-8"?>
archives="mail.jar, activation.jar, mail-plugin.jar"/>
name="jboss:service=Mail">
java:/TestJbossMail
lacklhl//名
password//密码
如果要经过Smtp认证的必须有这么样的一句
而在Jboss提供的例子中是没有的,大家必须加上.
其实大家可以根据自己的需要进行修改的,其实SMTP认证的Java代码是这样的.
protected Session getSession()
{
Session mailsession=null;
if(!getPassword().equals("")&&!getUser().equals(""))microsoft-com::office" />
{
final PasswordAuthentication pa = new PasswordAuthentication(getUser(), getPassword());
Authenticator a = new Authenticator() {
Protected PasswordAuthentication getPasswordAuthentication()
{
return pa;
}
};
Properties props = getProperties();
mailsession = Session.getInstance(props, a);
}else{
Properties props = getProperties();
mailsession = Session.getInstance(props, null);
}
return mailsession;
}
下面我就写一个简单的的测试,收就不做了,(我用的是,在SessionBean,JavaBean中的用法基本是一样的) ,编写.war或.ear包来测试一个简易的做法就是:
1、 在deploy目录下建立sendmail-test.war目录。
2、在sendmail-test.war目录下建立WEB-INF目录,其中有两个文件
jboss-web.xml和web.xml
内容分别是
jboss-web.xml内容是
web.xml内容是
<?xml version="1.0"?>
/P>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
三、在sendmail-test.war目录下建立新文件index.jsp其内容是
.NET.*, javax.activation.*, javax.naming.InitialContext" %>
String toAddress=request.getParameter("MailTo");
String fromAddress=request.getParameter("MailFrom");
String subject=request.getParameter("MailSubject");
String content=request.getParameter("MailContent");
InitialContext ctx = new InitialContext();
Session sessions = (Session) ctx.lookup("java:/TestJbossMail");
if(toAddress!=null &&!toAddress.equals("")){
try{
MimeMessage msg = new MimeMessage(sessions);
msg.setFrom(new InternetAddress(fromAddress));
msg.setRecipients(javax.mail.Message.RecipientType.TO,toAddress);
msg.setSubject(subject);
msg.setSentDate(new java.util.Date());
Multipart multipt = new MimeMultipart();
MimeBodyPart msgbody = new MimeBodyPart();
msgbody.setContent(content,"text/plain");
multipt.addBodyPart(msgbody);
msg.setContent(multipt);
Transport.send(msg);
System.out.println("SendMail OK!");
}
catch(MessagingException e)
{
e.printStackTrace();
}
}
%>
RM METHOD="POST" ACTION="index.jsp">
一切OK
这只是一个简单的例子,大家可以根据需要进行扩展,例如发Html格式的邮件,带附件的邮件等等
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10748419/viewspace-959292/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10748419/viewspace-959292/