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

相關文章
相關標籤/搜索