Javaは465ポートを使用してメールを送信
2867 ワード
package com.ldy.util;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
/**
* @ : EmailUtils
* @ :
* @ : lidongyang
* @ : 2017 12 20 3:32:16
*/
public class EmailUtils {
private static JavaMailSenderImpl javaMailSender;
private static final Logger logger = Logger.getLogger(EmailUtils.class);
static {
javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost(" ");//
//javaMailSender.setPort(25);// 25
javaMailSender.setUsername(" ");//
javaMailSender.setPassword(" ");//
javaMailSender.setDefaultEncoding("UTF-8");
Properties properties = new Properties();
//properties.setProperty("mail.debug", "true");//
//properties.setProperty("mail.smtp.timeout", "1000");//
// ssl 465 、 (25)
properties.setProperty("mail.smtp.auth", "true");//
properties.setProperty("mail.smtp.socketFactory.port", "465");// ssl
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
javaMailSender.setJavaMailProperties(properties);
}
/**
* @ : sendEmail
* @ :
* @ : lidongyang
* @ : 2017 12 20 3:59:15
* @param title
* @param content
* @param toMail
*/
public static void sendEmail(final String title, final String content, final String toMail) {
//
new Thread(new Runnable() {
@Override
public void run() {
if (StringUtils.isNotBlank(toMail)) {
logger.info("=================== ================");
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(userName);
mailMessage.setSubject(title);
mailMessage.setText(content);
String[] toAddress = toMail.split(",");
mailMessage.setTo(toAddress);
//
javaMailSender.send(mailMessage);
logger.info("================== ================");
}
}
}).start();
}
public static void main(String[] args) {
sendEmail(" "," ------"," ");
}
}