0
來自Gmail的郵件我想接收使用JavaMail從我的Gmail帳戶的郵件,但我正在執行過程中的錯誤....這是我的代碼MessagingException嘗試讀取使用Java
package sendemail;
import javax.mail.*;
import java.net.*;
import java.util.*;
public class Test {
public static void main(String args[])
{
Properties prop=new Properties();
prop.setProperty("mail.store.protocol", "imap");
prop.setProperty("mail.imap.port", "993");
try
{
Session session=Session.getInstance(prop,null);
Store store=session.getStore();
store.connect("imap.gmail.com", "Username", "Password");
Folder folder=store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message msg=folder.getMessage(folder.getMessageCount());
Address[] add=msg.getFrom();
for(Address address:add)
{
System.out.println("FROM:"+address.toString());
}
Multipart mp=(Multipart)msg.getContent();
BodyPart bp=mp.getBodyPart(0);
System.out.println("SENT DATE:"+msg.getSentDate());
System.out.println("SUBJECT:"+msg.getSubject());
System.out.println("CONTENT:"+msg.getContent());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
錯誤:
javax.mail.MessagingException;
nested exception is:
java.io.IOException
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:298)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at sendemail.Test.main(Test.java:25)
Caused by: java.io.IOException
at com.sun.mail.iap.ResponseInputStream.read0(ResponseInputStream.java:78)
at com.sun.mail.iap.ResponseInputStream.readResponse(ResponseInputStream.java:48)
at com.sun.mail.iap.Response.<init>(Response.java:64)
at com.sun.mail.imap.protocol.IMAPResponse.<init>(IMAPResponse.java:31)
at com.sun.mail.imap.protocol.IMAPResponse.readResponse(IMAPResponse.java:105)
at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:153)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:72)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:61)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:273)
... 3 more
BUILD SUCCESSFUL (total time: 11 seconds)
我想從我的帳戶接收郵件並顯示ii作爲輸出。 (我正在使用netbeans來運行此程序)
只是爲了完整:Imap在Gmail設置中被激活,對吧? – Ray
如果您需要爲您的帳戶啓用IMAP,請使用https://support.google.com/mail/troubleshooter/1668960?hl=zh-CN#ts=1665018 – DeadChex
您是否位於可能阻止IMAP的代理防火牆之後? –