我可以从托管bean发送JSF中的电子邮件吗?

时间:2013-04-09 17:01:41

标签: java jsf javamail

我想从JSF应用程序发送电子邮件。我创建了简单的Java SE应用程序和方法来发送邮件,它的工作原理。所以我将此代码移动到Managed Bean,但在JSF应用程序中,这不起作用。我是Java的新手,我试图调试它但是虽然调试已经开始并且它说该站点在localhost:8080上该站点没有出现,所以我只能运行项目并单击我的按钮来验证是否收到了电子邮件收件箱中。

我在Ubuntu 12.10上使用NetBeans。 而且由于它在其他项目(Java Se应用程序)中运行良好,我想也许我必须在JSP Web应用程序中触发这种不同的发送方式? 为什么它不适用于ManagedBean?

这是我的带有复制方法的类(此方法适用于Java Se app):

import javax.mail.Address;
import javax.mail.*;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.DefaultAuthenticator;


public class myEmailSend {
    public void sendMail(){

        Email email = new SimpleEmail();
        try {
            String authuser = "me@gmail.com";
            String authpwd = "password";
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
            email.setDebug(true);
            email.setHostName("smtp.gmail.com");
            email.getMailSession().getProperties().put("mail.smtps.auth", "true");
            email.getMailSession().getProperties().put("mail.debug", "true");
            email.getMailSession().getProperties().put("mail.smtps.port", "587");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
            email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
            email.setFrom("me@gmail.com", "Agencja Ubezpieczeniowa");
            email.setSubject("TestMail");
            email.setMsg("This is a test mail3");
            email.addTo("someone@gmail.com", "ToName");
            //email.setStartTLSRequired(false);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

我有这个豆子。我尝试了两种方法:创建myEmailSend对象并使用其方法,并通过sendMail2()直接发送:这些都不起作用:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.mail.*;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;


/**
 *
 * @author root
 */
@ManagedBean
@RequestScoped
public class mySendBean {

    private myEmailSend mE;
    /**
     * Creates a new instance of mySendBean
     */
    public mySendBean() {
        mE=new myEmailSend();
    }

    public void sendMail(){
        mE.sendMail();
    }
    public void sendMail2(){


        Email email = new SimpleEmail();
        try {
            String authuser = "me@gmail.com";
            String authpwd = "password";
            email.setSmtpPort(587);
            email.setAuthenticator(new DefaultAuthenticator(authuser, authpwd));
            email.setDebug(true);
            email.setHostName("smtp.gmail.com");
            email.getMailSession().getProperties().put("mail.smtps.auth", "true");
            email.getMailSession().getProperties().put("mail.debug", "true");
            email.getMailSession().getProperties().put("mail.smtps.port", "587");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.port", "587");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.class",   "javax.net.ssl.SSLSocketFactory");
            email.getMailSession().getProperties().put("mail.smtps.socketFactory.fallback", "false");
            email.getMailSession().getProperties().put("mail.smtp.starttls.enable", "true");
            email.setFrom("me@gmail.com", "Agencja Ubezpieczeniowa");
            email.setSubject("TestMail");
            email.setMsg("This is a test mail3");
            email.addTo("someone@gmail.com", "ToName");
            //email.setStartTLSRequired(false);
            email.send();
        } catch (EmailException e) {
            e.printStackTrace();
        }
    }
}

并尝试从网站上的按钮触发它:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

    <body>
        <div>zapytaj <br></br>TODO write content<br></br></div>
        <h:form>
            <h:commandButton id="comand1" value="sendEmail" action="#{mySendBean.sendMail2()}" />
        </h:form>
    </body>
</html>
发生了一些事情,因为我看到:“等待localhost ......”在Firefox中但仍然没有邮件。

1 个答案:

答案 0 :(得分:0)

是的,你可以:p我只需要使用jsf模板