In my last blog (http://productdesignsagain.blogspot.in/2015/09/user-master-data-save-application.html) we made a registration page. Now we will add email notification feature. For this we need to modify 'com.users.controller' package from previous shown project structure:
UserController.java will have a slight modification, it will create object of mail.java on save:
package com.users.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.users.bean.UserBean;
import com.users.model.User;
import com.users.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser( @ModelAttribute("user") UserBean u1, BindingResult result) {
User u = prepareModel(u1);
System.out.println("+++++++++++++"+u);
userService.addUser(u);
mail m = new mail(u1.getEmail_id()); // create mail class object
return new ModelAndView("save");
}
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcome(@ModelAttribute("user") UserBean u1, BindingResult result) {
System.out.println("1*****************");
return new ModelAndView("welcome");
}
private User prepareModel(UserBean uBean){
User u1 = new User();
u1.setPassword(uBean.getPassword());
u1.setEmail_id(uBean.getEmail_id());
u1.setUser_id(uBean.getUser_id());
uBean.setUser_id(null);
return u1;
}
}
UserController.java will have a slight modification, it will create object of mail.java on save:
package com.users.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.users.bean.UserBean;
import com.users.model.User;
import com.users.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser( @ModelAttribute("user") UserBean u1, BindingResult result) {
User u = prepareModel(u1);
System.out.println("+++++++++++++"+u);
userService.addUser(u);
mail m = new mail(u1.getEmail_id()); // create mail class object
return new ModelAndView("save");
}
@RequestMapping(value = "/welcome", method = RequestMethod.GET)
public ModelAndView welcome(@ModelAttribute("user") UserBean u1, BindingResult result) {
System.out.println("1*****************");
return new ModelAndView("welcome");
}
private User prepareModel(UserBean uBean){
User u1 = new User();
u1.setPassword(uBean.getPassword());
u1.setEmail_id(uBean.getEmail_id());
u1.setUser_id(uBean.getUser_id());
uBean.setUser_id(null);
return u1;
}
}
Mail.java
--------------------------
package com.users.controller;
import java.util.Properties;
import java.util.regex.Pattern;
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 org.apache.xerces.impl.xs.identity.Selector.Matcher;
public class mail {
mail(String m)
{
System.out.println("in mail constructor");
boolean trueBinary = false;
final String username = "< SENDING SERVER ID";
final String password = "SENDING SERVER PASSWORD";
String patt = "@(([a-zA-Z0-9_/-/.]+))*.*.";
Pattern pattern2 =Pattern.compile(patt);
java.util.regex.Matcher mat = pattern2.matcher(m);
int count = 0;
while(mat.find()) {
System.out.println("found gila bila: " + mat.group(0));
if(mat.group(0).equals("@gmail.com"))
{
System.out.println("gmail");
trueBinary = true;
}
else
trueBinary = false;
}
if(trueBinary == true)
{
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("SENDING SERVER ID"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(m));
message.setSubject("Registration Complete");
message.setText("Dear user you have successfully registered");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
else
{
String to = "sento@ta.com";//change accordingly
String from = "sendfrom@lala.com";
String host = "localhost";//or IP address
//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
//compose the message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");
// Send message
Transport.send(message);
System.out.println("message sent successfully....");
}catch (MessagingException mex) {mex.printStackTrace();}
}
}
}
import java.util.Properties;
import java.util.regex.Pattern;
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 org.apache.xerces.impl.xs.identity.Selector.Matcher;
public class mail {
mail(String m)
{
System.out.println("in mail constructor");
boolean trueBinary = false;
final String username = "< SENDING SERVER ID";
final String password = "SENDING SERVER PASSWORD";
String patt = "@(([a-zA-Z0-9_/-/.]+))*.*.";
Pattern pattern2 =Pattern.compile(patt);
java.util.regex.Matcher mat = pattern2.matcher(m);
int count = 0;
while(mat.find()) {
System.out.println("found gila bila: " + mat.group(0));
if(mat.group(0).equals("@gmail.com"))
{
System.out.println("gmail");
trueBinary = true;
}
else
trueBinary = false;
}
if(trueBinary == true)
{
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("SENDING SERVER ID"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(m));
message.setSubject("Registration Complete");
message.setText("Dear user you have successfully registered");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
else
{
String to = "sento@ta.com";//change accordingly
String from = "sendfrom@lala.com";
String host = "localhost";//or IP address
//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);
//compose the message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");
// Send message
Transport.send(message);
System.out.println("message sent successfully....");
}catch (MessagingException mex) {mex.printStackTrace();}
}
}
}