我是Android Studio的新手,我构建了一个从电子邮件中读取并从中提取数据的应用程序,它一直显示我在SSL套接字工厂中出错,我搜索了很多,似乎没什么工作,在清单中我添加了互联网的许可,并试图将端口号更改为143而不是993,但它仍然是相同的,如果有人可以帮助我。
public class EmailManager {
private String stmpHost = "smtp.gmail.com";
private String mailServer = "imap.gmail.com";
private EmailAccount account;
Session smtpSession;
private Session imapSession;
private Folder inbox;
public Store store;
public String from="xxx@gmail.com";
public String sub ="100";
public String FName, LName,Age, appDate, Loc, All, Med, refby;
public int lineNumber = 0;
BufferedReader br;
public InputStream is;
String sCurrentLine = null;
String next = " ";
Date datetime;
String[] info;
Context context;
DBHandler dbHandler;
SQLiteDatabase sqLiteDatabase;
public EmailManager(String username, String password, String stmpHost, String mailServer) {
account = new EmailAccount(username, password);
this.stmpHost = stmpHost;
this.mailServer = mailServer;
initProtocol();
}
private void initProtocol() {
EmailAuthenticator authenticator = new EmailAuthenticator(account);
Properties props1 = new Properties();
props1.setProperty("mail.store.protocol", "smtps");
props1.setProperty("mail.host", stmpHost);
props1.put("mail.smtp.auth", "true");
props1.put("mail.smtp.port", "465");
props1.put("mail.smtp.socketFactory.port", "465");
props1.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props1.put("mail.smtp.socketFactory.fallback", "false");
props1.setProperty("mail.smtp.quitwait", "false");
smtpSession = Session.getDefaultInstance(props1, authenticator);
Properties props2 = new Properties();
props2.setProperty("mail.transport.protocol", "imaps");
props2.setProperty("mail.imaps.host", mailServer);
props2.setProperty("mail.imaps.port", "993");
props2.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props2.setProperty("mail.imaps.socketFactory.fallback", "false");
imapSession = Session.getInstance(props2);
}
public Message[] getMails() throws Exception {
store = imapSession.getStore("imaps");
store.connect(mailServer, account.username, account.password);
inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
Message[] result = inbox.getMessages();
for (int i = 0; i < result.length; i++) {
//print subjects of all mails in the inbox
{
if(result[i].getFrom().toString() == from && result[i].getSubject()==sub){
try {
result[i].getContent().toString();
} catch (IOException e) {
e.printStackTrace();
}
LineReader();
saveData();
}
}
}
return result;
}
public void LineReader() throws Exception {
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
appDate = df.format(datetime);
try{
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
while((next = br.readLine()) != null){
sCurrentLine = next;
lineNumber++;
}
if (sCurrentLine != null) {
info = sCurrentLine.split(",");
}
FName = info[0];
LName = info[1];
Age = info[2];
appDate = info[3];
Loc = info[4];
Med = info[5];
All = info[6];
refby = info[7];
}
catch (IOException e1){
e1.printStackTrace();
}finally{
try {
if (br !=null)br.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
}
private void saveData() {
String FirstName = FName;
String LastName = LName;
String age = Age;
String Location = Loc;
String Medicine = Med;
String Allergies = All;
String Referredby = refby;
java.sql.Date DatenTime = java.sql.Date.valueOf(appDate) ;
Emails em = new Emails();
em.setFName(FirstName);
em.setLName(LastName);
em.setAge(age);
em.setDatetime(DatenTime);
em.setLoc(Location);
em.setMed(Medicine);
em.setAll(Allergies);
em.setRefby(Referredby);
dbHandler = new DBHandler(context);
sqLiteDatabase = dbHandler.getWritableDatabase();
dbHandler.addEmail(em, sqLiteDatabase);
dbHandler.close();
}
public void close() {
//Close connection
try {
inbox.close(false);
store.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
错误
javax.mail.MessagingException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: imap.gmail.com, 993; Exception: android.os.NetworkOnMainThreadException;
01-27 09:54:03.712 11463-11463/com.mbp.doctorshelper W/System.err: nested exception is:
01-27 09:54:03.712 11463-11463/com.mbp.doctorshelper W/System.err: java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: imap.gmail.com, 993; Exception: android.os.NetworkOnMainThreadException
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at javax.mail.Service.connect(Service.java:288)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at javax.mail.Service.connect(Service.java:169)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at com.mbp.doctorshelper.EmailManager.getMails(EmailManager.java:76)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at com.mbp.doctorshelper.AppsList$1.run(AppsList.java:68)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at android.os.Looper.loop(Looper.java:234)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5526)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-27 09:54:03.716 11463-11463/com.mbp.doctorshelper W/System.err: Caused by: java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: imap.gmail.com, 993; Exception: android.os.NetworkOnMainThreadException
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:178)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:538)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: ... 11 more
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: Caused by: android.os.NetworkOnMainThreadException
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at java.net.InetAddress.getByName(InetAddress.java:305)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at java.net.InetSocketAddress.<init>(InetSocketAddress.java:100)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at java.net.InetSocketAddress.<init>(InetSocketAddress.java:85)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
01-27 09:54:03.717 11463-11463/com.mbp.doctorshelper W/System.err: ... 14 more