javamail in Creator2
I am trying to get my application to send out emails, however i am unable to get it to work. I got a stand alone java program to send email through Gmail however when i plug the same exact code into Creator 2, I start receiving the following exception:
java.security.AccessControlException: access denied (java.security.SecurityPermission insertProvider.SunJSSE)
Any help would be greatly appreciated!
Here is the stand alone program that sends email correctly, I changed the email address, user name and password:
import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
publicclass GoogleTest{
privatestaticfinal String SMTP_HOST_NAME ="smtp.gmail.com";
privatestaticfinal String SMTP_PORT ="465";
privatestaticfinal String emailMsgTxt ="This email is sent from my java program";
privatestaticfinal String emailSubjectTxt ="Java email";
privatestaticfinal String emailFromAddress ="myemail@gmail.com";
privatestaticfinal String SSL_FACTORY ="javax.net.ssl.SSLSocketFactory";
privatestaticfinal String[] sendTo ={"myemail@gmail.com"};
publicstaticvoid main(String args[])throws Exception{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt,
emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
publicvoid sendSSLMessage(String recipients[], String subject,
String message, String from)throws MessagingException{
boolean debug =false;
Properties props =new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth","true");
props.put("mail.debug","true");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback","false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
returnnew PasswordAuthentication("myusername","mypassword");
}
});
session.setDebug(debug);
Message msg =new MimeMessage(session);
InternetAddress addressFrom =new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo =new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++){
addressTo[i] =new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message,"text/plain");
Transport.send(msg);
}
}
And here is the code I put into a simple Creator Project, i cut out some of the creator generated code to shorten this, also the email addresses, username and password has been changed so its not the cause of the problem as it works in the above code just fine:
package emailtest;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.rave.web.ui.component.Body;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.Head;
import com.sun.rave.web.ui.component.Html;
import com.sun.rave.web.ui.component.Link;
import com.sun.rave.web.ui.component.Page;
import javax.faces.FacesException;
import com.sun.rave.web.ui.component.TextArea;
import com.sun.rave.web.ui.component.Button;
import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.rave.web.ui.component.StaticText;
publicclass Page1extends AbstractPageBean{
// <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">
privateint __placeholder;
String SMTP_HOST_NAME ="smtp.gmail.com";
String SMTP_PORT ="465";
String emailMsgTxt;
String emailSubjectTxt ="A test from gmail";
String emailFromAddress ="myemail@gmail.com";
String SSL_FACTORY ="javax.net.ssl.SSLSocketFactory";
String[] sendTo ={"myemail@gmail.com"};
private StaticText staticText1 =new StaticText();
public StaticText getStaticText1(){
return staticText1;
}
publicvoid setStaticText1(StaticText st){
this.staticText1 = st;
}
/**
*
Construct a new Page bean instance.
*/
public Page1(){
}
/**
*
Return a reference to the scoped data bean.
*/
protected RequestBean1 getRequestBean1(){
return (RequestBean1)getBean("RequestBean1");
}
/**
*
Return a reference to the scoped data bean.
*/
protected ApplicationBean1 getApplicationBean1(){
return (ApplicationBean1)getBean("ApplicationBean1");
}
/**
*
Return a reference to the scoped data bean.
*/
protected SessionBean1 getSessionBean1(){
return (SessionBean1)getBean("SessionBean1");
}
publicvoid init(){
super.init();
// <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
// Initialize automatically managed components
// *Note* - this logic should NOT be modified
try{
_init();
}catch (Exception e){
log("Page1 Initialization Failure", e);
throw einstanceof FacesException ? (FacesException) e:new FacesException(e);
}
}
publicvoid preprocess(){
}
publicvoid prerender(){
}
publicvoid destroy(){
}
public String button1_action(){
emailMsgTxt = textArea1.getValue().toString();
try{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
this.sendSSLMessage(sendTo, emailSubjectTxt,
emailMsgTxt, emailFromAddress);
staticText1.setValue("Sucessfully Sent mail to All Users");
}catch(Exception e){
staticText1.setValue(e);
}
returnnull;
}
publicvoid sendSSLMessage(String recipients[], String subject,
String message, String from)throws MessagingException{
boolean debug =false;
Properties props =new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth","true");
props.put("mail.debug","false");
props.put("mail.smtp.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.port", SMTP_PORT);
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
props.put("mail.smtp.socketFactory.fallback","false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
returnnew PasswordAuthentication("myusername","mypassword");
}
}
);
session.setDebug(debug);
Message msg =new MimeMessage(session);
InternetAddress addressFrom =new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo =new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++){
addressTo[i] =new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message,"text/plain");
Transport.send(msg);
}
}

