Sending email using Spring MVC via gmail or company’s email

Thursday, December 10th, 2015 (1 Comment)

This article, we will develop simply spring project sending email use gmail or company’s email.

We will focus on configuration file for many type email. Simple page input information and page result as below:

page email

page email

Step 1: create project with following image:

project struct

project struct

Step 2: add maven email library and config files of spring

porn.xml:

servlet-context.xml:

servlet-gmail.xml: have 2 way sending email

If using host is gmail:

If using host is company’s email

  • line 12: username to log in mail server (exam: admin)
  • line 16: mail server (exam: mail.company.com)
  • line 17: number of port
  • line 18: email of username (exam: [email protected])
  • line 22: for server use mail SSL

web.xml:

  • Line 31 – 36: config pageEncoding=UTF-8 for all jsp page, you can not fix pageEncoding for each page.
  • Line 38 – 53: config load utf-8 in spring mvc

Step 3: modify java class

EmailInfo.java: storing email’s information

EmailController.java: handle page input and result

If using gmail host

  • This class using with host is gmail and config file of gmail above

If using company’s email

a) Using javax.mail.Transport library mail.jar

  • this class using with config file of company’s email
  • line 52: need create session to authenticate to email server

b) Using JavaMailSender of springframework

replace sendWithAttach method by method below:

Step 4: edit page input and result

AttachEmailInput.jsp:

AttachEmailSuccess.jsp:

index.jsp: move default web to page input

Build and run project, will have result the same the first image.

You can integrated upload file (upload file project) with sending email, using addAttachment function of MimeMessageHelper library.

Tags: , , , ,

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

Related Posts

spring
How to create quartz spring dynamic in database
spring
Spring mvc mongodb crud users roles
spring
Java spring mvc tutorial on lvtutorial.com
spring
Angularjs spring mvc crud example

One Comment:

Leave a Reply

Your email address will not be published. Required fields are marked *