首先添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
测试代码
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
public class test {
public static void main(String[] args) {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
//iredMail 服务域名
mailSender.setHost( "mail.xxxxxx.com" );
mailSender.setPort( 25 );
mailSender.setProtocol( "smtp" );
//iredMail 首个邮箱用户名
mailSender.setUsername( "postmaster@xxxxxx.com" );
//iredMail 首个邮箱密码
mailSender.setPassword( "xxxxxxxxxxxxxxxxxx" );
mailSender.setDefaultEncoding( "UTF-8" );
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("postmaster@xxxxxx.com");
//发给谁
message.setTo("xxxxx@qq.com");
message.setSubject( "Verification Code");
message.setText("hello");
mailSender.send(message);
System.out.println("success");
}
}
网友评论