0

我想从 JSF 应用程序发送电子邮件。我创建了简单的 Java SE 应用程序和发送电子邮件的方法,它可以工作。所以我将此代码移至托管 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();
        }
    }
}

我有这个豆子。我都尝试了:创建 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 ...”,但仍然没有邮件。

4

1 回答 1

0

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

于 2013-04-17T19:35:54.050 回答