Spring mail 邮件发送的简单实现

时间:2023-11-25 19:05:08
package cn.taskSys.utils;

import java.util.Properties;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl; public class SimpleMailSend { public void sendMail(){
try {
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
// 设定mail server
senderImpl.setHost("mail.creditharmony.cn");
senderImpl.setPort(25); // 建立邮件消息
SimpleMailMessage mailMessage = new SimpleMailMessage();
// 设置收件人,寄件人 用数组发送多个邮件
String[] array = new String[] {"aaaa@creditharmony.cn","bbbb@creditharmony.cn"};
mailMessage.setTo(array);//发送
       mailMessage.setCc(array);//抄送
//mailMessage.setTo("aaaa@creditharmony.cn");
mailMessage.setFrom("PMS@creditharmony.cn");
mailMessage.setSubject("任务管理系统-任务延期提醒");
mailMessage.setText("你的任务已延期!!! "); senderImpl.setUsername("PMS"); // 根据自己的情况,设置username,username设置时应注意不要加邮箱后缀
senderImpl.setPassword("密码"); // 根据自己的情况, 设置password Properties prop = new Properties();
prop.put("mail.smtp.auth", "true"); // 将这个参数设为true,让服务器进行认证,认证用户名和密码是否正确
prop.put("mail.smtp.timeout", "25000");
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.starttls.enable", "true"); senderImpl.setJavaMailProperties(prop);
// 发送邮件
senderImpl.send(mailMessage);
} catch (MailException e) {
e.printStackTrace();
}
} public static void main(String args[]) {
SimpleMailSend simpleMailSend = new SimpleMailSend();
simpleMailSend.sendMail(); System.out.println(" 邮件发送成功.. ");
} }