Spring MVC Email API Integration Process -
Write bean dependencies in .xml file and include his dependent jar file in work repository.
<!-- Spring Email Sender Bean Configuration -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.gmail.com" />
<property name="port" value="587" />
<property name="username" value="email@gmail.com" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
Write Corresponding Java Class to load
/**
* @author Wasim Ansari
* */
@Controller
public class EmailSentController {
private Logger logger = Logger.getLogger(EmailSentController.class);
public static String emailToRecipient, emailSubject, emailMessage;
@Autowired
private JavaMailSender mailSenderObj;
@Autowired
private IUserService service;
@RequestMapping(value="/restPassword", method = RequestMethod.POST)
public ModelAndView sendEmailToClient(@RequestParam String username) {
UserInfo userInformation = service.getDataByUserName(username.trim());
emailSubject = "New Mail Setup..";
emailMessage = "Welcome to email sender.";
emailToRecipient = "techa2zsolution@gmail.com";
mailSenderObj.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper mimeMsgHelperObj = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMsgHelperObj.setTo(emailToRecipient);
mimeMsgHelperObj.setFrom("email@gmail.com");
mimeMsgHelperObj.setText(emailMessage);
mimeMsgHelperObj.setSubject(emailSubject);
}
});
return null;
}
}
Write bean dependencies in .xml file and include his dependent jar file in work repository.
<!-- Spring Email Sender Bean Configuration -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.gmail.com" />
<property name="port" value="587" />
<property name="username" value="email@gmail.com" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
Write Corresponding Java Class to load
/**
* @author Wasim Ansari
* */
@Controller
public class EmailSentController {
private Logger logger = Logger.getLogger(EmailSentController.class);
public static String emailToRecipient, emailSubject, emailMessage;
@Autowired
private JavaMailSender mailSenderObj;
@Autowired
private IUserService service;
@RequestMapping(value="/restPassword", method = RequestMethod.POST)
public ModelAndView sendEmailToClient(@RequestParam String username) {
UserInfo userInformation = service.getDataByUserName(username.trim());
emailSubject = "New Mail Setup..";
emailMessage = "Welcome to email sender.";
emailToRecipient = "techa2zsolution@gmail.com";
mailSenderObj.send(new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper mimeMsgHelperObj = new MimeMessageHelper(mimeMessage, true, "UTF-8");
mimeMsgHelperObj.setTo(emailToRecipient);
mimeMsgHelperObj.setFrom("email@gmail.com");
mimeMsgHelperObj.setText(emailMessage);
mimeMsgHelperObj.setSubject(emailSubject);
}
});
return null;
}
}