Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

how to send email using jsf

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have Manged bean to send Email using javaMail API , test separate successfully , in jsf i set button properties as follow h:commandButton action="Mail.send" where Mail is manged bean .


but it does not work why?
 
Ranch Hand
Posts: 42
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please support your question with your code snippet for better understanding.

Does the Managed bean method is getting called or not? If yes then that is the exception you are getting?

If you provide atleast this much information then only someone will be able to help you.
 
Ranch Hand
Posts: 56
Android MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hassan ali wrote:hi

i have Manged bean to send Email using javaMail API , test separate successfully , in jsf i set button properties as follow h:commandButton action="Mail.send" where Mail is manged bean .


but it does not work why?





Please Send code...
 
hassan ali
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks


this my bean

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class Mail {
private String sms;
private String email;
public String getSms() {
return sms;
}



public void setSms(String sms) {
this.sms = sms;
}



public String getEmail() {
return email;
}



public void setEmail(String email) {
this.email = email;
}



public String getSubject() {
return subject;
}



public void setSubject(String subject) {
this.subject = subject;
}

private String name;
private String subject;

public String send(){

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");

props.setProperty("mail.smtp.port", "26");

props.setProperty("mail.host", "mail.jerma.net");
props.setProperty("mail.user", "hassanaly@jerma.net");
props.setProperty("mail.password", "159487");

Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(true);
Transport transport;
try {
transport = mailSession.getTransport();


MimeMessage message = new MimeMessage(mailSession);
message.setSubject("HTML mail with images");
message.setFrom(new InternetAddress("hassanaly@jerma.net"));
message.setContent("<h1>"+subject+"</h1>
" +
"<h3>"+email+"</h3>
" +
"<h3>"+name+"</h3>
" +
"<h5>"+sms+"</h5>
" , "text/html");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("hassanali@jerma.net"));

transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "ok";
}



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

public String getName() {
return name;
}
}



and this my form in jsf


<body>
This and everything before will be ignored
<ui:composition>
<h:form>
<h2><span class="style11">Contact <span class="style2">Us</span> </span></h2>

Do you have a pre-sales question, need help with selecting the right price plan, or just want to drop us a note?  Simply fill out this form, and we'll respond promptly.  All e-mail inquiries receive replies within twenty-four hours, although in most cases, only minutes.


If you are interested in our discount coupon, leave us a message with your email address, and we will respond with the code.


 





<strong class="style2">Name:</strong> <label> <h:inputText required="true" value="#{mail.name}" /> </label>  
<strong class="style2">Email:</strong> <label> <h:inputText autocomplete="on" required="true" value="#{mail.email}" /> </label>  
<strong class="style2">Subject:</strong> <label> <h:inputText required="true" value="#{mail.subject}" /> </label>  
 
<strong>Detailed description of your question:</strong> Please describe your question in as much detail as you can.  The more information you can provide us with, the better equipped we will be to help you, quickly and efficiently
<h:inputTextarea rows="10" cols="40" required="true" value="#{mail.sms}"></h:inputTextarea>
<label> <h:commandButton type="submit" action="#{mail.send}" /> </label>

 


</h:form>
</ui:composition>
This and everything after will be ignored
</body>



 
Saloon Keeper
Posts: 22479
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSF does not provide any sort of email support. To use JSF to generate and send email, use the exact same code that you'd use in a non-JSF application.

In your example, I think that you were hoping to use JSF to format the email body as an HTML email. That doesn't work, however, since JSF formats what's going back to the user's display screen, not what's going out through the mail channel. However, if you do want a template-based email body generator, there are options you can use, such as Apache Velocity. Velocity has its own template language (VTL) that is similar to J2EE EL.

BTW, I have my mail client set to display mail in text-only format to cut down on malware exposures, so I do hope you'll take people like me into consideration.
 
hassan ali
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so jsf is the V in MVC and Beans are M so whey i can not use this bean to send from web application while this bean send email from my PC as Desktop Application?
if i add such function


public static void main(String arg[]){
sendmail m=new sendmail();

m.setEmail("asdasd");
m.setName("g");
m.setSms("aaaaaaaaaaaaaaaaaaaaaaa");
m.setSubject("test");
m.send();
}
it works Good why in web application does not work
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly do you mean by it does not work?

Are you getting any Exceptions thrown?

Is your "send" method, actually getting invoked on button click/submit?

It could also be that your web-server doesn't allow emailing, via firewall rules or port blocking.

WP
 
hassan ali
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no there is no error exception

this is url jerma
http://jerma.net/faces/index.xhtml;jsessionid=529A217E77D218B5D98A860784FA0D11


for contact us page i got no error message but no email send ???


 
hassan ali
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have tried to let my jsf post t servlet

servlet sends email
but
it can not read parameters
and
it can not retrieve bean from session
while
it is manged bean scope is session




 
hassan ali
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have remove servlet idea and got to basic manged bean

it run on my machine (localhost) it works fine and send email i got it.
i upload all files online in website but does not work . why?
 
William P O'Sullivan
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, It is quite possible that the server has some port restrictions.

Most web servers will block the standard mail ports to avoid spamming and misuse.

WP
 
hassan ali
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes it was configration

it was set server from mail.jerma.net to localhost
 
William P O'Sullivan
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!

Resolved then?

Pat.
 
Arunkumar Chinnadurai
Ranch Hand
Posts: 56
Android MyEclipse IDE Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hassan ali wrote:ok thanks


this my bean

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

......
</h:form>
</ui:composition>
This and everything after will be ignored
</body>





Please User CodeTag
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic