C Program to Create Multiplication Table

Multiplication table is a table of products of decimal sequence of numbers. For example, a multiplication table of 5 by 5 contains a top most row with values ranging from 1 to 5 and a left most row with values ranging from 1 to 5. The middle cells contains the algebraic product of the corresponding value in the top row and left most row. Following table is a 5×5 multiplication table,

  1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25

The following c program prints an n by n multiplication table. For example, if user inputs a value of 10, multiplication tables of all numbers from 1 to 10 are printed.

C Program to Create n by n Multiplication Table

We use the scanf and printf functions to generate multiplication table in c.

#include <stdio.h>

// Multiplication table generator
void printMultTable(int tableSize) {
  printf("      ");
  for(int i = 1; i<=tableSize;i++ ) {
    printf("%4d",i);
  }
  printf("\n");
  for(int i=0;i<tableSize;i++) {
    printf("----");
  }
  printf("--------\n");
  for(int i = 1 ;i<=tableSize;i++) {
      // print left most column first
      printf("%4d |",i);
      for(int j=1;j<=tableSize;j++) {
          printf("%4d",i*j);
      }
      printf("\n");
  }
}

int main() {
  printf("Please enter multiplication table size: ");
  int tableSize;
  scanf("%d",&tableSize);
  printMultTable(tableSize);
  return 0;
}

The output for the program for an input of 6 is given below,

Please enter multiplication table size: 6
         1   2   3   4   5   6
--------------------------------
   1 |   1   2   3   4   5   6
   2 |   2   4   6   8  10  12
   3 |   3   6   9  12  15  18
   4 |   4   8  12  16  20  24
   5 |   5  10  15  20  25  30
   6 |   6  12  18  24  30  36

Students memorize the multiplication tables of numbers from 1 to 10. Using these tables it is possible to multiply large numbers without a calculator.

C Program to Generate Multiplication Table for a Number

The following c program prints the multiplication table of a given number. This table is actually a subset of the table we created earlier. Here we print the column corresponding to the given number in the first row.

#include <stdio.h>

// Multiplication table for a number
void printTable(int number) {
  for(int i = 1; i<=10;i++ ) {
    printf("%dx%d=%d\n",i,number,i*number);
  }
}

int main() {
  printf("Please enter a number from 1 to 10: ");
  int number;
  scanf("%d",&number);
  printTable(number);
  return 0;
}

The output from the c program is given below,

Please enter a number from 1 to 10: 9
1x9=9
2x9=18
3x9=27
4x9=36
5x9=45
6x9=54
7x9=63
8x9=72
9x9=81
10x9=90

Java Byte Code Modification Using Cglib and Asm

This article looks at how cglib library can be used for java byte code manipulation at runtime. A number of popular java projects such as spring framework, guice and hibernate internally use cglib for byte code manipulation.

Byte code modification enables manipulation or creation of new classes after compilation phase of java applications. In java, classes are linked dynamically at runtime and hence it is possible to switch the compile dependency with a different implementation. For example, spring framework uses byte code manipulation to return a modified version of the original class at runtime. This allows spring framework add new features to existing classes. For example, spring AOP heavily uses this feature.

An interesting example of this behavior is java configuration classes in spring. When we write a java configuration class for beans (@Configuration and @Bean), we create new instances of the bean. However at runtime spring replaces this class with an implementation that internally reuses an existing instance for a bean (making it a singleton). Spring uses cglib (which internally uses low level asm library) for byte code manipulation. The following sections contain a number of example of byte code modification using cglib.

This example uses the following tools,

Create a simple gradle java project using eclipse. Modify the build.gradle to add the dependency on cglib 3.2.5,

apply plugin: 'application'

repositories {
    jcenter()
}

dependencies {
    compile 'cglib:cglib:3.2.5'
}

Using Cglib Enhancer Class

The cglib Enhancer class can be used to create dynamic subclasses for method interception. Note that all methods of the class are intercepted. A derived instance of Callback interface can be used handle method interception. For example, the derived interface FixedValue can be used return a fixed value instead of invoking the method of the original class. The following class demonstrates the use of Enhancer and Fixedvalue,

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.FixedValue;

