• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vineet kumar Gupta
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18100
51
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 20986
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic