SpringBoot Email Sending with @Async Optimization
276 words
1 minute
SpringBoot Email Sending with @Async Optimization
SpringBoot Email Sending with @Async Optimization
Basic Configuration
1. Add the Dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId></dependency>2. Configure Mail Properties
spring: mail: password: your-auth-code host: smtp.163.comThe password field requires an authorization code, not your email login password. For 163 mail, enable POP3/SMTP/IMAP in settings and generate an auth code.
3. Send a Simple Email
@RestController@RequiredArgsConstructorpublic class EmailController {
private final JavaMailSender javaMailSender;
@GetMapping("/send-email") public String sendEmail(String email) { SimpleMailMessage message = new SimpleMailMessage(); message.setTo(email); message.setSubject("Verification Code"); message.setText("Your code is: 123456"); javaMailSender.send(message); return "ok"; }}Async Sending with @Async
Synchronous email sending blocks the request thread. For high-concurrency scenarios, async is recommended.
1. Enable Async Support
@SpringBootApplication@EnableAsyncpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}2. Async Email Service
@Component@Slf4jpublic class EmailService {
@Autowired private JavaMailSender javaMailSender;
@Autowired private StringRedisTemplate stringRedisTemplate;
@Async public void sendVerificationCode(String email) { String code = UUID.randomUUID().toString().substring(0, 6); SimpleMailMessage message = new SimpleMailMessage(); message.setTo(email); message.setSubject("Verification Code"); message.setText(code);
stringRedisTemplate.boundValueOps("code:" + email) .set(code, 5, TimeUnit.MINUTES); javaMailSender.send(message); log.info("Verification code sent to: {}", email); }}3. Custom Thread Pool
The default async thread pool has a core pool size of 8. Adjust it as needed:
spring: task: execution: pool: core-size: 50Or via Java configuration:
@Beanpublic ThreadPoolTaskExecutor threadPoolExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(20); executor.setMaxPoolSize(50); executor.setQueueCapacity(200); executor.setThreadNamePrefix("mail-executor-"); executor.initialize(); return executor;}Common @Async Pitfalls
@Async won’t work in these cases:
- The method is not
public - Return type is not
voidorFuture - The method is
static - Spring cannot scan the class (missing
@Component/@Serviceetc.) - Caller and callee are in the same class — Spring AOP proxies can’t intercept internal calls
- Creating the object with
newinstead of dependency injection - Adding
@Transactionaldirectly on an@Asyncmethod — put@Transactionalon the called method instead
Reference: https://blog.csdn.net/u011413452/article/details/124844941
Support & Share
If this article helped you, please share or support!
SpringBoot Email Sending with @Async Optimization
https://blog.zinzin.cc/posts/springboot-email-en/Related PostsSmart
1
Send SMS via Tencent Cloud API with Spring Boot
JavaA progressive guide from raw API debugging to a clean Spring Boot utility class, covering configuration property binding and static utility design.
2
Tianyi TEWA-7500V ONT: Super Password & IPv6 Firewall
NetworkingChina Mobile Tianyi TEWA-7500V (HiSilicon hsan + HGS new firmware): challenge-response login, hidden TELNET page, setObjs for telnet, read lastgood.xml for super password, hbus API to loosen IPv6 ingress — full reproducible guide.
3
Three Years After Graduation: A Second Look
EssayRevisiting the mindset shifts three years after graduation — on environment, discipline, boundaries, and what truly matters.
4
Random Thoughts: Three Years After Graduation
EssayReflections on the mindset shifts three years after graduation — about environment, discipline, and being true to oneself.
5
Docker Pull Stuck on Windows: A Proxy Configuration Horror Story
DevOpsTUN mode won't save your Docker daemon, the real config lives in settings-store.json, and dead mirror registries will eat your afternoon. A first-hand account.
Random PostsRandom