public class EnhancerFixedValueDemo {

    public static void main(String[] args) {

        Enhancer er = new Enhancer();
        er.setSuperclass(SimpleClass.class);
        er.setCallback(new FixedValue() {

            @Override
            public Object loadObject() throws Exception {
                // This is called for every method call of the original class
                // We return a fixed value of the same type as the original
                // method
                return "Hello FixedValue!";
            }

        });

        SimpleClass sc = (SimpleClass) er.create();
        // We think we are using Simple Class!
        // But instead Hello FixedValue! is printed
        System.out.println(sc.getHello());

    }

}

class SimpleClass {
    public String getHello() {
        return "Hello World!";
    }
}

We can use the MethodInterceptor interface to invoke the original method call and optionally return a modified version. Check out the following example which uses Enhancer and MethodInterceptor,

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class EnhancerMethodInterceptorDemo {
    public static void main(String[] args) {

        Enhancer er = new Enhancer();
        er.setSuperclass(SimpleClass2.class);
        er.setCallback(new MethodInterceptor() {

            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                // Call the method of the original class
                Object o = proxy.invokeSuper(obj, args);

                // Take a look at what original class returned
                System.out.println("Inspecting " + o);

                // We can even modify it if needed.
                // if you have mutliple methods, return type should be checked
                // (use method.getReturnType())
                return ((String) o).toUpperCase();
            }
        });

        SimpleClass2 sc = (SimpleClass2) er.create();
        System.out.println(sc.getHello());

    }

}

class SimpleClass2 {
    public String getHello() {
        return "Hello World!";
    }
}

Using Cglib ImmutableBean

The ImmutableBean in cglib can be used to return a read only copy of a java bean. Any modification attempted will throw a runtime error. Check out the following example,

import net.sf.cglib.beans.ImmutableBean;

public class ImmutableBeanDemo {
    public static void main(String[] args) {
        SimpleBean bean = new SimpleBean();
        bean.setName("QPT");

        SimpleBean bean2 = (SimpleBean) ImmutableBean.create(bean);

        // This throws a runtime exception
        // java.lang.IllegalStateException
        bean2.setName("QPT2");

    }

}

class SimpleBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Using Cglib BeanCopier

The BeanCopier in cglib can be used to copy properties from one bean class to another bean class. The copying is done looking at the matching property name. Check out the following example,

import net.sf.cglib.beans.BeanCopier;

public class BeanCopierDemo {
    public static void main(String[] args) {
        SimpleBeanA beanA = new SimpleBeanA();
        beanA.setName("QPT");

        SimpleBeanB beanB = new SimpleBeanB();

        BeanCopier copier = BeanCopier.create(SimpleBeanA.class, SimpleBeanB.class, false);
        copier.copy(beanA, beanB, null);

        System.out.println(beanB.getName());
    }

}

class SimpleBeanA {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

class SimpleBeanB {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Spring 4 Hello World Example With Java Configuration

Spring framework is a lightweight, modular and non-invasive application framework for building java applications. Older versions of spring framework required verbose XML files for configuring java beans. Since spring 3.0, bean configuration can be specified in java code itself and is commonly know as java config(java configuration). Now it is possible to build a full fledged spring application without a single line of XML!

The following example shows how java config and annotations can be used to build a simple hello world program using spring dependency injection. This example requires the following tools,

  • Java 8
  • Spring framework 4.3.7.RELEASE or above
  • Spring Tool Suite (With Gradle 3.4) 3.8.4.RELEASE or above

Ensure that you have java 8 on your system. To verify availability of java 8, run the following command from console,

java -version

If you don’t have java 8 installed, download it from here.

Step 1: Create a simple gradle java project from spring tool suite. Delete any java classes created under src/main/java and src/test/java folders.

Step 2: Create a java class to represent our service layer. In this example, we will write a very simple service class named HelloWorldService(in demo package) with a simple method to write "Hello World!" to the console. Save HelloWorldService.java in src/main/java/demo folder. Note the use of @Service annotation which tells framework that this is a component and its lifecycle must be managed by spring IoC container.

HelloWorldService.java

package demo;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

@Service
public class HelloWorldService {
    public static final Log LOG = LogFactory.getLog(HelloWorldService.class);

    public void sayHello() {
        LOG.info("Hello World!");
    }
}

Step 3: Create a java class to represent entry point to our application. In this example, we will name it as SpringDemoApp under demo package. We will also annotate this class with @Configuration and @ComponentScan. The @Configuration tells the spring framework that SpringDemoApp contains bean definitions and @ComponentScan tells spring framework to look for beans in the project and register them automatically. Spring looks for annotations such as @Component, @Service and even @Configuration to identify classes which are managed bean components. Hence SpringDemoApp and HelloWorldService are automatically added as managed beans.

Note the use of @Autowired on the HelloWorldService member variable in SpringDemoApp. This tells the spring framework to provide an instance of HelloWorldService if it has one. Spring already knows about the HelloWorldService bean through the @Service annotation. When the main method is called, SpringDemoApp initializes the spring framework which in turn loads all the beans and processes any dependency injection required. We call the injected HelloWorldService to print hello world message on the console.

SpringDemoApp.java

package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
public class SpringDemoApp {

    // This will be created and injected by spring IoC
    @Autowired
    private HelloWorldService hello;

    public static void main(String[] args) {
        ApplicationContext context = getAppContext();

        SpringDemoApp app = context.getBean("springDemoApp", SpringDemoApp.class);
        app.runApp();
    }

    public void runApp() {
        hello.sayHello();
    }

    public static ApplicationContext getAppContext() {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringDemoApp.class);
        return context;
    }
}

The default name of a bean configured by spring is the name of the class with first letter changed to lowercase. This is why getBean uses the name "springDemoApp" to refer to SpringDemoApp bean instance. Note that we have used a package name for our classes. Component scan has issues with default package and if you create your classes without a package you may see the following error,

Caused by: java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injected

Step 4: Replace the build.gradle with the following one. We simply indicate that this is a runnable Java application with dependency on spring-context 4.3.7.RELEASE. This in turn transitively adds spring-core and spring-beans projects.

build.gradle

apply plugin: 'application'

mainClassName = "demo.SpringDemoApp"

repositories {
    jcenter()
}

dependencies {
    compile 'org.springframework:spring-context:4.3.7.RELEASE'
}

Step 5: From the gradle tasks window in STS, execute the gradle application/run task. If everything goes well, Hello World! will be printed on the console.

In the above example we have used @Configuration, but it is not needed since we haven’t programmatically defined any beans. We need it if we define our HelloWorldService using the @Bean annotation in the SpringDemoApp.java as shown below,

@Bean
public HelloWorldService getHello() {
    return new HelloWorldService();
}

In this case we can remove @Service and @ComponentScan annotations. Why? @Configuration already tells spring that SpringDemoApp is a bean and @Bean tells it that HelloWorldService is a bean. Hence we don’t need @Service and @ComponentSCan. It is also possible to use a mix of @ComponentScan and @Bean annotations in a single program.

The following picture shows the hello world project structure in spring tool suite,

spring-4-hello-world-example

How to Write a Minimal Spring Application

Spring framework and spring boot are very popular these days for building enterprise java applications. Over years spring framework ecosystem has grown considerably. Almost all infrastructural component needs are now provided by spring framework. Spring boot simplifies development further by providing a set of defaults and automatic configuration.

Under the hood, spring framework is powered by an IoC container providing dependency injection. Spring manages the dependencies between objects providing object instances automatically whenever needed. A set of libraries known as the core container provides the basic features for a spring application. Core container consists of spring-core, spring-beans, spring-context, spring-context-support and spring-expression.

The minimal set of libraries required to build a spring application using dependency injection include spring-core and spring-beans. Our aim here is to build the simplest spring application and hence we have excluded spring-context. However for any non-trivial spring application, spring-context is needed since it contains features such as annotation configuration, java configuration and an enhanced bean factory called application context.

The following hello world spring application demo uses,

