1. 程式人生 > >javax.mail.AhenticationFailedException: 535 Err / A secure connection is requiered(java發郵件)

javax.mail.AhenticationFailedException: 535 Err / A secure connection is requiered(java發郵件)

  • Java發郵件的幾種方式:

    • JavaMail

    • Commons Email

    • Spring Mail

  • 實驗場景: Spring Mail、Web Project

  • 具體描述

    • 郵件伺服器: smtp.qq.com (QQ)

    • 框架: Spring

    • jar包:

      • Spring.jar( 多個jar包,集合)
      • mail-1.4.7.jar (版本自選)
      • commons-logging-1.2.jar(列印日誌資訊,同上)
  • 執行結果:
    • 列印日誌資訊如下(xml中設定):
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE"
, arg "73400320" DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN" DEBUG SMTP: Found extension "AUTH=LOGIN", arg "" DEBUG SMTP: Found extension "MAILCOMPRESS", arg "" DEBUG SMTP: Found extension "8BITMIME", arg "" DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM DEBUG SMTP: AUTH LOGIN command trace suppressed DEBUG SMTP: AUTH LOGIN failed Exception in
thread "main" org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256 at org.springframework.mail
.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:392) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:306) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296) at mail.SpringSimpleMailTest.main(SpringSimpleMailTest.java:35) Caused by: javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿´: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256 at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826) at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685) at javax.mail.Service.connect(Service.java:295) at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389) ... 3 more
    一開始,嘗試使用 smtp.163.com(網易),丟擲類似的錯誤,由於問題源於一個開源專案的研究,可能對其進行了封裝,除錯了很久,只能解決部分問題。
    。。。
    查閱了一些資料,郵件傳送成功,此類相關問題應該就能迎刃而解!
  • 原因如下:

    • 服務 授權碼

    • 正確配置郵件傳送相關資訊

  • Web Project 專案結構:

    • project name:TestMail

    • package name : mail

    • file : 3 個

    • RT:
      這裡寫圖片描述
  • SpringSimpleMailTest.java
package mail;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/** 
 * @ClassName: SpringSimpleMailTest
 * 
 * @Description: TODO Spring 實現簡單的郵件傳送測試
 * 
 * @author kngines
 * 
 * @date 2017年5月6日   
 */
public class SpringSimpleMailTest {

    public static void main(String[] args) {

        // 建立應用程式上下文
        ApplicationContext actx = new ClassPathXmlApplicationContext("mail/mailMessage.xml");

        // Spring 封裝MailSender,不再重複造輪子
        MailSender ms = (MailSender) actx.getBean("mailSender");

        // 簡單郵件傳送
        SimpleMailMessage smm = (SimpleMailMessage) actx.getBean("mailMessage");

        // 主題,內容
        smm.setSubject("你好");
        smm.setText("這個是一個通過Spring框架來發送郵件的小程式");

        // 傳送
        ms.send(smm);
    }
}
  • mail.properties
#-------------------------------------
# 郵件服務配置
#-------------------------------------

# 伺服器
mail.host=smtp.qq.com

# 埠號 qq郵箱需要使用SSL 埠號465或587
mail.smtp.port=465

# 伺服器超時時間
mail.smtp.timeout=50000

# starttls 是對純文字通訊協議的擴充套件。它提供一種方式將純文字連線升級為加密連線(TLS或SSL)
mail.smtp.starttls.enable=true

# 開啟控制檯列印伺服器相應資訊(日誌)
mail.debug=true

# 登入使用者名稱,如果是qq,則填寫qq號
mail.username=123456

# 授權碼(例如,qq 開啟 SMTP 授權碼,qq授權碼16位)
mail.password=xzbasdjfadladsfa

# 是否需要驗證碼
mail.smtp.auth=true

# 發件人地址(qq郵箱 A)
mail.from=123456@qq.com

# 收件人地址(qq郵箱 B)
mail.to=9876543@qq.com
  • mailMessage.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
     http://www.springframework.org/schema/aop  
     http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p">

    <!-- 載入Properties檔案 -->
    <bean id="configurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/mail/mail.properties</value>
            </list>
        </property>
    </bean>

    <!-- 申明SimpleMailMessage物件 -->
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from">
            <value>${mail.from}</value>
        </property>
        <!-- 設定接收方 -->
        <property name="to" value="${mail.to}" />
        <!-- 檢視SimpleMailMessage原始碼還可以注入標題,內容等 -->
    </bean>

    <!-- 宣告 JavaMailSenderImpl物件 -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="defaultEncoding" value="UTF-8" />
        <property name="host" value="${mail.host}" />
        <property name="username" value="${mail.username}" />
        <property name="password" value="${mail.password}" />
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.debug">${mail.debug}</prop>
                <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop>
                <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
                <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
                <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
                <prop key="mail.smtp.socketFactory.fallback">false</prop>
            </props>
        </property>
    </bean>

</beans>  
  • 根據步驟建立Web Project 、匯入jar包之後,檢查 客戶端授權碼、埠是否配置(密碼不是登入郵箱時的真實密碼)

  • 成功執行結果如下:

DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "73400320"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
DEBUG SMTP: Found extension "MAILCOMPRESS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: STARTTLS requested but already using SSL
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<1234567@qq.com>
250 Ok
RCPT TO:<3147755101@qq.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   3147755101@qq.com
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Sat, 6 May 2017 20:10:20 +0800 (CST)
From: 123456@qq.com
To: 9876543@qq.com
Message-ID: <857394605.0.1494072621026.JavaMail.[email protected]-PC>
Subject: =?UTF-8?B?5L2g5aW9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: base64

6L+Z5Liq5piiA5Liq6YCa6L+HU5qGG5p6l5Y+R6Ku5E5bCP56iL
5bqP
.
250 Ok: queued as 
QUIT
221 Bye

相關推薦

javax.mail.AhenticationFailedException: 535 Err / A secure connection is requieredjava郵件

Java發郵件的幾種方式: JavaMail Commons Email Spring Mail 實驗場景: Spring Mail、Web Project 具體描述: 郵件伺服器: s

A secure connection is requiered(such as ssl). More information at http://service.mail.qq.com/cgi-bi

span QQ class inf orm socket SM secure tor javax.mail.AuthenticationFailedException: 530 Error: A secure connection is requiered(such as

使用javamail報出:530 Error: A secure connection is requiered(such as ssl). More information at http://se

2、使用javamail報出530 Error: A secure connection is requiered(such as ssl). More information at http://service.mail.qq 這個原因還容易找一些,直接跳到了自己傳送郵件的郵

530 A secure connection is requiered(such as ssl)

原因主要是qq郵箱的黑名單制度,通過網站傳送來的郵件會被認為不安全,解決辦法為 登入qq郵箱,進入郵箱首頁,點選最下面的自助查詢,收信查詢,刪除黑名單 ================= 如果163郵箱作為發信郵箱,在測試時,發現發郵件報錯535 Error

javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇéÇë¿問題

        今天在做郵件傳送啟用帳戶的時候,報瞭如下錯誤:    javax.mail.AuthenticationFailedException: 535 Error: ÇëʹÓÃÊÚȨÂëµÇ¼¡£ÏêÇé

Create a Secure Connection to AWS VPC

Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So

java mail: javax.mail.AuthenticationFailedException: 535 authentication failed

javax.mail.AuthenticationFailedException: 535 Error: authentication failed at com.sun.mail.smtp.SMTPT

javax.mail.AuthenticationFailedException: 535 authentication failed

Exception in thread “main” javax.mail.AuthenticationFailedException: 535 Error: authentication fai

poj3511--A Simple Problem with Integers線段樹求和

poj pac style som can com onos roman miss A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K

POJ 2492 A Bug's Life 並查集

ont set -1 flat com rom init red least Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assum

Codeforces A - Bear and Prime 100交互題

mes 思路 logs cin out else 一個 如果能 pos A - Bear and Prime 100 思路:任何一個合數都可以寫成2個以上質數的乘積。在2-100中,除了4,9,25,49外都可以寫成兩個以上不同質數的乘積。 所以打一個質數加這四個數的表:{

HDU1142 A Walk Through the Forest最短路+DAG

input ica highlight iss diff wan before star length A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6

poj-2488 a knight's journey搜索題

ber namespace ons pat ins gin input lse small Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing t

A Bug's Life 並查集

     Background     Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that

linux學習-mail傳送郵件mail不能郵件

主機:centos 7 , 安裝sendmail,啟動sendmail,一般就可以了,但是有些同學為什麼不行,就是啟不動出現如下的情況!那現在如何解決。 yum install sendmailsystemctl start sendmail.service system

關於如何在linux環境下生成a庫和so庫改,附圖

一般linux環境下的連結庫分為a庫和so庫,分別表示靜態連結庫和動態連結庫,其作用是把C程式編譯好做成一種可執行連結檔案,c主程式檔案呼叫這些程式的函式介面是可以使用a庫或so庫,在主程式中只需要i

Java Mail---SMTP協議-Java郵件(帶附件)演示過程

轉載請註明出處: http://blog.csdn.net/qq_26525215 本文源自 JavaMail-API簡介: 郵件客戶端的主要任務是向郵件伺服器傳送郵件,以及接收來自郵件伺服器的郵件。 Sun公司制定了一套API,它封裝了郵件通訊的

[A]1065 A+B and C (64bit)挖坑待填

\n con 大於 64bit suppose rst tell art 問題 Given three integers A, B and C in [-2^63, 2^63], you are supposed to tell whether A+B > C. In

解決 RabbitMQ 叢集 Channel shutdown: connection error 錯誤HAProxy 負載均衡

具體錯誤資訊: 2018-05-04 11:21:48.116 ERROR 60848 --- [.168.0.202:8001] o.s.a.r.c.CachingConnectionFactory : Channel shutdown: connection error 2018-05-0

hiho第一週A+B各種語言版本內容來自hiho

using System; public class AplusB { private static void Main() { string line; while((line = Console.ReadLine()) != null)