3

我是android编程新手,我想检索Gmail收件箱。在浏览了许多页面后,我对如何检索它有了一些想法,编译时没有错误,但在通过模拟器执行时,突然应用程序关闭说。不幸的是,“App_name”已经停止。

我在这里粘贴我的代码,请让我知道问题出在哪里。

inbox.java 我想在其中获取我的收件箱。

import javax.mail.MessagingException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class Inbox extends Activity {

    EmailManager emailManager;
    private String stmpHost = "smtp.gmail.com";
    private String mailServer = "imap.gmail.com";    
    public String urlServer = "gmail.com";
    public String username = "your username";
    public String password = "your password";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inbox);

        emailManager=new EmailManager(username, password, urlServer, stmpHost, mailServer);
        try {
            emailManager.getInboxMails();           
            emailManager.close();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent intent = getIntent();
        String message = intent.getStringExtra(DatabaseConnector.EXTRA_MESSAGE);
        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
        // Set the text view as the activity layout
        setContentView(textView);
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_inbox, menu);
        return true;
    }

}




import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
//import javax.mail.Transport;
//import javax.mail.internet.InternetAddress;
//import javax.mail.internet.MimeMessage;
//
//import android.location.Address;
import android.util.Log;
//import android.widget.Toast;




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,sentMail;
    private Store store,sentstore;
      boolean isSet;
    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 void getInboxMails() throws MessagingException {
        store = imapSession.getStore("imaps");
        store.connect(mailServer, account.username, account.password);
        inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        Message[] messages = inbox.getMessages();
//      List<Address>addresses=new ArrayList<Address>();

        for (int i = messages.length - 1; i >= 0; i--) 
        {

             Part p = messages[i]; 
            try {
                String subject = messages[i].getSubject();
                Log.v("subject","subject="+subject);
                String body=getText(p);
                Log.v("body","body="+body);
                javax.mail.Address[] address=messages[i].getFrom();
                String add=address.toString().trim();
                Log.v("address","address="+add);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return ;
    }

    private boolean textIsHtml = false;

    /**
     * Return the primary text content of the message.
     */
    private String getText(Part p) throws
                MessagingException, IOException {
        if (p.isMimeType("text/*")) {
            String s = (String)p.getContent();
            textIsHtml = p.isMimeType("text/html");
            return s;
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart)p.getContent();
            String text = null;
            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                if (bp.isMimeType("text/plain")) {
                    if (text == null)
                        text = getText(bp);
                    continue;
                } else if (bp.isMimeType("text/html")) {
                    String s = getText(bp);
                    if (s != null)
                        return s;
                } else {
                    return getText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart)p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getText(mp.getBodyPart(i));
                if (s != null)
                    return s;
            }
        }

        return null;
    }


    public void close() {
        //Close connection 
        try {
//          inbox.close(false);
//          store.close();
            sentMail.close(false);
            sentstore.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }


}


package com.example.lomenttech;


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;


public class EmailAuthenticator extends Authenticator   {
    private EmailAccount account;
    public EmailAuthenticator(EmailAccount account) {
        super();
        this.account = account;
    }
    protected PasswordAuthentication getPasswordAuthentication() {  
        return new PasswordAuthentication(account.emailAddress, account.password);  
    } 
}



package com.example.lomenttech;


public class EmailAccount {
    public String urlServer = "gmail.com";
    public String username = "meenashah.np";
    public String password = "your password";
    public String emailAddress;
    public EmailAccount(String username, String password, String urlServer) {
        this.username = username;
        this.password = password;
        this.urlServer = urlServer;
        this.emailAddress = username + "@" + urlServer;
    }

}

package com.example.lomenttech;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

public class ByteArrayDataSource implements DataSource {   
    private byte[] data;   
    private String type;   

    public ByteArrayDataSource(byte[] data, String type) {   
        super();   
        this.data = data;   
        this.type = type;   
    }   
    public ByteArrayDataSource(byte[] data) {   
        super();   
        this.data = data;   
    }   
    public void setType(String type) {   
        this.type = type;   
    }   
    public String getContentType() {   
        if (type == null)   
            return "application/octet-stream";   
        else  
            return type;
    }   
    public InputStream getInputStream() throws IOException {   
        return new ByteArrayInputStream(data);   
    }   
    public String getName() {   
        return "ByteArrayDataSource";   
    }   
    public OutputStream getOutputStream() throws IOException {   
        throw new IOException("Not Supported");   
    }
    @Override
    public PrintWriter getLogWriter() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public int getLoginTimeout() throws SQLException {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void setLogWriter(PrintWriter arg0) throws SQLException {
        // TODO Auto-generated method stub

    }
    @Override
    public void setLoginTimeout(int arg0) throws SQLException {
        // TODO Auto-generated method stub

    }
    @Override
    public boolean isWrapperFor(Class<?> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public <T> T unwrap(Class<T> arg0) throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public Connection getConnection() throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }
    @Override
    public Connection getConnection(String arg0, String arg1)
            throws SQLException {
        // TODO Auto-generated method stub
        return null;
    }   
} 



These are the logcat errors:

01-22 19:54:23.027: E/AndroidRuntime(849): FATAL EXCEPTION: main
01-22 19:54:23.027: E/AndroidRuntime(849): java.lang.NoClassDefFoundError: javax.mail.Session
01-22 19:54:23.027: E/AndroidRuntime(849):  at com.example.lomenttech.EmailManager.getInboxMails(EmailManager.java:73)
01-22 19:54:23.027: E/AndroidRuntime(849):  at com.example.lomenttech.Inbox.onCreate(Inbox.java:37)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.Activity.performCreate(Activity.java:5104)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.os.Looper.loop(Looper.java:137)
01-22 19:54:23.027: E/AndroidRuntime(849):  at android.app.ActivityThread.main(ActivityThread.java:5039)
01-22 19:54:23.027: E/AndroidRuntime(849):  at java.lang.reflect.Method.invokeNative(Native Method)
01-22 19:54:23.027: E/AndroidRuntime(849):  at java.lang.reflect.Method.invoke(Method.java:511)
01-22 19:54:23.027: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-22 19:54:23.027: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-22 19:54:23.027: E/AndroidRuntime(849):  at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0