  • Java 8
  • Spring framework 4.7.3 (spring-core and spring-beans)
  • Gradle 3.4
  • Spring Tool Suite IDE

Writing a Minimalist Spring Framework App

Install Spring Tool Suite (STS) and create a simple gradle java application. Add the following classes under src/main/java folder. To demonstrate the use of dependency injection, our application has two classes. SpringCoreDemoApp contains the entry point to the application (main method). It uses an instance of a class HelloWord to print a message on the console using apache commons logging (it is always available to spring applications as a transitive dependency). Since HelloWorld is created and supplied by spring, we just provide a setter and getter method for the same in SpringCoreDemoApp. Note that we are using the IoC container to provide an instance of SpringCoreDemoApp itself.

HelloWorld.java

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HelloWorld {
    public static final Log LOG = LogFactory.getLog(HelloWorld.class);

    public void sayHello() {
        LOG.info("Hello World!");
    }
}

SpringCoreDemoApp.java

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;

public class SpringCoreDemoApp {
    // This will be created and injected by spring IoC
    private HelloWorld hello;

    public HelloWorld getHello() {
        return hello;
    }

    public void setHello(HelloWorld hello) {
        this.hello = hello;
    }

    public static void main(String[] args) {
        BeanFactory beanFactory = getBeanFactory();

        SpringCoreDemoApp app = beanFactory.getBean("app", SpringCoreDemoApp.class);
        app.runApp();
    }

    public void runApp() {
        hello.sayHello();
    }

    public static BeanFactory getBeanFactory() {
        DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
        reader.loadBeanDefinitions(new ClassPathResource("beans.xml", SpringCoreDemoApp.class));
        return xbf;
    }

}

We then write a simple XML file (beans.xml) configuring all the beans that we expect spring IoC container to manage. We also specify the dependency of SpringCoreDemoApp on HelloWorld. This file needs to be in classpath and hence we create it under src/main/resources. Gradle adds this folder automatically to the classpath.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <bean id="helloWorld" class="HelloWorld">
    </bean>

    <bean id="app" class="SpringCoreDemoApp">
        <property name="hello" ref="helloWorld"></property>
    </bean>

</beans>

Finally we modify the gradle build file to specify the dependency on spring-beans. The spring-core is automatically added since it is a dependency of spring-beans. The only other library loaded through transitive dependency is apache commons logging 1.2.

build.gradle

apply plugin: 'application'

mainClassName = "SpringCoreDemoApp"

repositories {
    jcenter()
}

dependencies {
    compile 'org.springframework:spring-beans:4.3.7.RELEASE'

}

From the STS IDE, open gradle tasks window and execute the run task under application group. You will see Hello World! printed on the console. We have built a spring application using the minimal set of libraries possible!

How to Send Email From Spring Boot Applications

Spring framework provides a number of library classes for sending email. It provides an easy to use mail interface called JavaMailSender built on top of the JavaMail API. JavaMail API is a messaging framework available as part of Java SE and Java EE platforms.

Spring boot provides a starter project and auto configuration for JavaMailSender API. To use spring email features, just add the starter project spring-boot-starter-mail as a dependency on your project. Then configure the required spring.mail properties in the application.properties. With just these 2 steps, you can send email from spring boot applications.

The spring-boot-starter-mail adds the following dependent libraries to your project,

  • javax.mail
  • spring-context
  • spring-context-support
  • spring-boot-starter

Following are the configurable spring.mail properties,

