我正在尝试在任何Gmail帐户中阅读电子邮件。以下是代码,如何列出任何已使用getMail()方法的Gmail帐户中的收件箱项目。
mailReader.java
package myMail.namespace;
import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class mailReader extends javax.mail.Authenticator{
static {
Security.addProvider(new myMail.namespace.JSSEProvider());
}
static Session session;
static Store store;
public static Message [] getMail() throws MessagingException{
connect();
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
return messages;
}
private static void connect() throws MessagingException{
if (null != session)
return;
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.imap.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.socketFactory.port", "993");
session = Session.getDefaultInstance(props);
store = session.getStore("imap");
store.connect("imap.gmail.com", "myMail@gmail.com", "myPass");
}
}
package myMail.namespace;
import java.security.Security;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
public class mailReader extends javax.mail.Authenticator{
static {
Security.addProvider(new myMail.namespace.JSSEProvider());
}
static Session session;
static Store store;
public static Message [] getMail() throws MessagingException{
connect();
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_ONLY);
Message[] messages = folder.getMessages();
return messages;
}
private static void connect() throws MessagingException{
if (null != session)
return;
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.imap.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.socketFactory.port", "993");
session = Session.getDefaultInstance(props);
store = session.getStore("imap");
store.connect("imap.gmail.com", "myMail@gmail.com", "myPass");
}
}
和readMail类
package myMail.namespace;
import javax.mail.Message;
import javax.mail.MessagingException;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class readMail extends ListActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
Message[] messages = new Message[] { };
try {
messages = mailReader.getMail();
}
catch (MessagingException e) {
Log.e("mailReader getMail error. in readmail.java", e.getMessage(), e);
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = getItem(position);
TextView resultView = null;
resultView = new TextView(super.getContext());
try {
resultView.setText(message.getSubject());
}
catch (MessagingException e) {
Log.e("resultView.setText in readmail", e.getMessage(), e);
}
return resultView;
}
});
}
}
和JSSE提供商
package myMail.namespace;
import javax.mail.Message;
import javax.mail.MessagingException;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class readMail extends ListActivity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
//setContentView(R.layout.main);
Message[] messages = new Message[] { };
try {
messages = mailReader.getMail();
}
catch (MessagingException e) {
Log.e("mailReader getMail error. in readmail.java", e.getMessage(), e);
}
setListAdapter(new ArrayAdapter<Message>(this,
android.R.layout.simple_list_item_1, messages){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = getItem(position);
TextView resultView = null;
resultView = new TextView(super.getContext());
try {
resultView.setText(message.getSubject());
}
catch (MessagingException e) {
Log.e("resultView.setText in readmail", e.getMessage(), e);
}
return resultView;
}
});
}
}
答案 0 :(得分:0)
我认为你应该在“public View getView(int position,View convertView,ViewGroup parent)”的函数中调用super.getView()来获取有效的布局视图并从中查找文本视图,然后填写电子邮件的主题。