没有从servlet获取会话bean的实例

时间:2018-04-12 04:13:29

标签: java servlets javamail javabeans

这是我的servlet

我正在调用会话bean的方法send来发送邮件,但它没有调用它看起来好像我没有得到它的实例...

public class SendMail extends HttpServlet {

    @EJB
    emailBean myBean;   
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String email = request.getParameter("email");
        String subject = request.getParameter("subject");
        String message = request.getParameter("message");    


        String from = "xxx@gmail.com";
        String userName = "xxx";
        String password = "xxxx";           


        if(email!=null&&subject!=null&&message!=null&&myBean!=null)
        {
            myBean.Send(from, message, userName, subject, password, email);
        }
    }
}

这是我的会话bean

@Stateless

public class emailBean {

    public void Send(String from, String message, String userName, String subject, String password, String email) {         

        System.out.println("first");
        Properties props = System.getProperties();
        props.put("mail.smtp.host", "mail.smtp.gmail");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.fallback", "false");

        props.put("mail.smtp.ssl.enable", "true");
        System.out.println("hey");
        Session mailSession = Session.getDefaultInstance(props, null);
        mailSession.setDebug(true);
        Message mailMessage = new MimeMessage(mailSession);
        try {
            mailMessage.setFrom(new InternetAddress(from));

            mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
            mailMessage.setContent(message, "text/html");
            mailMessage.setSubject(subject);
            Transport transport = mailSession.getTransport("smtp");
            transport.connect("smtp.gmail.com", userName, password);
            transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        } catch (Exception e) {
            e.printStackTrace();    
        }    
    }
}

我的代码出了什么问题?

0 个答案:

没有答案