javax.mail 与 JavaMailSender 使用指定版本TLSv1.2协议发送邮件

public static void main(String[] args) {
	final String username = "a@outlook.com";
	final String password = "";

	// 收件箱
	final String to = "a@qq.com";

	Properties props = new Properties();
	props.put("mail.debug", "true");
	props.put("mail.socket.debug", "true");
	props.put("mail.smtp.auth", "true");
	props.put("mail.smtp.starttls.enable", "true");
	props.put("mail.smtp.starttls.required", "true");
	props.put("mail.smtp.ssl.protocols", "TLSv1.2");

	// 不作服务器证书校验
	//props.put("mail.smtp.ssl.checkserveridentity", "false");

	// 添加信任的服务器地址,多个地址之间用空格分开
	//props.put("mail.smtp.ssl.trust", "smtp.office365.com");
	//props.put("mail.smtp.host", "smtp.office365.com");
	//props.put("mail.smtp.port", "587");

	Session session = Session.getInstance(props, new javax.mail.Authenticator() {
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(username, password);
		}
	});
	
	session.setDebug(true);
	session.setDebugOut(System.out);

	try {

		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress(username));
		message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

		message.setSubject("Email Testing Subject");
		message.setText("hello world!");

		Transport.send(message);

	} catch (MessagingException e) {
		throw new RuntimeException(e);
	}
}

}java

public static void main(String[] args) {
	
	SpringTLSEmail.simpleMailSend("a@qq.com", "测试", "消息内容");
}

public static void simpleMailSend(String email, String subject, String msg) {

	// 建立邮件发送服务器
	JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
	mailSender.setHost("smtp.office365.com");
	mailSender.setUsername("a@outlook.com");
	mailSender.setPassword("");
	mailSender.setProtocol("smtp");
	
	// 加认证机制
	Properties javaMailProperties = new Properties();

	javaMailProperties.put("mail.debug", "true");
	javaMailProperties.put("mail.smtp.auth", "true");
	javaMailProperties.put("mail.smtp.starttls.enable", "true");
	javaMailProperties.put("mail.smtp.starttls.required", "true");
	javaMailProperties.put("mail.smtp.ssl.protocols", "TLSv1.2"); //使用指定版本协议

	
	//javaMailProperties.put("mail.smtp.socketFactory.port", 587);
	//javaMailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
	//javaMailProperties.put("mail.smtp.socketFactory.fallback", "false");
	//javaMailProperties.put("mail.smtp.timeout", 5000);

	mailSender.setJavaMailProperties(javaMailProperties);

	// 建立邮件内容
	SimpleMailMessage message = new SimpleMailMessage();
	message.setFrom("a@outlook.com");
	message.setTo(email);
	message.setSubject(subject);
	message.setText(msg);

	Session session = mailSender.getSession();
	session.setDebug(true);
	session.setDebugOut(System.out);

	// 发送邮件
	mailSender.send(message);
}

#若是想看源码或者验证是否启用了TLSv1.2指定协议重要源码在这里 spring是使用的javax.mail-1.6.2.jar库 , 在这个包下 com/sun/mail/util/SocketFetcher.java 这个文件搜索一下代码即可以看到 String protocols = props.getProperty(prefix + ".ssl.protocols", null);spring

相关文章
相关标签/搜索