【问题标题】:Android - Reading *.txt attachment in email throught imap/pop3 programmaticallyAndroid - 以编程方式通过 imap/pop3 读取电子邮件中的 *.text 附件
【发布时间】:2014-09-27 17:14:22
【问题描述】:

在安卓中。如何通过 pop3/imap 协议从电子邮件中读取附加的 *.txt 文件内容?

电子邮件提供商可能是 gmail、yahoo、exchange server..v.v..

【问题讨论】:

    标签: android email imap pop3


    【解决方案1】:

    看看How to get gmail mails programmatically in android的例子

    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();
    

    要从 message[] 中接收 .txt 附件,请查看 http://www.codejava.net/java-ee/javamail/download-attachments-in-e-mail-messages-using-javamail

    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();
        }
    }
    

    【讨论】:

    • 非常感谢:D
    猜你喜欢
    • 2010-11-12
    • 2023-03-25
    • 2017-09-23
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多