我只想問我如何獲得我的電子郵件收件箱文件夾 上的消息並顯示它? 同樣,我怎樣才能訪問Gmail的收件箱或其他郵件客戶端的收件箱文件夾?這是可能的android?我怎樣才能得到Gmail /電子郵件客戶端收件箱文件夾中的郵件?
4
A
回答
2
在android上沒有正式的支持。在使用gmail-ls內容提供商的時候存在一些問題,但是從我收集的信息來看,這並不適用於最新版本的android(詳情請參閱this)。例如,如果您想使用Gmail郵件內容提供商check out this作爲示例,但請記住,這是一種黑客攻擊,並未得到正式支持。所以,如果/不工作時不要感到驚訝。
你可能想看看像gmail4j。它沒有一個正式的內容提供者的好集成,但它至少有一個已知的API。
1
public class EmailinboxActivity extends Activity {
/** Called when the activity is first created. */
EmailManager emailManager;
private String stmpHost = "smtp.gmail.com";
private String mailServer = "imap.gmail.com";
public String urlServer = "gmail.com";
public String username = " Any Gmail Username";
public String password = " Any Gmail Password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
emailManager=new EmailManager(username, password, urlServer, stmpHost, mailServer);
try {
// emailManager.getInboxMails();
emailManager.close();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//
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");
}
}
//
public class EmailAccount {
public String urlServer = "gmail.com";
public String username = "username";
public String password = "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;
}
}
//
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.android.emailinbox;
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 ;
}
/*public void getsentMails() throws MessagingException {
sentstore = imapSession.getStore("imaps");
sentstore.connect(mailServer, account.username, account.password);
sentMail = sentstore.getFolder("Sent");
sentMail.open(Folder.READ_ONLY);
Message[] sentmessages = sentMail.getMessages();
Log.v("messages","messages"+sentmessages);
Log.v("messages","messages"+sentmessages.length);
for (int i = sentmessages.length - 1; i >= 0; i--)
{
Part p = sentmessages[i];
try {
String sentsubject = sentmessages[i].getSubject();
Log.v("sentsubject","sentsubject="+sentsubject);
String sentbody=getText(p);
Log.v("sentbody","sentbody="+sentbody);
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 ;
}*/
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();
}
}
/*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);
} */
public String getText(Part p) throws MessagingException, IOException {
if (p.isMimeType("text/*")) {
boolean textIsHtml = false;
String s = (String) p.getContent();
textIsHtml = p.isMimeType("text/html");
return String.valueOf(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 String.valueOf(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 String.valueOf(s);
}
}
return null;
}
}
相關問題
- 1. 客戶端更新 - PHP/ASP - 電子郵件收件箱
- 2. CSS的電子郵件客戶端的收件箱
- 3. 發送電子郵件客戶端兼容電子郵件
- 4. Java郵件 - 電子郵件客戶端中未顯示附件?
- 5. java中的電子郵件客戶端
- 6. 收到的垃圾郵件gmail郵箱
- 7. 檢測電子郵件發送的電子郵件客戶端
- 8. 我們如何才能將PHP電子郵件發送到Gmail郵箱
- 9. 電子郵件模板 - 目標特定電子郵件客戶端(Gmail)
- 10. 我怎樣才能得到實際的電子郵件地址從蟒蛇電子郵件對象
- 11. cakePHP-電子郵件客戶端:讀本地主機上的Gmail郵箱
- 12. PHP郵件:在垃圾郵件文件夾中收到所有電子郵件
- 13. Gmail將電子郵件移至垃圾郵件文件夾
- 14. 將收件箱電子郵件的Web鉤子添加到公司gmail帳戶
- 15. PHP郵件功能不發送電子郵件到Gmail帳戶
- 16. 將共享郵箱電子郵件移到特定文件夾
- 17. 電子郵件客戶端檢測
- 18. 如何直接啓動電子郵件客戶端到收件箱視圖?
- 19. 查詢電子郵件收件箱
- 20. 電子郵件轉到垃圾郵件而不是收件箱
- 21. 某些電子郵件客戶端中的HTML電子郵件中的鏈接
- 22. 電子郵件收件箱集成
- 23. 我們怎樣才能得到實際的電子郵件到Windows Phone
- 24. 使用gmail帳戶在CakePHP的收件人垃圾郵件文件夾中顯示發送的電子郵件?
- 25. gmail api:回覆電子郵件已經在vb.net的收件箱
- 26. 用於從Gmail收件箱獲取電子郵件的代碼
- 27. Gmail收件箱的電子郵件圖標和標頭
- 28. 電子郵件超鏈接允許用戶選擇電子郵件客戶端
- 29. 顯示電子郵件收件箱?
- 30. 是否可以閱讀默認Android電子郵件客戶端的收件箱?
我工作過它..這可以幫助你 – 2012-10-31 13:16:23