Scheduled Email Sending with Spring Boot, JavaMail, and HttpClient
This tutorial demonstrates how to build a Spring Boot Maven project that fetches random sentences from an online API, configures JavaMail with POP3/SMTP credentials, and uses a scheduled task to automatically send emails, including deployment tips for Linux and Windows.
The author was inspired by a JavaScript love‑confession example and decided to implement a similar feature using Java.
The project is a Maven‑based Spring Boot application that adds mail sending, HTTP client, and scheduling capabilities.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!-- httpclient dependency -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>Before coding, the author enables POP3/SMTP service in the email provider and obtains an authorization code.
spring:
mail:
username: [email protected] # your email address
password: xxxxxxx # SMTP/POP3/IMAP authorization code
host: smtp.qq.com # mail server address
properties:
mail:
smtp:
auth: true # enable SMTP authentication
port: 587
# recipient configuration
she:
mail: [email protected]The main application class activates scheduling:
@EnableScheduling
@SpringBootApplication
public class BiaoBaiApp {
public static void main(String[] args) {
SpringApplication.run(BiaoBaiApp.class, args);
}
}A SendMessage component uses JavaMailSender to send plain‑text emails and contains a static method that retrieves a random sentence from https://chp.shadiao.app/api.php via Apache HttpClient.
@Component
public class SendMessage {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Value("${she.mail}")
private String[] sheMail;
public void sendMessage(String subject, String message) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);
helper.setTo(sheMail);
helper.setSubject(subject);
helper.setText(message);
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
e.printStackTrace();
}
}
/** Remote fetch of the message */
public static String getOneS() {
try {
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet("https://chp.shadiao.app/api.php");
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity, "utf-8");
return responseString;
} catch (IOException e) {
throw new RuntimeException("Failed to fetch sentence from website");
}
}
}A scheduled task runs daily at 17:20, obtains the sentence, and sends the email.
@Component
public class MyScheduled {
@Autowired
private SendMessage sendMessage;
// Execute every day at 17:20
@Scheduled(cron = "0 20 17 * * *")
public void dsrw() {
String message = sendMessage.getOneS();
sendMessage.sendMessage("Message from QingCha DanZhou! ❤", message);
}
}After building the jar, the author suggests opening port 587 (and optionally HTTP/HTTPS ports) on the server, then running the application in the background on Linux with nohup java -jar your.jar >test.log & or scheduling it via Windows Task Scheduler. nohup java -jar your.jar >test.log & For a richer email appearance, the author provides an optional method to send HTML content.
public void sendHtmlMessage(String subject, String message) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setFrom(from);
helper.setTo(sheMail);
helper.setSubject(subject);
helper.setText(message, true); // true enables HTML
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
e.printStackTrace();
}
}The full source code is shared via a Baidu Cloud link (https://pan.baidu.com/s/17z1Pjs1iP9CDd7IYuGohYA, extraction code: 4rxp) for readers to explore and improve.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
