Android-通过imap/pop3编程读取电子邮件中的*.txt附件
在android中。如何通过pop3/imap协议从电子邮件中读取附加的*.txt文件内容Android-通过imap/pop3编程读取电子邮件中的*.txt附件,android,email,imap,pop3,Android,Email,Imap,Pop3,在android中。如何通过pop3/imap协议从电子邮件中读取附加的*.txt文件内容 电子邮件提供商可能是gmail、yahoo、exchange server..v.v..看一下 要从消息[]接收.txt附件,请查看 String contentType=message.getContentType(); 字符串messageContent=“”; if(contentType.contains(“多部分”)){ //内容可能包含附件 Multipart Multipart=(Multi
电子邮件提供商可能是gmail、yahoo、exchange server..v.v..看一下 要从消息[]接收.txt附件,请查看
String contentType=message.getContentType();
字符串messageContent=“”;
if(contentType.contains(“多部分”)){
//内容可能包含附件
Multipart Multipart=(Multipart)message.getContent();
int numberOfParts=multiPart.getCount();
对于(int partCount=0;partCount1){
attachFiles=attachFiles.substring(0,attachFiles.length()-2);
}
}else if(contentType.contains(“text/plain”)
||contentType.contains(“text/html”)){
对象内容=message.getContent();
如果(内容!=null){
messageContent=content.toString();
}
}
非常感谢您:D
Properties props = new Properties();
//IMAPS protocol
props.setProperty(“mail.store.protocol”, “imaps”);
//Set host address
props.setProperty(“mail.imaps.host”, imaps.gmail.com);
//Set specified port
props.setProperty(“mail.imaps.port”, “993″);
//Using SSL
props.setProperty(“mail.imaps.socketFactory.class”, “javax.net.ssl.SSLSocketFactory”);
props.setProperty(“mail.imaps.socketFactory.fallback”, “false”);
//Setting IMAP session
Session imapSession = Session.getInstance(props);
Store store = imapSession.getStore(“imaps”);
//Connect to server by sending username and password.
//Example mailServer = imap.gmail.com, username = abc, password = abc
store.connect(mailServer, account.username, account.password);
//Get all mails in Inbox Forlder
inbox = store.getFolder(“Inbox”);
inbox.open(Folder.READ_ONLY);
//Return result to array of message
Message[] result = inbox.getMessages();
String contentType = message.getContentType();
String messageContent = "";
if (contentType.contains("multipart")) {
// content may contain attachments
Multipart multiPart = (Multipart) message.getContent();
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
attachFiles += fileName + ", ";
part.saveFile(saveDirectory + File.separator + fileName);
} else {
// this part may be the message content
messageContent = part.getContent().toString();
}
}
if (attachFiles.length() > 1) {
attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
}
} else if (contentType.contains("text/plain")
|| contentType.contains("text/html")) {
Object content = message.getContent();
if (content != null) {
messageContent = content.toString();
}
}