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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • Immutable Secrets Management: A Zero-Trust Approach to Sensitive Data in Containers
  • FIPS 140-3: The Security Standard That Protects Our Federal Data
  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Beyond Simple Responses: Building Truly Conversational LLM Chatbots
  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 user avatar
Alejandro Duarte
DZone Core CORE ·
Jun. 13, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
7.5K Views

Join the DZone community and get the full member experience.

Join For Free

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("noreply@example.com");
    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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!