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();
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值