  • spring.mail.host – Email server host
  • spring.mail.port – Email port
  • spring.mail.username – Email server username
  • spring.mail.password – Email server password
  • spring.mail.jndi-name – Session jndi name. If set overrides other properties
  • spring.mail.protocol – Protocol for email (smtp)
  • spring.mail.properties.* – JavaMail session properties

For a complete list of spring.mail.properties.* see this link. For example, the property spring.mail.properties.mail.smtp.timeout can be set to configure the socket read timeout.

Sending a Simple Text Email in Spring Boot Apps

Read this spring boot and sts tutorial first if you don’t know how to create spring boot applications using spring tool suite. Note that all our sample projects use gradle build system (which is superior to maven).

Let us build a simple spring boot application which sends an email using a gmail account whenever a url is accessed. For simplicity, we will write the email functionality in the controller itself.

Step 1: Create a spring boot starter project with gradle support and web starter library. See this spring boot tutorial if you are not familiar with this step.

Step 2: Configure email properties in application.properties (src/main/resources).

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=username
spring.mail.password=password
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

Step 3: Modify the build.gradle to add the spring-boot-starter-mail dependency.

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-mail')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Step 4: Write a simple controller class and use JavaMailSender and MimeMessageHelper to send simple text email.

package com.quickprogrammingtips.springboot;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SimpleEmailController {
    
    @Autowired
    private JavaMailSender sender;

    @RequestMapping("/simpleemail")
    @ResponseBody
    String home() {
        try {
            sendEmail();
            return "Email Sent!";
        }catch(Exception ex) {
            return "Error in sending email: "+ex;
        }
    }

    private void sendEmail() throws Exception{
        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        
        helper.setTo("set-your-recipient-email-here@gmail.com");
        helper.setText("How are you?");
        helper.setSubject("Hi");
        
        sender.send(message);
    }
}

Step 5: Since we are using gmail to send email, we need to enable "less secure access" on the gmail account. Log out from all the Google accounts from the browser. Login in to the gmail account configured in application.properties. Then click on this link and turn on access for less secure apps. In some cases, you may need to access this link as well! If you don’t follow these steps, you will get the following error,

org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 534-5.7.14 Please log in via your web browser and 534-5.7.14 then try again.

Less secure sign in option is required for Yahoo mail as well. This article contains configuration details for other email providers such as Yahoo mail, sendgrid and mailgun.

Step 6: Access the URL http://localhost:8080/simpleemail to send email. Check the recipient email account to verify that email is working.

Sending Email Attachments in Spring Boot Apps

The following controller sends an email with a picture attached to it. We load a cat picture from the classpath. Before running this example, ensure that you have a cat.jpg file copied to the src/main/resources folder.

package com.quickprogrammingtips.springboot;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SimpleEmailController2 {
    
    @Autowired
    private JavaMailSender sender;

    @RequestMapping("/simpleemail2")
    @ResponseBody
    String home() {
        try {
            sendEmail();
            return "Email Sent!";
        }catch(Exception ex) {
            return "Error in sending email: "+ex;
        }
    }

    private void sendEmail() throws Exception{
        MimeMessage message = sender.createMimeMessage();
        
        // Enable the multipart flag!
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        
        helper.setTo("set-your-recipient-email-here@gmail.com");
        helper.setText("How are you?");
        helper.setSubject("Hi");
        
        ClassPathResource file = new ClassPathResource("cat.jpg");
        helper.addAttachment("cat.jpg", file);
        
        sender.send(message);
    }
}

Note that the above controller is configured on a different path(/simpleemail2). Also note the multipart flag passed as the second parameter to MimeMessageHelper constructor. If this is not set to true, you will get the following error,

java.lang.IllegalStateException: Not in multipart mode – create an appropriate MimeMessageHelper via a constructor that takes a ‘multipart’ flag if you need to set alternative texts or add inline elements or attachments.

Sending Email with Inline Resources in Spring Boot Apps

Before running this example, ensure that you have a cat.jpg file copied to the src/main/resources folder. The following controller embeds the cat picture to the email as an inline resource. The recipient will see the resource embedded in the email (not as an attachment). Note that resources must be added to the helper after adding the text.

package com.quickprogrammingtips.springboot;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SimpleEmailController3 {

    @Autowired
    private JavaMailSender sender;

    @RequestMapping("/simpleemail3")
    @ResponseBody
    String home() {
        try {
            sendEmail();
            return "Email Sent!";
        } catch (Exception ex) {
            return "Error in sending email: " + ex;
        }
    }

    private void sendEmail() throws Exception {
        MimeMessage message = sender.createMimeMessage();

        // Enable the multipart flag!
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setTo("set-your-recipient-email-here@gmail.com");
        helper.setText("<html><body>Here is a cat picture! <img src='cid:id101'/><body></html>", true);
        helper.setSubject("Hi");

        ClassPathResource file = new ClassPathResource("cat.jpg");
        helper.addInline("id101", file);

        sender.send(message);
    }
}

Sending Email with Velocity Templates in Spring Boot Apps

In any non trivial email application, it is important to use templates. This enables separation of the structure of an email from the actual data that is sent in each email. For example, you can create a welcome email template and then substitute just the name placeholder in the template to send personalized email to each user. The following example uses the popular velocity templating engine to send email.

Velocity is not an actively maintained project and hence as of spring 4.3 velocity engine is deprecated in favor of freemarker. We will ignore this warning in the following program. Also note that velocity auto configuration works only till spring boot 1.4.5.RELEASE. If you are using spring boot version 1.5.0.RELEASE or above and need auto configuration, you should switch to freemarker.

Let us first create a simple velocity template for welcome message in the folder src/main/resources with the name welcome.vm.

<html>
    <body>
        <h3>Hi ${user}, welcome to the application!</h3>
    </body>
</html>

Add spring-boot-starter-velocity to the build.gradle file and change the spring boot version to 1.4.5.RELEASE,

buildscript {
    ext {
        springBootVersion = '1.4.5.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-mail')
    compile('org.springframework.boot:spring-boot-starter-velocity')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Finally let us create a controller to process velocity template and send the email,

package com.quickprogrammingtips.springboot;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class SimpleEmailController4 {

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private VelocityEngine velocityEngine;

    @RequestMapping("/simpleemail4")
    @ResponseBody
    String home() {
        try {
            sendEmail();
            return "Email Sent!";
        } catch (Exception ex) {
            return "Error in sending email: " + ex;
        }
    }

    private void sendEmail() throws Exception {
        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message);

        Map<String, Object> model = new HashMap();
        model.put("user", "qpt");
        String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "welcome.vm", model);

        helper.setTo("set-your-recipient-email-here@gmail.com");
        helper.setText(text, true); // set to html
        helper.setSubject("Hi");

        sender.send(message);
    }
}

Sending Email with FreeMarker Templates in Spring Boot Apps

Spring boot provides a starter project for using freemarker templates. Just add the spring-boot-starter-freemarker dependency to your project.

Let us start by creating a simple freemarker template named welcome.ftl in the src/main/resources folder,

<html>
    <body>
        <h3>Hi ${user}, welcome to the application!</h3>
    </body>
</html>

Modify the build.gradle to add a dependency on spring-boot-starter-freemarker. Note that we are using the latest version of spring boot (1.5.2.RELEASE),

buildscript {
    ext {
        springBootVersion = '1.5.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-mail')
    compile('org.springframework.boot:spring-boot-starter-freemarker')
 
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

Finally create a controller to process freemarker template and send an email using JavaMailSender,

package com.quickprogrammingtips.springboot;

import java.util.HashMap;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Controller
public class SimpleEmailController5 {

    @Autowired
    private JavaMailSender sender;

    @Autowired
    private Configuration freemarkerConfig;

    @RequestMapping("/simpleemail5")
    @ResponseBody
    String home() {
        try {
            sendEmail();
            return "Email Sent!";
        } catch (Exception ex) {
            return "Error in sending email: " + ex;
        }
    }

    private void sendEmail() throws Exception {
        MimeMessage message = sender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message);

        Map<String, Object> model = new HashMap();
        model.put("user", "qpt");
        
        // set loading location to src/main/resources
        // You may want to use a subfolder such as /templates here
        freemarkerConfig.setClassForTemplateLoading(this.getClass(), "/");
        
        Template t = freemarkerConfig.getTemplate("welcome.ftl");
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);

        helper.setTo("set-your-recipient-email-here@gmail.com");
        helper.setText(text, true); // set to html
        helper.setSubject("Hi");

        sender.send(message);
    }
}