0

I am trying to implement a page where a user enters the Email address, the subject and the Message and then on send button, the message is sent together with an attachment. It seems My major Problem is calling the CommandButton to execute the Void Class Sending the Message. Here is My xhtml page:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <ui:composition template="./fros3.xhtml">
        <ui:define name="top">
        </ui:define>
        <ui:define name="content">
        </ui:define>
        <ui:define name="bottom">
            <h:form>
                <p:growl id="msg" showDetail="true" sticky="true" />
                <p:panel header="Email ScreenShot" style=" width: 50%">
                    <h:panelGrid columns="2">
                        <h:outputLabel value="E-mail address:" styleClass="requiredLbl" />
                        <p:inputText value="#{sendscreen.email}"
                                     id="username" required="true" label="username" />
                        <h:outputLabel value="Subject:" styleClass="requiredLbl" />
                        <p:inputText value="#{sendscreen.subject}"
                                     id="subject" required="true" />
                        <h:outputLabel value="Message:" styleClass="requiredLbl" />
                        <h:inputTextarea id="txt" value="#{sendscreen.message1}" required="True" style=" width: 250px; height: 100px"/>
                        <p:commandButton value="Send Email" ajax="false" action="#{sendscreen.sendemails()}" />
                        <h:panelGroup/>
                    </h:panelGrid>
                </p:panel>
            </h:form>
        </ui:define>
    </ui:composition>
</html>

My Sending Class is this(Only the Mothod).

public void sendemails() {
        try {
            System.out.println("Sending ......");
            Message message = new MimeMessage(sm.getSession());
            message.setFrom(new InternetAddress(username));
            message.setRecipient(Message.RecipientType.CC, new InternetAddress(email));
            message.setSubject(Subject);
            message.setContent(message1, "text/plain");
            BodyPart messageBodyPart = new MimeBodyPart();
            messageBodyPart.setText(message1);
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            MimeBodyPart attachmentBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource("C:/capture/screenShot.jpg");
            attachmentBodyPart.setDataHandler(new DataHandler(source));
            attachmentBodyPart.setFileName("screenShot.jpg");
            multipart.addBodyPart(attachmentBodyPart);

            Transport.send(message);
     }catch(Exception asd){
       System.out.println(asd.getMessage());
     }

I do not have any error showing on my logs and yet this method is not getting called. What is the Problem.

2

1 Answer 1

2
#{sendscreen.sendemails()}

Take off the () from the function.

Send Mails needs to return String as you're navigating away to a new page, if you want to stay on the same page have it return "" or null.

public String sendemails() {
   //do stuff
   return null;
}

In general if you don't want to navigate away from the page your best of using a actionListener rather than an action on your button submit. (Ie, remove the ajax="false") and change action to actionListener - this will also fix your problem as it will pick up your void method. (assuming you remove the () still from the action)

4
  • I have done exactly that and Nothing is happenning the Application just hangs and then stops but the method is not called Mar 20, 2014 at 8:03
  • @Stanley can you give us more information then? version of PF, the rest of the class - you're saying its hanging whats causing that?
    – VeenarM
    Mar 20, 2014 at 8:10
  • I ma using primefaces 3.5 and even the Action Listener does not call the method. Mar 20, 2014 at 8:11
  • use chrome inspector or firebug, to see if your actually going a POST or partial POST back to the server under network section - this will tell you if you're even doing anything and awaiting a response. It may be bean name related etc..
    – VeenarM
    Mar 20, 2014 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.