现在的位置: 首页 > 综合 > 正文

使用JBoss配置发送邮件

2013年03月23日 ⁄ 综合 ⁄ 共 4632字 ⁄ 字号 评论关闭
现在需要利用jmail 发邮件,本想自己写发邮件,但查看jboss资料,发现其已mail service 只需要做相应的配置既可配置文档位置 {jboss4.2_home}/server/default/deploy/mail-service.xml
原文
 

<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">
       
<!-- Test -->
       
<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="nobody"/>
          
<!-- Change to the mail server -->
          
<property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/>
          
<!-- Change to the SMTP gateway server -->
          
<property name="mail.smtp.host" value="smtp.nosuchhost.nosuchdomain.com"/>
          
<!-- Change to the address mail will be from -->
          
<property name="mail.from" value="nobody@nosuchhost.nosuchdomain.com"/>
          
<!-- Enable debugging output from the javamail classes -->
          
<property name="mail.debug" value="false"/>
       
</configuration>
    
</attribute>
 
</mbean>
根据自己的发送邮箱的设置,修改相应参数,如下(参考)
 

<mbean code="org.jboss.mail.MailService"
         name
="jboss:service=Mail">
    
<attribute name="JNDIName">java:/tourMail</attribute><!-- JNDI name 具体看相关资料 名 -->
    
<attribute name="User">88bye</attribute><!-- 邮箱用户名 -->
    
<attribute name="Password">******</attribute><!-- 邮箱密码 -->
    
<attribute name="Configuration">
       
<!-- Test -->
       
<configuration>
         
<!-- smtp check -->
         
<property name="mail.smtp.auth" value="true"/><!-- 这个很重要,如果邮箱服务器要smtp 验证一定要加该属性才能通过验证 -->
          
<!-- Change to your mail server prototocol -->
          
<property name="mail.store.protocol" value="pop3"/><!-- 接受协议 默认pop3-->
          
<property name="mail.transport.protocol" value="smtp"/><!-- 传输协议 默认smtp -->
          
<!-- Change to the user who will receive mail -->
          
<property name="mail.user" value="Admin"/>
          
<!-- Change to the mail server -->
          
<property name="mail.pop3.host" value="pop3.126.com"/><!-- pop3 服务器 -->
          
<!-- Change to the SMTP gateway server -->
          
<property name="mail.smtp.host" value="smtp.126.com"/><!-- smtp 服务器 -->
          
<!-- Change to the address mail will be from -->
          
<property name="mail.from" value="email@126.com"/><!-- 邮箱地址 ,与上面的用户名一致 -->
          
<!-- Enable debugging output from the javamail classes -->
          
<property name="mail.debug" value="true"/><!-- 是否打印信息 -->
       
</configuration>
    
</attribute>
 
</mbean>

配置就好了,就要看怎么应用了,一下是个测试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:/tourMail");
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="mail.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>

抱歉!评论已关闭.