Giter Club home page Giter Club logo

spring-boot-mail's Introduction

spring-boot-mail Build Status codecov

Email module for sending emails.

  • Works with Groovy and Java.
  • Works with Spring Boot and regular Spring.

This will be the last email service I will ever write in my whole lifetime.

Maven Dependency

<dependency>
    <groupId>com.github.choonchernlim</groupId>
    <artifactId>spring-boot-mail</artifactId>
    <version>0.1.4</version>
</dependency>

MailBean Properties

Property Required? Description
from Yes Sender email.
tos Yes Recipient email(s).
subject Yes Subject line.
text Yes Email message.
replyTo No Email for user to reply to.

Default is value from from property.
ccs No Carbon copy emails.
bccs No Blind carbon copy emails.
attachments No Map of attachments when key is the filename and extension and value is the file.
isHtmlText No true to render message as HTML, otherwise false to render message as plain text.

Default is false.

MailService API

Method Description
send(MailBean) Sends email.
sendException(MailBean, Exception) Sends email where email message contains mailBean.text and properly formatted caught exception.
sendWebException(MailBean, Exception) Sends email where email message contains mailBean.text, properly formatted caught exception and pertinent information from HttpServletRequest object for debugging purpose.

The email message will be formatted accordingly based on mailBean.isHtmlText.

Usage

With Spring Boot

Spring Boot automatically supplies JavaMailSender if any spring.mail namespace exists.

application.yml

Typically, all you need is to specify your institution's SMTP hostname:-

spring.mail.host: [YOUR_SMTP_HOST]

... or, if you are using Google Mail:-

spring.mail:
  host: smtp.gmail.com
  username: [YOUR_EMAIL]@gmail.com
  password: [YOUR_PASSWORD]
  properties.mail.smtp:
    auth: true
    socketFactory:
      port: 465
      class: javax.net.ssl.SSLSocketFactory
      fallback: false

... but to get it working properly using Google Mail, go to https://myaccount.google.com/secureaccount first and set "Allow less secure apps" to "ON".

Spring Configuration

Create a Spring Configuration that imports SpringBootMailConfig:-

@Configuration
@Import(SpringBootMailConfig)
class AppConfig {
}

Without Spring Boot

Besides importing SpringBootMailConfig, you also have to supply JavaMailSender with all the mail properties:-

@Configuration
@Import(SpringBootMailConfig)
class AppConfig {
    @Bean
    JavaMailSender javaMailSender() {
        new JavaMailSenderImpl(
                host: [YOUR_SMTP_HOST]
        )
    }
}

Sending Mail

At the simplest level, this is all you need to send an email:-

@Autowired
MailService mailService

mailService.send(MailBean.builder().
                     from('[email protected]').
                     tos(['[email protected]'] as Set).
                     subject('subject').
                     text('text').
                     build())

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.