`

JBoss 邮件发送(普通java类)

阅读更多
JBoss 邮件发送(普通java类)2006-10-03 3:05作者:罗代均,ldj_work#126.com ,转载请保持完整性.

1.Jboss配置mailSession

打开jboss-4.0.4.GA\server\default\deploy\mail-service.xml,按如下修改

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server>
<!-- $Id: mail-service.xml,v 1.5.6.1 2005/06/01 06:20:43 starksm Exp $ -->

<server>

  <!-- ==================================================================== -->
  <!-- Mail Connection Factory                                              -->
  <!-- ==================================================================== -->

  <mbean code="org.jboss.mail.MailService"
         name="jboss:service=Mail">
    <attribute name="JNDIName">java:/Mail</attribute>
    <attribute name="User">ldj_work@126.com</attribute>
    <attribute name="Password">111111</attribute>
    <attribute name="Configuration">
       <!-- Test -->
       <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="ldj"/>

          <!-- Change to the mail server  -->
          <property name="mail.pop3.host" value="pop3.126.com"/>

          <!-- Change to the SMTP gateway server -->
          <property name="mail.smtp.host" value="smtp.126.com"/>

          <!-- Change to the address mail will be from  -->
          <property name="mail.from" value="ldj_work@126.com"/>

          <!-- Enable debugging output from the javamail classes -->
          <property name="mail.debug" value="true"/> //是否显示调试信息
       </configuration>
       <depends>jboss:service=Naming</depends>
    </attribute>
  </mbean>

</server>



2,邮件发送类

public void send(){

     try {
            InitialContext ctx = new InitialContext();
            sessions = (Session)ctx.lookup("java:/Mail")  ;            //通过jndi从jboss取mail session
                MimeMessage msg = new MimeMessage(sessions);
                msg.setFrom(new InternetAddress(ldj@126.com));                       //发件人
                msg.setRecipients(javax.mail.Message.RecipientType.TO,to);     //收件人
                msg.setSubject("邮件主题", "utf-8");                                                   //邮件主题,编码utf-8
                msg.setSentDate(new java.util.Date());
                Multipart multipt = new MimeMultipart();
                MimeBodyPart msgbody = new MimeBodyPart();
                msgbody.setContent("邮件正文", "text/html; charset=utf-8");          //邮件内容,可为html格式
                multipt.addBodyPart(msgbody);
                msg.setContent(multipt);
                Transport.send(msg);
        }catch (Exception e){
            e.printStackTrace();
        }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics