JavaEE5学习笔记04-JavaMail使用总结---4-jboss使用javamail

1.      JBoss容器配合使用

如果您是用商业化的服务器集成JavaMail,则可以通过JNDI的方式直接获取应用容器中给您提供的邮件会话Session

比如在JBoss中的${JBOSS_HOME}\server\default\deploy\mail-service.xml的内容配置如下:

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

<server>

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

         name="jboss:service=Mail">

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

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

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

    <attribute name="Configuration">

      <!-- A test configuration -->

      <configuration>

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

        <!-- 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="suhuanzheng7784877"/>

        <!-- Change to the mail server  -->

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

        <!-- Change to the SMTP gateway server -->

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

        <!-- The mail server port -->

        <property name="mail.smtp.port" value="25"/>

        <!-- Change to the address mail will be from  -->

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

        <!-- Enable debugging output from the javamail classes -->

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

      </configuration>

    </attribute>

    <depends>jboss:service=Naming</depends>

  </mbean>

</server>

以上是JBoss的邮件配置,那么在容器中调用mail时候,就可以省去了这些配置。其实不借助JBoss服务器的话,自己维护一个配置文件,将一些登录、验证信息填写进去也是可以的。

有了JBoss的代码如下:

mail.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

<title>邮件</title>

</head>

<body>

<form id="form1" name="" method="post" action="mail.jsp">

  <table width="372" height="302" border="1" cellpadding="1" cellspacing="1" bordercolor="#000000">

    <tr>

      <td colspan="2">【发送简单邮件】</td>

    </tr>

    <tr>

      <td width="118" height="25"><div align="center">收件人:</div></td>

      <td width="241" height="25"><div align="left">

        <label>

        <input name="recever" type="text" id="recever" />

        </label>

      </div></td>

    </tr>

    <tr>

      <td height="25"><div align="center">标题:</div></td>

      <td height="25"><div align="left">

        <label>

        <input name="title" type="text" id="title" />

        </label>

      </div></td>

    </tr>

    <tr>

      <td height="202"><div align="center">内容:</div></td>

      <td height="202"><div align="left">

        <label>

        <textarea name="mess" rows="15" id="mess"></textarea>

        </label>

      </div></td>

    </tr>

    <tr>

      <td colspan="2"><label>

        <input type="submit" name="Submit" value="提交" />

      </label></td>

    </tr>

  </table>

</form>

</body>

</html>

mail.jsp

<%@ page language="java"

    import="java.util.*,javax.swing.*,javax.naming.*,javax.mail.*,javax.mail.internet.*"

    pageEncoding="utf-8"%>

<%

    String JNDI = "java:/Mail";

    String PROTOCOL = "smtp";

    String MAIL_Server = "smtp.163.com";

    int PORT = 25;

    Context ctx = new InitialContext();

    Session mailSession = (Session) ctx.lookup(JNDI);

    Transport transport = mailSession.getTransport(PROTOCOL);

 

    transport.connect(MAIL_Server, PORT, "suhuanzheng7784877","1111");

 

    request.setCharacterEncoding("GBK");

 

    String recever = request.getParameter("recever");

 

    String title = request.getParameter("title");

 

    String mess = request.getParameter("mess");

 

    MimeMessage msg = new MimeMessage(mailSession);

 

    msg.setFrom(new InternetAddress([email protected]));

 

    // 设置收件人

    // 收件人可以是多个,所以是数组

    InternetAddress[] internetAddressTo = { new InternetAddress(recever) };

 

    // 目标的发送类型【直接发、抄送、暗送】

    msg.setRecipients(Message.RecipientType.TO, internetAddressTo);

 

    msg.setSubject(title);

 

    Multipart mp = new MimeMultipart();

 

    // 构造正文

    MimeBodyPart mbpContent = new MimeBodyPart();

    mbpContent.setText(mess);

 

    mp.addBodyPart(mbpContent);

 

    msg.setContent(mp);

 

    msg.setSentDate(new Date());

 

    transport.send(msg, msg.getAllRecipients());

    out.print("<h3>邮件发送成功</h3>");

是一个html提交表单到一个jsp中,jsp是处理发送邮件。通过JNDI获取Session,之后的处理和单独在客户端处理邮件一样。

你可能感兴趣的:(jsp,应用服务器,jboss,swing,XHTML)