Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.unilever.audit.services2; import com.unilever.audit.entities.Merchandisers; import com.unilever.audit.services.MerchandisersFacadeREST; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.Resource; import javax.ejb.Stateless; import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.Produces; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.MimeMessage; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.transaction.UserTransaction; import org.json.simple.JSONObject; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * REST Web Service * * @author Trust */ @Path("ForgetPassword") @Stateless public class ForgetPassword { @Context private UriInfo context; @PersistenceContext(unitName = "com.unilever_audit_war_1.0-SNAPSHOTPU") private EntityManager em; @Resource private UserTransaction utx; @Inject private MerchandisersFacadeREST merchandisersFacadeREST; public ForgetPassword() { } @GET @Path("Email/{email}") @Produces("application/json") public String LoginIn(@PathParam("email") String email) throws IOException { boolean status = true; String error = null; JSONObject result = new JSONObject(); Map<String, Object> hm = new HashMap<String, Object>(); hm.put("email", email); Merchandisers merchidisers = (Merchandisers) merchandisersFacadeREST .findOneByQuery("Merchandisers.findByEmail", hm); if (merchidisers == null) { status = false; error = "invalid email"; } else { Properties props = new Properties(); final String from = ""; final String password = ""; props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.user", from); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.password", password); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); session.setDebug(true); try { MimeMessage msg = new MimeMessage(session); msg.setFrom(); msg.setRecipients(Message.RecipientType.TO, email); msg.setSubject("Unilever Confirmation"); msg.setSentDate(new Date()); msg.setText(""); Transport.send(msg); } catch (MessagingException ex) { // status = false; // error="Error Sending Email"; ex.printStackTrace(); } } result.put("status", status); result.put("error", error); System.out.println("----------------" + result.toString()); return result.toString(); } }