javamail發(fā)送qq郵箱失敗的原因及解決
javaMail報錯:Unsupported or unrecognized SSL message
c.n.m.service.impl.EmailServiceImpl : 郵件發(fā)送異常, Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unsupported or unrecognized SSL message. Failed messages: javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
原因分析: ssl與tls端口
坑點:官方文檔發(fā)送郵件服務器: smtp.qq.com,使用SSL,端口號465或587,其實是使用SSL用465,使用TLS使用587,不能混用,特別是JavaMail在設置Properties屬性的時候需要指定使用哪一種協(xié)議,不能用混,否則報錯QQ郵箱SMTP/IMAP服務
比如說我配置的是郵箱服務器是 smtp.qq.com:587,但是JavaMail的Properties設置的是
javaMailProperties.put(“mail.smtp.starttls.enable”, “true”);則會報上述錯,因為協(xié)議不匹配,必須使用465端口
總結
“mail.smtp.ssl.enable”和“mail.smtp.starttls.enable”是JavaMail郵件發(fā)送配置中的兩個不同參數(shù),它們的區(qū)別主要在于使用的加密協(xié)議
mail.smtp.ssl.enable:這個參數(shù)用于啟用SSL(Secure Sockets Layer)加密協(xié)議。當設置為true時,郵件傳輸將通過一個SSL連接進行,這通常意味著使用465端口。SSL協(xié)議在建立連接后立即加密數(shù)據(jù)流,適用于那些要求全加密通信的SMTP服務器。
mail.smtp.starttls.enable:這個參數(shù)用于啟用TLS(Transport Layer Security)加密協(xié)議。當設置為true時,郵件傳輸將通過一個開始時未加密的連接進行,然后在傳輸過程中升級為TLS加密,這通常意味著使用587端口。TLS協(xié)議是在數(shù)據(jù)傳輸之前建立一個加密層,適用于那些提供明文連接然后升級到加密通信的SMTP服務器。
總的來說,mail.smtp.ssl.enable和mail.smtp.starttls.enable都是用于郵件加密的配置項,但它們分別對應不同的加密方式和端口。如果SMTP服務器支持SSL,則應配置mail.smtp.ssl.enable;如果SMTP服務器支持TLS,則應配置mail.smtp.starttls.enable。
private JavaMailSenderImpl generateMailSender(EmailSendDto emailSendDto) { JavaMailSenderImpl currentMailSender = new JavaMailSenderImpl(); currentMailSender.setHost(emailSendDto.getHost()); currentMailSender.setPort(Integer.parseInt(emailSendDto.getPort())); currentMailSender.setUsername(emailSendDto.getUsername()); currentMailSender.setPassword(emailSendDto.getPassword()); currentMailSender.setDefaultEncoding("UTF-8"); Properties javaMailProperties = new Properties(); javaMailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); javaMailProperties.put("mail.smtp.auth", "true"); javaMailProperties.put("mail.smtp.ssl.enable", "true"); // 465端口 // javaMailProperties.put("mail.smtp.starttls.enable", "true"); // 587端口 javaMailProperties.put("mail.debug", "true"); currentMailSender.setJavaMailProperties(javaMailProperties); return currentMailSender; }
到此這篇關于javamail發(fā)送qq郵箱失敗的原因及解決的文章就介紹到這了,更多相關javamail發(fā)送qq郵箱內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot通過ThreadLocal實現(xiàn)登錄攔截詳解流程
這篇文章主要介紹了SpringBoot(HandlerInterceptor)+ThreadLocal實現(xiàn)登錄攔截,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05SpringCloud?微服務數(shù)據(jù)權限控制的實現(xiàn)
這篇文章主要介紹的是權限控制的數(shù)據(jù)權限層面,意思是控制可訪問數(shù)據(jù)資源的數(shù)量,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-11-11詳解Spring Boot中如何自定義SpringMVC配置
這篇文章主要給大家介紹了關于Spring Boot中如何自定義SpringMVC配置的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2021-09-09在Java中使用ModelMapper簡化Shapefile屬性轉JavaBean實戰(zhàn)過程
本文介紹了在Java中使用ModelMapper庫簡化Shapefile屬性轉JavaBean的過程,對比了原始的set方法和構造方法,展示了如何使用ModelMapper進行動態(tài)屬性映射,從而減少手動編寫轉換代碼的工作量,通過示例代碼,展示了如何使用GeoTools讀取Shapefile屬性并將其轉換為JavaBean對象2025-02-02