Awe galera…
estou com um problema…vamos ver se alguem poderia me ajudar…
esotu trabalhando em uma aplicação com eclipse, struts e jboss, e tenho q enviar email em determinado momento…
pesquisei aqui no forum, e estou utilizando um exemplo encontrado aqui…abaixo segue os codigos;;
GmailTest.java
package com.leilao.action.cad;
import java.security.Security;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.leilao.delegate.DominioDelegate;
import com.leilao.keys.MappingFowards;
/**
*
@author Fabricio
Cadastro de usuários
*/
public class GmailTest extends Action {
// ---------------------------------------------------------- Public Methods
/**
* Processes the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or null if the response has already been
* completed.
*
* @param mapping The <code>ActionMapping</code> used to select this instance.
* @param form The optional <code>ActionForm</code> bean for this request (if any).
* @param request The HTTP request we are processing.
* @param response The HTTP response we are creating.
* @exception Exception If some exception occurs.
* @return An <code>ActionForward</code> instance describing where and how
* control should be forwarded, or null if the response has already been
* completed.
*/
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
String forward = MappingFowards.FW_SUCCESS;
DominioDelegate dominioDelegate = new DominioDelegate();
final String SMTP_HOSTNAME = "smtp.gmail.com";
final String SMTP_PORT = "465";
final String emailMsgTxt = "Teste de envio JavaMail/Gmail";
final String emailSubjectTxt = "JavaMail GmailTest";
final String emailFromAddress = "netbusiness2007@gmail.com";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
final String[] sendTo = { "fapaso4@hotmail.com.br" };
try {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
dominioDelegate.insertEmail(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}catch (Exception e) {
System.out.println("ERRO GmailTest -> " + e.getMessage());
}
return (mapping.findForward(forward));
}
}
LoginBusiness.java
public void insertEmail(String recipients[], String subject, String message, String from) throws MessagingException,TechnicalException{
final String SMTP_HOSTNAME = "smtp.gmail.com";
final String SMTP_PORT = "465";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOSTNAME);
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");
System.out.println("teste 1");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("netbusiness2007@gmail.com", "adm123456");
}
});
System.out.println("teste 2");
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);
}
criei uma conta no gmail…com os seguinte dados:
nome do usuario: netbusiness2007@gmail.com
senha: adm123456
o erro é o seguinte:
2006-08-13 16:51:34,625 INFO [STDOUT] teste1
2006-08-13 16:51:34,625 INFO [STDOUT] ERRO GmailTest -> java.rmi.AccessException: SecurityException; nested exception is:
java.lang.SecurityException: Access to default session denied
alguem poderia me ajudar?
Tá autenticando certinho no GMail ? verifique isso.
me desculpe…mas sou meio leigo
nessa parte de email…
como posso verificar se esta autenticando corretamente?
achei q esse trecho da classe q destaquei embaixo fosse
justamente para isso…mas o erro ocorre exatamente neste
trecho como podemos perceber pelos “prints”…
System.out.println(“teste 1”);
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("netbusiness2007@gmail.com ", “adm123456”);
}
});
System.out.println(“teste 2”);
agradeço desde jah a ajuda…
mas infelizmente ainda necessito…
abraços…
[quote=“fapaso”]me desculpe…mas sou meio leigo
nessa parte de email…
como posso verificar se esta autenticando corretamente?
achei q esse trecho da classe q destaquei embaixo fosse
justamente para isso…mas o erro ocorre exatamente neste
trecho como podemos perceber pelos “prints”…
System.out.println(“teste 1”);
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("netbusiness2007@gmail.com ", “adm123456”);
}
});
System.out.println(“teste 2”);
agradeço desde jah a ajuda…
mas infelizmente ainda necessito…
abraços…[/quote]
Dá uma olhada e veja se te ajuda:
http://www.cafeh.com.br/artigos/7/index.jsp