permaculture playing cards*
The moose likes JSF and the fly likes want to send mail in jsf i am using is this code and getting error Access to defaul session denied Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "want to send mail in jsf i am using is this code and getting error Access to defaul session denied" Watch "want to send mail in jsf i am using is this code and getting error Access to defaul session denied" New topic
Author

want to send mail in jsf i am using is this code and getting error Access to defaul session denied

Vineet kumar Gupta
Greenhorn

Joined: Dec 18, 2011
Posts: 16
i have all .jar files installed at correct position...and getting ""


package jmail;

import java.util.Properties;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class mail implements ActionListener
{
public void processAction(ActionEvent event) throws AbortProcessingException
{
final String username = "vkg.ssdn@gmail.com";
final String password = "password";

Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "587");
FacesContext cont = FacesContext.getCurrentInstance();
HttpServletResponse resp=(HttpServletResponse)cont.getExternalContext().getResponse();
HttpSession ses=(HttpSession)cont.getExternalContext().getSession(true);

Session session = Session.getDefaultInstance(prop,
new javax.mail.Authenticator(){

@Override
protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(username,password);} });

try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(("vkg.ssdn@gmail.com")));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("vkg.ssdn@gmail.com"));
message.setSubject("Testing Subject");
message.setText("hello friends this is great job....,"
+ "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("done");
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}

}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16305
    
  21

If you wrap the java code using code tags (see the "Code" button on the message editor), it will make things a lot easier to read.

We really need the stack trace to be able to see more details, though.

JSF has no especial interest in JavaMail, however. If you can make it work in a stand-alone non-web application, the same code should be able to work in a JSF-initiated action.


Customer surveys are for companies who didn't pay proper attention to begin with.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Although, trying to get the default mail session in a web application doesn't seem to be the right thing to do, for some reason which I don't understand. However the problem is easily fixed by not trying to get the default session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want to send mail in jsf i am using is this code and getting error Access to defaul session denied