Jboss3.0-Tomcat4.03下运用JavaMail发电子邮件.

类别:Java 点击:0 评论:0 推荐:
 

Operator System:    Windows 2000 server 2

Application server:  Jboss3.0-tomcat4.03

DB:                          Oracle8.17

JDK:                        jdk1.31

       Web Server           Apache1.3      

首先安装好Jboss3.0-Tomcat4.03我安装在E:\Jboss3.0_Tomcat4.03,打开E:\Jboss3.0_Tomcat4.03\server\default\deploy目录下的mail-service.xml其内容如下

<?xml version="1.0" encoding="UTF-8"?>

<server>

  <classpath codebase="lib"

             archives="mail.jar, activation.jar, mail-plugin.jar"/>

  <mbean code="org.jboss.mail.MailService"

         name="jboss:service=Mail">

    <attribute name="JNDIName">java:/Mail</attribute>

    <attribute name="User">nobody</attribute>

    <attribute name="Password">password</attribute>

    <attribute name="Configuration">

       <configuration>

          <property name="mail.store.protocol" value="pop3"/>

          <property name="mail.transport.protocol" value="smtp"/>

          <property name="mail.user" value="nobody"/>

          <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/>

          <property name="mail.smtp.host" value="smtp.nosuchhost.nosuchdomain.com"/>

          <property name="mail.from" value="[email protected]"/>

          <property name="mail.debug" value="false"/>

       </configuration>

    </attribute>

  </mbean>

</server>

我用的是www.21cn.com的免费邮箱,它是要经过SMTP认证的,所以这里是必填的

 <attribute name="User">nobody</attribute>

 <attribute name="Password">password</attribute>

修改的文件如下

<?xml version="1.0" encoding="UTF-8"?>

<server>

  <classpath codebase="lib"

             archives="mail.jar, activation.jar, mail-plugin.jar"/>

  <mbean code="org.jboss.mail.MailService"

         name="jboss:service=Mail">

    <attribute name="JNDIName">java:/TestJbossMail</attribute>

    <attribute name="User">lacklhl</attribute>//用户名

    <attribute name="Password">password</attribute>//密码

    <attribute name="Configuration">

<configuration>

<property name="mail.smtp.auth" value="true"/>        

<property name="mail.store.protocol" value="pop3"/>

                 <property name="mail.transport.protocol" value="smtp"/>

                 <property name="mail.user" value="Etre"/>

                 <property name="mail.pop3.host" value="pop3.21cn.com"/>

                 <property name="mail.smtp.host" value="smtp.21cn.com"/>

                 <property name="mail.from" value="[email protected]"/>

                 <property name="mail.debug" value="false"/>

       </configuration>

    </attribute>

  </mbean>

</server>

如果要经过Smtp认证的必须有这么样的一句

<property name="mail.smtp.auth" value="true"/> 而在Jboss提供的例子中是没有的,大家必须加上.

其实大家可以根据自己的需要进行修改的,其实SMTP认证的Java代码是这样的.

protected Session getSession()

    {

        Session mailsession=null;

        if(!getPassword().equals("")&&!getUser().equals(""))

        {

               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;

}  

下面我就写一个简单的SendMail的测试,收邮件就不做了,(我用的是jsp,在SessionBean,JavaBean中的用法基本是一样的) ,编写.war或.ear包来测试一个简易的做法就是:

1、 在deploy目录下建立sendmail-test.war目录。

2、在sendmail-test.war目录下建立WEB-INF目录,其中有两个文件

jboss-web.xml和web.xml

内容分别是

jboss-web.xml内容是

<jboss-web>

</jboss-web>

web.xml内容是

<?xml version="1.0"?>

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd">

 

<web-app>

</web-app>

三、在sendmail-test.war目录下建立新文件index.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:/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();

        }

}

%>

<HTML>

<BODY BGCOLOR="white">

<form METHOD="POST" ACTION="index.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>http://localhost:8080/sendmail-test

一切OK

这只是一个简单的例子,大家可以根据需要进行扩展,例如发Html格式的邮件,带附件的邮件等等

本文地址:http://com.8s8s.com/it/it17599.htm