2016-11-13 80 views
-1
Sun Nov 13 16:20:41 PKT 2016 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 
javax.mail.AuthenticationFailedException: [ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure) 
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:665) 
    at javax.mail.Service.connect(Service.java:295) 
    at javax.mail.Service.connect(Service.java:176) 
    at SentItems.initialize(SentItems.java:229) 
    at SentItems.<init>(SentItems.java:111) 
    at SentItems$1.run(SentItems.java:98) 
    at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
    at java.awt.EventQueue.access$500(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.awt.EventQueue$3.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
    at java.awt.EventQueue.dispatchEvent(Unknown Source) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source) 
    at java.awt.EventDispatchThread.run(Unknown Source) 
PCM_SIGNED 22050.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian 

我知道它爲什麼是這樣的錯誤,這是由於關閉谷歌設置安全性較低的應用程序,但我希望通過Java代碼來做到這一點。我得到使用Java郵件API

+0

您可以添加程序的電子郵件連接代碼,當然可以屏蔽IP,電子郵件ID,密碼等內容 –

+0

可能您應該使用SSL身份驗證。刪除您的代碼並更新您的問題。 –

回答

0
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Folder; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.NoSuchProviderException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Store; 
@SuppressWarnings("unused") 
public class Read { 
public static void check(String host, String storeType, String username, 
String password) 
{ 
try { 
// create properties field 
Properties properties = new Properties(); 
properties.put("mail.imaps.host", host); 
properties.put("mail.imaps.port", "993"); 
properties.put("mail.imaps.starttls.enable", "true"); 
// Setup authentication, get session 
Session emailSession = Session.getInstance(properties, 
new javax.mail.Authenticator() { 
protected PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication(username, password); 
    } 
    }); 
     emailSession.setDebug(true); 
    // create the IMAP store object and connect with the IMAP server 
    Store store = emailSession.getStore("imaps"); 
    store.connect(); 
    // create the folder object and open it 
    Folder emailFolder = store.getFolder("INBOX"); 
    emailFolder.open(Folder.READ_ONLY); 
    // retrieve the messages from the folder in an array and print it 
    Message[] messages = emailFolder.getMessages(); 
    System.out.println("messages.length---" + messages.length); 
    for (int i = 0, n = messages.length; i < n; i++) { 
    Message message = messages[i]; 
    System.out.println("---------------------------------"); 
    System.out.println("Email Number " + (i + 1)); 
    System.out.println("Subject: " + message.getSubject()); 
    System.out.println("From: " + message.getFrom()[0]); 
    System.out.println("Text: " + message.getContent().toString()); 
    } 
    // close the store and folder objects 
    emailFolder.close(false); 
    store.close(); 
    } catch (NoSuchProviderException e) { 
    e.printStackTrace(); 
    } catch (MessagingException e) { 
    e.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
    } 
    public static void main(String[] args) { 
    String host = "imap.gmail.com";// change accordingly 
    String mailStoreType = "imap"; 
    String username = "*******@gmail.com";// change accordingly 
    String password = "********";// change accordingly 
    check(host, mailStoreType, username, password); 
    } 
    } 

這是我的代碼。