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



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

我使用的是Ubuntu 12.10,带有NetBeans。因为它在其他项目(Java Se应用程序)工作完美,我想也许我必须触发这个发送不同的方式在JSP web应用程序?为什么它不能从ManagedBean工作?

这是我的类与复制方法(此方法在Java Se应用程序中工作):

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

我有这个bean。我尝试了两者:创建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>

发生了一些事情,因为我在Firefox中看到:"waiting for localhost…",但仍然没有邮件。

是的,你可以:p我只是不得不使用jsf模板

相关内容

  • 没有找到相关文章

最新更新