2012-07-20 115 views
1

我有一個示例gmail客戶端類。在該課程中,我初始化了兩個SMTPS會話。 有一段時間它工作正常。但是現在Gmail會拋出SMTO發送失敗異常。Gmail中的SMTPSendFailedException 530,SMTP失敗,而IMAP工作

這是我如何通過JavaMail連接到Gmail ..

//String smtpHost = "imap.gmail.com"; 
//String imapHost = "smtp.gmail.com"; 
//String userName = "[email protected]"; 
//String userPass = "PASSWORD"; 
//Properties props = new Properties(); 


public DirectMailBox(String smtpHost, String imapHost, String userName, 
     String userPass, Properties props) throws MessagingException { 
    super(); 
    this.imapHost = imapHost; 
    this.smtpHost = smtpHost; 
    this.userName = userName; 
    this.userPass = userPass; 
    this.props = props; 

    session = Session.getDefaultInstance(props); 
    session.setDebug(true); 
    imapsStore = session.getStore("imaps"); 
    imapsStore.connect(imapHost, userName, userPass); 

    smtpsTransport = session.getTransport("smtps"); 
    smtpsTransport.connect(smtpHost, userName, userPass); 
    smtpsTransport.addTransportListener(this); 
} 

,這裏是我如何調用sendmail的..

public void sendMail(MimeMessage newMessage) throws MessagingException { 
    smtpsTransport.sendMessage(newMessage, newMessage.getAllRecipients()); 
} 

這個URL,但沒有辦法操作指導。 http://support.google.com/mail/bin/answer.py?answer=14257

這裏是一個調試轉儲.....

DEBUG: setDebug: JavaMail version 1.4ea 
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc] 
DEBUG: mail.imap.fetchsize: 16384 
* OK Gimap ready for requests from 14.140.225.250 uy8if8067087pbc.8 
A0 CAPABILITY 
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH 
A0 OK Thats all she wrote! uy8if8067087pbc.8 
IMAP DEBUG: AUTH: XOAUTH 
DEBUG: protocolConnect login, host=smtp.gmail.com, [email protected], password=<non-null> 
A1 LOGIN [email protected] PASSWORD 
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE 
A1 OK [email protected] Rakesh Waghela authenticated (Success) 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "imap.gmail.com", port 465, isSSL true 
220 mx.google.com ESMTP mt9sm3371719pbb.14 
DEBUG SMTP: connected to host "imap.gmail.com", port: 465 

EHLO LPT0031 
250-mx.google.com at your service, [14.140.225.250] 
250-SIZE 35882577 
250-8BITMIME 
250-AUTH LOGIN PLAIN XOAUTH 
250 ENHANCEDSTATUSCODES 
DEBUG SMTP: Found extension "SIZE", arg "35882577" 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH" 
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
DEBUG: setDebug: JavaMail version 1.4ea 
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc] 
DEBUG: mail.imap.fetchsize: 16384 
* OK Gimap ready for requests from 14.140.225.250 vf8if4407540pbc.7 
A0 CAPABILITY 
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH 
A0 OK Thats all she wrote! vf8if4407540pbc.7 
IMAP DEBUG: AUTH: XOAUTH 
DEBUG: protocolConnect login, host=smtp.gmail.com, [email protected], password=<non-null> 
A1 LOGIN [email protected] PASSWORD 
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE 
A1 OK [email protected] Rakesh Waghela authenticated (Success) 
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc] 
DEBUG SMTP: useEhlo true, useAuth false 
DEBUG SMTP: trying to connect to host "imap.gmail.com", port 465, isSSL true 
220 mx.google.com ESMTP ru4sm3359950pbc.66 
DEBUG SMTP: connected to host "imap.gmail.com", port: 465 

EHLO LPT0031 
250-mx.google.com at your service, [14.140.225.250] 
250-SIZE 35882577 
250-8BITMIME 
250-AUTH LOGIN PLAIN XOAUTH 
250 ENHANCEDSTATUSCODES 
DEBUG SMTP: Found extension "SIZE", arg "35882577" 
DEBUG SMTP: Found extension "8BITMIME", arg "" 
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH" 
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
DEBUG SMTP: use8bit false 
MAIL FROM:<[email protected]> 
530-5.5.1 Authentication Required. Learn more at 
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 mt9sm3371719pbb.14 
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication Required. Learn more at 
530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 mt9sm3371719pbb.14 

    at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388) 
    at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959) 
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583) 
    at com.wellogic.phoenix.mail.direct.DirectMailBox.sendMail(DirectMailBox.java:86) 
    at com.wellogic.phoenix.test.DirectMailTest.testSendMail(DirectMailTest.java:40) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Not Delivered... 
+0

http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ – HectorLector 2012-07-20 07:24:17

+0

@HectorLector它不喜歡我不能夠連接.. !!這是我不能發送郵件...... !!! IMAP使用相同的代碼。 – 2012-07-20 07:45:31

回答

5

終於解決了這個問題.. !!使用以下方法。

http://mlusincuba.wordpress.com/2012/03/03/java-mail-gmail-smtp-client/

package com.mycompany.employee.utils; 

import java.io.UnsupportedEncodingException; 
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

/** 
* @author Mlungisi 
* 
*/ 
public class GMail { 

    public static void sendMail(String senderEmail, String recipientEmail, String subject, String message) throws MessagingException, UnsupportedEncodingException { 

     // SSL // I USED THIS METHOD    
     Properties propsSSL = new Properties(); 

     // EVEN IF YOU SKIP THESE TWO PROP IT WOULD WORK 
     propsSSL.put("mail.transport.protocol", "smtps"); 
     propsSSL.put("mail.smtps.host", "smtp.gmail.com"); 

     // THIS IS THE MOST IMPORTANT PROP --> "mail.smtps.auth" 
     propsSSL.put("mail.smtps.auth", "true"); 

     Session sessionSSL = Session.getInstance(propsSSL); 
     sessionSSL.setDebug(true); 

     Message messageSSL = new MimeMessage(sessionSSL); 
     messageSSL.setFrom(new InternetAddress("[email protected]", "Mlungisi Sincuba")); 
     messageSSL.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]")); // real recipient 
     messageSSL.setSubject("Test mail using SSL"); 
     messageSSL.setText("This is test email sent to Your account using SSL."); 

     Transport transportSSL = sessionSSL.getTransport(); 
     // EVEN IF YOU SKIP PORT NUMBER , IT WOULD WORK 
     transportSSL.connect("smtp.gmail.com", 465, "[email protected]", "yourpassword"); // account used 
     transportSSL.sendMessage(messageSSL, messageSSL.getAllRecipients()); 
     transportSSL.close(); 

     System.out.println("SSL done."); 
    } 

    public static void main(String[] args) throws MessagingException, UnsupportedEncodingException { 
     System.out.println("Hello World!"); 
     sendMail(null, null, null, null); 
    } 
} 
+0

你提到的鏈接找不到!可以請這個方法找出錯誤嗎? dl.dropbox.com/u/3092259/TestJavaMail.zip我得到同樣的錯誤! – 2014-06-11 09:07:55

0

你似乎使用imap.gmail.com作爲您的SMTP服務器。你也可能想嘗試打開smtp認證。