We can also send mails by integrating the mailing functionality with Aspect Oriented Programming (AOP). This can be used to send mails after the user registers with an application. Think of a scenario where the user receives an activation mail after registration. This can also be used to send information about an order placed on an application. Use the following steps to create a MailAdvice
class using AOP:
Create a package called
com.packt.aop
.Create a class called
MailAdvice
.public class MailAdvice { public void advice (final ProceedingJoinPoint proceedingJoinPoint) { new Thread(new Runnable() { public void run() { System.out.println("proceedingJoinPoint:"+proceedingJoinPoint); try { proceedingJoinPoint.proceed(); } catch (Throwable t) { // All we can do is log the error. System.out.println(t); } } }).start(); } }
This class creates a new thread and starts it. In the
run
method, theproceedingJoinPoint...