0

使用代码,我在移动到收件箱时遇到错误。该代码之前工作过,但现在它给出了一个错误。我该如何纠正它?

代码与错误一起提供。这显示了 SSL 工厂错误。错误是:

07-10 09:16:25.404: W/System.err(1524): javax.mail.MessagingException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: imap.gmail.com, 993; Exception: android.os.NetworkOnMainThreadException;
07-10 09:16:25.404: W/System.err(1524):   nested exception is:
07-10 09:16:25.415: W/System.err(1524):     java.io.IOException: Couldn't connect using "javax.net.ssl.SSLSocketFactory" socket factory to host, port: imap.gmail.com, 993; Exception: android.os.NetworkOnMainThreadException
07-10 09:16:25.424: W/System.err(1524):     at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
07-10 09:16:25.424: W/System.err(1524):     at javax.mail.Service.connect(Service.java:288)
07-10 09:16:25.434: W/System.err(1524):     at javax.mail.Service.connect(Service.java:169)
07-10 09:16:25.434: W/System.err(1524):     at com.mycomp.android.test.EmailManager.getMails(EmailManager.java:56)
07-10 09:16:25.454: W/System.err(1524):     at com.mycomp.android.test.New1$1.onClick(New1.java:70)
07-10 09:16:25.454: W/System.err(1524):     at android.view.View.performClick(View.java:4084)
07-10 09:16:25.454: W/System.err(1524):     at android.view.View$PerformClick.run(View.java:16966)
07-10 09:16:25.454: W/System.err(1524):     at android.os.Handler.handleCallback(Handler.java:615)
07-10 09:16:25.454: W/System.err(1524):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 09:16:25.454: W/System.err(1524):     at android.os.Looper.loop(Looper.java:137)
07-10 09:16:25.454: W/System.err(1524):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-10 09:16:25.474: W/System.err(1524):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 09:16:25.474: W/System.err(1524):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 09:16:25.474: W/System.err(1524):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-10 09:16:25.474: W/System.err(1524):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-10 09:16:25.474: W/System.err(1524):     at dalvik.system.NativeStart.main(Native Method)
07-10 09:16:25.484: W/System.err(1524): 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
07-10 09:16:25.505: W/System.err(1524):     at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:178)
07-10 09:16:25.505: W/System.err(1524):     at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
07-10 09:16:25.514: W/System.err(1524):     at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
07-10 09:16:25.524: W/System.err(1524):     at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:538)
07-10 09:16:25.544: W/System.err(1524):     ... 15 more
07-10 09:16:25.544: W/System.err(1524): Caused by: android.os.NetworkOnMainThreadException
07-10 09:16:25.564: W/System.err(1524):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-10 09:16:25.564: W/System.err(1524):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-10 09:16:25.596: W/System.err(1524):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-10 09:16:25.604: W/System.err(1524):     at java.net.InetAddress.getByName(InetAddress.java:289)
07-10 09:16:25.614: W/System.err(1524):     at java.net.InetSocketAddress.<init>(InetSocketAddress.java:105)
07-10 09:16:25.614: W/System.err(1524):     at java.net.InetSocketAddress.<init>(InetSocketAddress.java:90)
07-10 09:16:25.624: W/System.err(1524):     at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
07-10 09:16:25.634: W/System.err(1524):     at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
07-10 09:16:25.644: W/System.err(1524):     ... 18 more

我的代码是:

package com.mycomp.android.test;

import java.io.IOException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;



public class EmailManager {
    private String stmpHost = "smtp.gmail.com";
    private String mailServer = "imap.gmail.com";
    private EmailAccount account;
    private Session smtpSession; 
    private Session imapSession; 

    private Folder inbox;
    private Store store;
    public EmailManager(String username, String password, String urlServer, String stmpHost, String mailServer) {
        account = new EmailAccount(username, password, urlServer);
        this.stmpHost = stmpHost;
        this.mailServer = mailServer;
        initProtocol();
    }
    private void initProtocol() {
        EmailAuthenticator authenticator = new EmailAuthenticator(account);

        Properties props1 = new Properties();  
        props1.setProperty("mail.transport.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.store.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 MessagingException {
        store = imapSession.getStore("imaps");
        store.connect(mailServer, account.username, account.password);
        inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        Message[] result = inbox.getMessages();


        return result;
    }
    public void close() {
        //Close connection 
        try {
            inbox.close(false);
            store.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {  
        MimeMessage message = new MimeMessage(smtpSession);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));  
        message.setSender(new InternetAddress(sender));  
        message.setSubject(subject);  
        message.setDataHandler(handler);  
        if (recipients.indexOf(',') > 0)  
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));  
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));  
        Transport.send(message);  
    } 
}
4

1 回答 1

0

如何修复 android.os.NetworkOnMainThreadException
原因是您尝试在 UI 线程中连接到网络,而 API 不允许这样做。每当您连接到网络时,请使用AsyncTask 。在这种情况下,您应该将 EmailManager 类的构造函数放入 AsycnTask 的 doInBackground 中。

于 2012-07-10T04:22:05.010 回答