简体   繁体   English

我可以通过托管bean在JSF中发送电子邮件吗?

[英]can I send email in JSF from managed bean?

I want to send email from JSF application. 我想从JSF应用程序发送电子邮件。 I created simple Java SE application and method to send email, it works. 我创建了简单的Java SE应用程序和发送电子邮件的方法,它可以正常工作。 So I moved this code to Managed Bean, but in JSF application this doesn't work. 所以我将这段代码移到了Managed Bean,但是在JSF应用程序中却无法正常工作。 I am new to Java, I tried to debug it but though debugging is started and it says that site is on localhost:8080 the site doesn't appear, so I only can Run project and click my button to verify if email is received on inbox. 我是Java新手,我尝试调试它,但是尽管调试已启动,但它说该站点位于localhost:8080上,所以该站点没有出现,所以我只能运行“项目”并单击我的按钮以验证是否在上收到了电子邮件。收件箱。

I am on Ubuntu 12.10, with NetBeans. 我在使用NetBeans的Ubuntu 12.10上。 And since it works perfect in other project (Java Se app) I thought maybe I have to trigger this sending different way in JSP web app? 而且,由于它在其他项目(Java Se应用程序)中运行完美,我想也许我必须在JSP Web应用程序中以其他方式触发这种发送方式吗? Why it doesn't work from ManagedBean? 为什么在ManagedBean中不起作用?

Here is my class with copied method (this method works in Java Se app): 这是我的带有复制方法的类(此方法在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();
        }
    }
}

I have this bean. 我有这个豆。 I tried both: creating myEmailSend object and using its method, and send directly via sendMail2(): none of these works: 我都尝试过:创建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();
        }
    }
}

and try to trigger it from button on site: 并尝试通过网站上的按钮触发它:

<?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>

something is happening cause I see: "waiting for localhost..." in Firefox but still no mail. 正在发生某种原因,因为我在Firefox中看到“正在等待localhost ...”,但仍然没有邮件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM