package action.cliente; /** * * @author borjabravo */ import java.util.Properties; 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 com.opensymphony.xwork2.ActionSupport; public class EmailerAction extends ActionSupport { private String email = "proyectotelecafe@gmail.com"; private String password = "telecafe"; private String from; private String subject; private String body; private String alerta; static Properties properties = new Properties(); static { properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.socketFactory.port", "465"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.port", "465"); } public String execute() { String ret = "SUCCESS"; try { Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(email, password); } }); Message message = new MimeMessage(session); message.setFrom(new InternetAddress(email)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); message.setSubject("Contacto Telecafé: " + subject); message.setText("Enviado por: " + from + "\r\n" + body); Transport.send(message); } catch (Exception e) { ret = "ERROR"; e.printStackTrace(); } alerta = "Su email ha llegado correctamente"; return ret; } public String getAlerta() { return alerta; } public void setAlerta(String alerta) { this.alerta = alerta; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public static Properties getProperties() { return properties; } public static void setProperties(Properties properties) { EmailerAction.properties = properties; } }