Jboss 无法将 smtp 服务器配置注入 Session 字段。
我编辑了standalone.xml 文件:
<mail-session jndi-name="java:jboss/mail/MyMail">
<smtp-server outbound-socket-binding-ref="mail-smtp-my-mail">
<login name="login" password="passwd"/>
</smtp-server>
</mail-session>
...
<socket-binding-group ...>
<outbound-socket-binding name="mail-smtp-my-mail">
<remote-destination host="my-stmp-server" port="2587"/>
</outbound-socket-binding>
我的课程有 Session 字段:
@Stateless
public class MailHelper {
@Resource(mappedName = "java:jboss/mail/MyMail")
private Session mailSession;
public String send() {
MimeMessage m = new MimeMessage(mailSession);
try {
m.setRecipients(Message.RecipientType.TO, "g@gmail.com");
Transport.send(m);
} catch (MessagingException e) {
e.printStackTrace();
}
return "...";
}
}
邮件助手用于jsp页面:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<jsp:useBean id="mailHelper" class="com.MailHelper" scope="session"/>
<%= mailHelper.send()%>
</body>
每次我在 mailSession 字段中有 null 时。怎么了?