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

Jboss3.0-Tomcat4.03下运用JavaMail发电子邮件. (转)[@more@] 

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.03serverdefaultdeploy目录下的Mail-service.XML其内容如下

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

 

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

 

  name="jboss:service=Mail">

  Java:/Mail

  nobody

  word">password

 

 

 

 

 

  domain.com"/>

 

 

  bug" value="false"/>

 

 

 

我用的是www.21cn.com的免费邮箱,它是要经过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: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;

下面我就写一个简单的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内容是

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" %>

Test JbsssMail DB

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">

 

 

 

From :

 

 

 

 

To :

 

 

 

 

Subject :

   

 

 

 

Content :

 

 

 

   

 

 

 

http://localhost:8080/sendmail-test

一切OK

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


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-998969/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-998969/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值