阿里邮箱发送邮件 Java
实体对象 分为授权实体跟测试实体 授权实体 @Data public class EmailAuthorization { //网易163邮箱的SMTP服务器地址 smtp.qiye.aliyun.com private String host; //网易163邮箱的SMTP服务器端口 private String post; //邮箱用户名 private String username; //邮箱密码 private String password; //可不填非必要参数 private Properties properties; }
参数实体
@Data public class EmailMsgData { //接收人 private String to; //标题 private String title; //内容 private String content; }
调用方法类
import lombok.extern.slf4j.Slf4j; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.stereotype.Component; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.util.ArrayList; import java.util.List; import java.util.Properties; @Component @Slf4j public class EmailSendMsgHandle { public void sendMsgOne(EmailAuthorization emailAuthorization, EmailMsgData emailMsgData){ ArrayList<EmailMsgData> emailMsgDatas = new ArrayList<>(); emailMsgDatas.add(emailMsgData); sendMsgBatch(emailAuthorization, emailMsgDatas); } public void sendMsgBatch(EmailAuthorization emailAuthorization, List<EmailMsgData> emailMsgDatas){ log.info("发邮件"); JavaMailSenderImpl mailSender = new JavaMailSenderImpl(); if (null == emailAuthorization.getProperties()) { Properties properties = System.getProperties(); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.host", emailAuthorization.getHost());//smtp服务器地址 properties.setProperty("mail.smtp.from", emailAuthorization.getUsername());//mailfrom 参数 properties.setProperty("mail.user",emailAuthorization.getUsername());//发件人的账号 properties.setProperty("mail.password",emailAuthorization.getPassword());// 发件人的账号的密码,如果开启三方客户端安全密码请使用新生产的密码 properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.setProperty("mail.smtp.socketFactory.fallback", "false"); properties.setProperty("mail.smtp.socketFactory.port", emailAuthorization.getPost()); emailAuthorization.setProperties(properties); } mailSender.setDefaultEncoding("utf-8"); mailSender.setJavaMailProperties(emailAuthorization.getProperties()); mailSender.setHost(emailAuthorization.getHost()); mailSender.setPassword(emailAuthorization.getPassword()); mailSender.setUsername(emailAuthorization.getUsername()); mailSender.setPort(Integer.parseInt(emailAuthorization.getPost())); MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = null; try { helper = new MimeMessageHelper(message, true); // 设置发送方邮箱地址 helper.setFrom(emailAuthorization.getUsername()); for (EmailMsgData emailMsgData : emailMsgDatas) { helper.setSubject(emailMsgData.getTitle()); helper.setText(emailMsgData.getContent(), true); helper.setTo(emailMsgData.getTo()); mailSender.send(message); } } catch (MessagingException e) { e.printStackTrace(); } } }
调用
@Autowired private EmailSendMsgHandle emailSendMsgHandle;
//授权参数装配
EmailAuthorization emailAuthorization = new EmailAuthorization(); //发送参数装配 EmailMsgData emailMsgData = new EmailMsgData(); //发送 emailSendMsgHandle.sendMsgOne(emailAuthorization,emailMsgData);
示例