public static void sendEmailToOne(EmailConfigEntity emailConfigEntity, EmailAndMessageTestEntity em) throws Exception {
Properties properties = new Properties();
properties.setProperty("mail.host", emailConfigEntity.getHost());
properties.setProperty("mail.smtp.port", String.valueOf(emailConfigEntity.getPort()));
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(emailConfigEntity.getFromEmail(), KettleEncr.decryptPasswd(emailConfigEntity.getAuthCode()));
}
});
session.setDebug(true);
Transport transport = session.getTransport();
transport.connect();
MimeMessage mimeMessage = new MimeMessage(session);
String nameOfSendEmail = "小毛驴";
mimeMessage.setFrom(new InternetAddress(emailConfigEntity.getFromEmail(), nameOfSendEmail));
InternetAddress email = new InternetAddress(em.getSendTo());
mimeMessage.addRecipient(Message.RecipientType.TO,email);
mimeMessage.setSubject("邮件测试");
mimeMessage.setContent(em.getContent(), "text/html;charset=UTF-8");
transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
transport.close();
}
|