JAVA使用多個郵箱賬號發送郵箱
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.yml配置文件(郵箱):
spring: mail: username: 郵箱賬號@qq.com,郵箱賬號@sina.com//多個郵箱以逗號隔開 password: 123123,hfnjpxafjeigbhde//確保郵箱已經開啟SMTP服務,有授權碼則使用授權碼,沒有則使用登陸密碼 host: smtp.qq.com,smtp.afwaf.com//郵箱對應host地址 properties: mail.smtp.auth: true mail.smtp.starttls: true
MyJavaMailSenderImpl
@Configuration @EnableConfigurationProperties(MailProperties.class) @Slf4j public class MyJavaMailSenderImpl extends JavaMailSenderImpl implements JavaMailSender { private ArrayList<String> accountList = null; private ArrayList<String> passwordList = null; private ArrayList<String> hostList = null; private int currentMailId = 0; public final MailProperties properties; public MyJavaMailSenderImpl(MailProperties properties) { this.properties = properties; if (hostList == null) { hostList = new ArrayList<>(); String[] hosts = this.properties.getHost().split(",");//獲取配置文件host地址,並以,切割.添加到ArrayList中 if (hosts != null) { for (String ht : hosts) { hostList.add(ht); } } } if (accountList == null) { accountList = new ArrayList<>(); } String[] username = this.properties.getUsername().split(",");//獲取配置文件郵箱用戶名並以,切割 if (username != null) { for (String user : username) { accountList.add(user); } } if (passwordList == null) { passwordList = new ArrayList<>(); } String[] passwords = this.properties.getPassword().split(",");//獲取密碼 if (passwords != null) { for (String password : passwords) { passwordList.add(password); } } } @Override protected void doSend(MimeMessage[] mimeMessages, Object[] originalMessages) throws MailException { super.setUsername(accountList.get(currentMailId)); super.setPassword(passwordList.get(currentMailId)); super.setHost(hostList.get(currentMailId)); log.info("發送Host: --------------"+super.getHost()); log.info("發送賬號: --------------"+super.getUsername()); super.setDefaultEncoding(this.properties.getDefaultEncoding().name()); super.setJavaMailProperties(asProperties(this.properties.getProperties())); try { super.doSend(mimeMessages, originalMessages); }catch (Exception e){ log.info("doSend():異常"); e.printStackTrace(); } currentMailId = ((currentMailId + 1) % accountList.size()); } private Properties asProperties(Map<String, String> source) { Properties properties = new Properties(); properties.putAll(source); return properties; } @Override public String getUsername() { return accountList.get(currentMailId); }
MyJavaMailComponent
@Slf4j @Component public class MyJavaMailComponent { @Autowired private MyJavaMailSenderImpl myJavaMailSenderImpl; @Autowired private CrawlMapper crawlMapper; public int sendMail(String to, String text, int pid) { Map<String, Object> map = new HashMap<String, Object>(); int row = 0; map.put("email", to); try { String result = send(to, text); if (result.equals("success")) { row = crawlMapper.updateStatusByContext(pid); } boolean resu = result.equals("success") ? true : false; boolean resu2 = row > 0 ? true : false; log.info("郵箱發送結果:" + resu + ", 數據庫狀態修改:" + resu2); } catch (IOException e) { e.printStackTrace(); row = 0; } catch (MessagingException e) { e.printStackTrace(); row = 0; } return row; } public int sendMailTwoDay(String to, String text) { Map<String, Object> map = new HashMap<String, Object>(); int row = 0; map.put("email", to); try { String result = send(to, text); } catch (IOException e) { e.printStackTrace(); row = 0; } catch (MessagingException e) { e.printStackTrace(); row = 0; } return row; } private String send(String email, String text) throws MessagingException, UnsupportedEncodingException { MimeMessage message = myJavaMailSenderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8"); InternetAddress from = new InternetAddress(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日"); String date = simpleDateFormat.format(new Date()); from.setAddress(myJavaMailSenderImpl.getUsername()); from.setPersonal("我是一份警告郵件", "UTF-8"); helper.setFrom(from); helper.setTo(email); helper.setSubject(date + "-信息反饋"); helper.setText(text, true); myJavaMailSenderImpl.send(message); return "success"; }
發送方法
@Scheduled(cron = "0 0 * * * ?")//每小時的第0分0秒執行 public void sendEmail() { mailComponent.sendMail(對方郵箱, 發送內容); } }