Posted on 2011-06-25 22:44
贝贝爸爸 阅读(464)
评论(0) 编辑 收藏
首先,需要配置mail-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- $Id: mail-service.xml 62350 2007-04-15 16:50:12Z dimitris@jboss.org $ -->
<server>
<!-- ==================================================================== -->
<!-- Mail Connection Factory -->
<!-- ==================================================================== -->
<mbean code="org.jboss.mail.MailService"
name="jboss:service=Mail">
<attribute name="JNDIName">java:/Mail</attribute>
<attribute name="User">bpm</attribute>
<attribute name="Password">*****</attribute>
<attribute name="Configuration">
<!-- A test configuration -->
<configuration>
<!-- Change to your mail server prototocol -->
<property name="mail.store.protocol" value="pop3"/>
<property name="mail.transport.protocol" value="smtp"/>
<!-- Change to the user who will receive mail -->
<property name="mail.user" value="bpm"/>
<property name="mail.smtp.auth" value="true"/>
<!-- Change to the mail server -->
<property name="mail.pop3.host" value="**pop3服务器地址**"/>
<!-- Change to the SMTP gateway server -->
<property name="mail.smtp.host" value="***smtp服务器地址***"/>
<!-- The mail server port -->
<property name="mail.smtp.port" value="25"/>
<!-- Change to the address mail will be from -->
<property name="mail.from" value="bpm@eontime.com.cn"/>
<!-- Enable debugging output from the javamail classes -->
<property name="mail.debug" value="false"/>
</configuration>
</attribute>
<depends>jboss:service=Naming</depends>
</mbean>
</server>
其次新建一个jsp页面mail2.jsp,作为测试
<%@page contentType="text/html"%>
<%@ page import="javax.mail.*,javax.mail.internet.*, javax.activation.*, javax.naming.InitialContext" %>
<h3>Test JbsssMail DB</h3>
<%
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:/Mail");
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();
}
}
%>
<HTML>
<BODY BGCOLOR="white">
<form METHOD="POST" ACTION="mail2.jsp">
<table CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="474">
<tr>
<td width="150"><div align="left">From :</small></td>
<td width="324"><input TYPE="TEXT" name="MailFrom" value=""></td>
</tr>
<tr>
<td width="150"><div align="left">To :</small></td>
<td width="324"><input TYPE="TEXT" name="MailTo" value=""></td>
</tr>
<tr>
<td width="150"><div align="left">Subject :</small></td>
<td width="324"><input TYPE="TEXT" name="MailSubject" value=""></td>
</tr>
<tr>
<td width="150"><div align="left">Content :</small></td>
<td width="324"><TEXTAREA cols=50 name="MailContent" rows=8></TEXTAREA></td>
</tr>
<tr>
<td></td>
<td colspan="2" width="474"><input TYPE="Submit"></td>
</tr>
</table>
</form>
</BODY>
</HTML>