DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Spring Boot Pet Clinic App: A Performance Study
  • Visually Designing Views for Java Web Apps
  • Spring Config Integration With a PCF Application: A Step-by-Step Guide

Trending

  • Your Event Storming Is Not Your Software Design
  • GET, POST, PUT, DELETE: Choosing HTTP Verbs in a Real .NET Core Web API, Not Just by the Textbook
  • Gen AI Debugging Tools Compared: Genkit vs Vercel AI SDK vs Mastra
  • Build a Voice Assistant With Gemini Live API

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Frameworks
  4. How to Activate New User Accounts by Email

How to Activate New User Accounts by Email

In a previous article, I explained how to create an activation link for new users of a web app. In this article, I'll show you how to send that link via email using SMTP.

By 
Alejandro Duarte
DZone Core CORE ·
Jun. 13, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
8.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

In a previous article, I explained how to create an activation link for new users of a Vaadin web application. In this article, I'll show you how to send that link via email using SMTP.

Adding the spring-boot-starter-mail Dependency

Spring Boot makes it easy to start sending emails from the app. We just need to add the following to the <dependencies> section of the pom.xml file:

XML
 
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>


Sending the Email Message

Now we can change the AuthService class to send a message through email instead of printing it in the console. For that, we need a MailSender instance that Spring Boot autoconfigures for us:

Java
 
@Service
public class AuthService {
    ...
    private final MailSender mailSender;

    ...
}


We can use this object in the register method to effectively send an email message:

Java
 
public void register(String email, String password) {
    User user = userRepository.save(new User(email, password, Role.USER));
    String text = "http://localhost:8080/activate?code=" + user.getActivationCode();
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("[email protected]");
    message.setSubject("Confirmation email");
    message.setText(text);
    message.setTo(email);
    mailSender.send(message);
}


Setting up a Test SMTP Server

In production, you will have to set up an SMTP server or use an existing one (like Gmail or Outlook) to send email messages. In your development environment, you can use something like FakeSMTP, a free SMTP server that incidentally is written in Java. Download it at http://nilhcem.com/FakeSMTP, run it, set the port to 9090, and start the server.

Configuring the SMTP Connection

In the application.properties file add the following connection properties:

Properties files
 
server.port=${PORT:8080}
spring.mail.host=localhost
spring.mail.port=9090


You can of course change the values to connect to your SMTP provider when you deploy the application to production (in which case you'll most likely need to add the spring.mail.password property as well).

Fixing the Dependencies Issue

If you run the app at this point, you'll get an error. This is due to a dependency conflict. To solve it, add the following exclusion to the vaadin dependency:

XML
 
<dependency>
    <groupId>com.vaadin</groupId>
    <!-- Replace artifactId with vaadin-core to use only free components -->
    <artifactId>vaadin</artifactId>
    <exclusions>
        <exclusion>
            <groupId>javax.mail</groupId>
            <artifactId>mailapi</artifactId>
        </exclusion>
    </exclusions>
</dependency>


Testing the App

That's it! To test the functionality, run the app using mvn, create a new user using the app and see the email in the FakeSMTP window.

Spring Framework app Spring Boot Activate (app) Dependency application Java (programming language) Property (programming) Testing

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Web Service Using Spring Boot 2.x
  • Spring Boot Pet Clinic App: A Performance Study
  • Visually Designing Views for Java Web Apps
  • Spring Config Integration With a PCF Application: A Step-by-Step Guide

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook