在托管bean

时间:2016-03-22 11:53:09

标签: jsf jsf-2.2

我使用此Java代码在JSF页面中发送消息:

    import com.web.utils.SendEmail;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.Serializable;
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
    import javax.faces.view.ViewScoped;
    import javax.inject.Named;
    import javax.mail.MessagingException;
    import javax.servlet.http.Part;

    @Named
    @ViewScoped
    public class Contacts implements Serializable
    {
        private static final long serialVersionUID = 1L;
        private Part file;
        private String name;
        private String senderEmail;
        private String telephone;
        private String comment;
        private String result;

        public Contacts()
        {
        }

        public void sendEmail() throws IOException, MessagingException
        {
            String[] to =
            {
                "test@gmail.com"
            }; // list of recipient email addresses
            String subject = name + " " + senderEmail + " " + telephone;

            SendEmail mail = new SendEmail();
            result = mail.intiSendEmail(to, subject, comment, file);
        }

        public void upload()
        {
            if (file != null)
            {
                try (InputStream inputStream = file.getInputStream(); FileOutputStream outputStream = new FileOutputStream("D:" + File.separator + "files" + File.separator + file.getSubmittedFileName()))
                {
                    int bytesRead = 0;
                    final byte[] chunck = new byte[1024];
                    while ((bytesRead = inputStream.read(chunck)) != -1)
                    {
                        outputStream.write(chunck, 0, bytesRead);
                    }

                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload successfully ended!"));
                }
                catch (IOException e)
                {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload failed!"));
                }
            }
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public String getSenderEmail()
        {
            return senderEmail;
        }

        public void setSenderEmail(String senderEmail)
        {
            this.senderEmail = senderEmail;
        }

        public String getTelephone()
        {
            return telephone;
        }

        public void setTelephone(String telephone)
        {
            this.telephone = telephone;
        }

        public String getComment()
        {
            return comment;
        }

        public void setComment(String comment)
        {
            this.comment = comment;
        }

        public String getResult()
        {
            return result;
        }

        public void setResult(String result)
        {
            this.result = result;
        }

        public Part getFile()
        {
            return file;
        }

        public void setFile(Part file)
        {
            this.file = file;
        }
    }

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
import javax.servlet.http.Part;

public class SendEmail implements Serializable
{
    private String USER_NAME = "test";
    private String PASSWORD = "test!";

    public String intiSendEmail(String[] to, String subject, String body, Part file) throws IOException, MessagingException
    {
        String status;

        Properties props = System.getProperties();
        String host = "smtp.gmail.com";
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", USER_NAME);
        props.put("mail.smtp.password", PASSWORD);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");

        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);

        try
        {
            message.setFrom(new InternetAddress(USER_NAME));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for (int i = 0; i < to.length; i++)
            {
                toAddress[i] = new InternetAddress(to[i]);
            }

            for (int i = 0; i < toAddress.length; i++)
            {
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }

            message.setSubject(subject);
            message.setText(body);

            /**
             * If file is not attached skip setting content
             */
            if (file != null)
            {
                MimeBodyPart messageBodyPart = new MimeBodyPart();
                Multipart multipart = new MimeMultipart();

                messageBodyPart = new MimeBodyPart();
                ByteArrayDataSource ds = new ByteArrayDataSource(file.getInputStream(), file.getContentType());
                messageBodyPart.setDataHandler(new DataHandler(ds));
                messageBodyPart.setFileName(file.getName());
                multipart.addBodyPart(messageBodyPart);

                message.setContent(multipart);
            }

            Transport transport = session.getTransport("smtp");
            transport.connect(host, USER_NAME, PASSWORD);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch (AddressException ae)
        {
            ae.printStackTrace();
            return status = "Cannot send Message!";
        }
        catch (MessagingException me)
        {
            me.printStackTrace();
            return status = "Cannot send Message!";
        }

        return status = "Message is send!";
    }

    /**
     * For testing
     */
    public void upload()
    {
        Part file = null;
        if (file != null)
        {
            try (InputStream inputStream = file.getInputStream(); FileOutputStream outputStream = new FileOutputStream("D:" + File.separator + "files" + File.separator + file.getSubmittedFileName()))
            {
                int bytesRead = 0;
                final byte[] chunck = new byte[1024];
                while ((bytesRead = inputStream.read(chunck)) != -1)
                {
                    outputStream.write(chunck, 0, bytesRead);
                }

                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload successfully ended!"));
            }
            catch (IOException e)
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Upload failed!"));
            }
        }
    }
}

我添加了线程池以加快页面性能。但是我可以看到重新部署包时没有正确处理代码执行。什么是在JSF中停止线程池的正确方法?

0 个答案:

没有答案