This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}

}
 
Saloon Keeper
Posts: 28320
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic