Springboot uses hutool to send mail

Inexplicable sadness 2022-05-22 12:03:56 阅读数:214

springbootuseshutoolsendmail

With 163 Mailbox example

On the local computer , The use of 25 port , Can send 、 But deploying to Alibaba cloud servers , It will be reported that the connection cannot be . Because Alibaba cloud 25 The port is banned . So here's how to use 465 Port and open ssl Mode of connection

1、 Sign in 163 mailbox . Set up -POP3/SMTP/IMAP- Opening service   Pictured , When you click Open , Will generate authorization code . It's like a password . Use Notepad to write down . Now you need to use

2、 Public class

package util;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import MailConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author sunhl
* @date 2020/10/29 10:50
*/
@Component
@Slf4j
public class MailUtils {
@Autowired
private MailConfig mailConfig;
/**
* Send a simple email
*
* @param to The recipient
* @param subject The theme
* @param content Text
*/
public void send(List<String> to, String subject, String content) {
log.info(" Mail configuration information [{}] The recipient [{}] The theme [{}] Text [{}]",mailConfig, to, subject, content);
MailAccount account = new MailAccount();
account.setHost(mailConfig.getHost());
account.setPort(mailConfig.getPort());
account.setAuth(mailConfig.isAuth());
account.setFrom(mailConfig.getFrom());
account.setUser(mailConfig.getFrom());
account.setPass(mailConfig.getPass()); // password
account.setStarttlsEnable(mailConfig.isStarttlsEnable());
account.setSslEnable(mailConfig.isSslEnable());
account.setSocketFactoryClass(mailConfig.getSocketFactoryClass());
account.setSocketFactoryFallback(mailConfig.isSocketFactoryFallback());
account.setSocketFactoryPort(mailConfig.getSocketFactoryPort());
MailUtil.send(account, to, subject, content, false);
}
}

3、 Configuration class . The use of nacos 

package config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
/**
* @author sunhl
* @date 2020/10/30 9:54
*/
@Data
@Configuration
@ConfigurationProperties("mail")
public class MailConfig {
/**
* Server address
*/
private String host;
/**
* port , Default 25
*/
private int port;
/**
* Default true
*/
private boolean auth;
/**
* Sender's email address
*/
private String from;
/**
* The sender's email authorization code
*/
private String pass;
// Turn on ssl
private boolean starttlsEnable;
private boolean sslEnable;
private String socketFactoryClass;
private boolean socketFactoryFallback;
private int socketFactoryPort;
}

4、nacos To configure

mail:
host: smtp.163.com
port: 465
auth: true
from: [email protected]
pass: EXSSEWVTETDLXYJS
starttlsEnable: true
sslEnable: true
socketFactoryClass: javax.net.ssl.SSLSocketFactory
socketFactoryFallback: false
socketFactoryPort: 465

Modify according to your own needs .

above ;

copyright:author[Inexplicable sadness],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202203140551544123.html