我有以下方法使用imap
从Gmail检索邮件public static void main(String[] args)
{
Properties props = new Properties();
try
{
props.load(new FileInputStream(new File("smtp.properties")));
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("smtp.gmail.com", "******@gmail.com", "mypass");
Folder inbox = store.getFolder("inbox");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
Message[] messages = inbox.getMessages();
System.out.println("------------------------------");
for (int i = 0; i < 10; i++)
{
System.out.println("Mail Subject:- " + messages[i].getSubject());
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
我的smtp.properties包含
mail.smtp.host=smtp.gmail.com
mail.smtp.socketFactory.port=465
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
mail.smtp.auth=true
mail.smtp.port=465
运行程序时,我得到以下内容
javax.mail.MessagingException: Remote host closed connection during handshake;
nested exception is:
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:695)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at gmailsmpt.Main.main(Main.java:25)
Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:532)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:337)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:116)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:121)
at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:710)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:659)
... 3 more
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(Unknown Source)
... 14 more
如何解决问题?
答案 0 :(得分:0)
还有更多的堆栈跟踪吗?较旧的Java版本存在TLS握手问题,RSA密钥超过1024位(也可能是具有正确位字节的密钥)。
这已在Java 1.7或Java 1.8中修复。
答案 1 :(得分:0)
答案 2 :(得分:0)
尝试在下面添加属性: properties.put(&#34; mail.smtp.ssl.trust&#34;,&#34; smtp.gmail.com&#34;);