android下载电子邮件附件到SD

时间:2013-02-15 05:57:54

标签: android email email-attachments

我在Android上阅读了很多关于电子邮件的博客和论坛......我现在的目标是从 POP3邮件服务器下载传入的电子邮件... 一个巨大的信息列表,如何发送电子邮件,同时有关于 HOWTO RECEIVE 的电子邮件,其中包含附件的 NO 信息,并以编程方式保存附件在 SD卡 ...

请,任何帮助表示赞赏!!!

最接近的意思是例如...... Android: Correctly downloading/saving an email attachement

此代码适用于java中的PC!

    package com.tsysv.mail;
    import java.io.IOException;
    import java.util.Properties;
    import javax.mail.Folder;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.NoSuchProviderException;
    import javax.mail.Session;
    import com.sun.mail.pop3.POP3Store;
    public class EmailReceiveTest {

public static void main(String[] args) {

    String mailPop3Host = "pop3.***.**";
    String mailStoreType = "pop3";
    String mailUser = "*****";
    String mailPassword = "******";

    receiveEmail(mailPop3Host, mailStoreType, mailUser, mailPassword);
}

public static void receiveEmail(String pop3Host, String storeType, String user, String password) {

    try {
        Properties properties = new Properties();
        properties.put("mail.pop3.host", pop3Host);
        Session emailSession = Session.getDefaultInstance(properties);

        POP3Store emailStore = (POP3Store) emailSession.getStore(storeType);
        emailStore.connect(user, password);

        Folder emailFolder = emailStore.getFolder("INBOX");
        emailFolder.open(Folder.READ_ONLY);

        Message[] messages = emailFolder.getMessages();
        for (int i = 0; i < messages.length; i++) {
            Message message = messages[i];
            System.out.println("==============================");
            System.out.println("Email #" + (i + 1));
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());
        }

        emailFolder.close(false);
        emailStore.close();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
} 

但是在Android上实现它会在StackTrace中出现这样的错误......

    02-15 10:21:55.044: W/System.err(2687): javax.mail.MessagingException: Connect failed;
    02-15 10:21:55.044: W/System.err(2687):   nested exception is:
    02-15 10:21:55.044: W/System.err(2687):     java.net.SocketException: Permission denied
    02-15 10:21:55.044: W/System.err(2687):     at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:176)
    02-15 10:21:55.054: W/System.err(2687):     at javax.mail.Service.connect(Service.java:291)
    02-15 10:21:55.054: W/System.err(2687):     at javax.mail.Service.connect(Service.java:172)
    02-15 10:21:55.054: W/System.err(2687):     at javax.mail.Service.connect(Service.java:192)
    02-15 10:21:55.054: W/System.err(2687):     at m.tsysv.pop3mail.MainActivity.receiveEmail(MainActivity.java:67)
    02-15 10:21:55.054: W/System.err(2687):     at m.tsysv.pop3mail.MainActivity$1.onClick(MainActivity.java:53)
    02-15 10:21:55.054: W/System.err(2687):     at android.view.View.performClick(View.java:2408)
    02-15 10:21:55.054: W/System.err(2687):     at android.view.View$PerformClick.run(View.java:8817)
    02-15 10:21:55.054: W/System.err(2687):     at android.os.Handler.handleCallback(Handler.java:587)
    02-15 10:21:55.054: W/System.err(2687):     at android.os.Handler.dispatchMessage(Handler.java:92)
    02-15 10:21:55.054: W/System.err(2687):     at android.os.Looper.loop(Looper.java:144)
    02-15 10:21:55.054: W/System.err(2687):     at android.app.ActivityThread.main(ActivityThread.java:4937)
    02-15 10:21:55.054: W/System.err(2687):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-15 10:21:55.054: W/System.err(2687):     at java.lang.reflect.Method.invoke(Method.java:521)
    02-15 10:21:55.054: W/System.err(2687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    02-15 10:21:55.054: W/System.err(2687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    02-15 10:21:55.054: W/System.err(2687):     at dalvik.system.NativeStart.main(Native Method)
    02-15 10:21:55.054: W/System.err(2687): Caused by: java.net.SocketException: Permission denied
    02-15 10:21:55.064: W/System.err(2687):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
    02-15 10:21:55.064: W/System.err(2687):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
    02-15 10:21:55.064: W/System.err(2687):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
    02-15 10:21:55.064: W/System.err(2687):     at java.net.Socket.checkClosedAndCreate(Socket.java:873)
    02-15 10:21:55.064: W/System.err(2687):     at java.net.Socket.connect(Socket.java:1020)
    02-15 10:21:55.064: W/System.err(2687):     at java.net.Socket.connect(Socket.java:997)
    02-15 10:21:55.064: W/System.err(2687):     at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
    02-15 10:21:55.064: W/System.err(2687):     at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    02-15 10:21:55.064: W/System.err(2687):     at com.sun.mail.pop3.Protocol.<init>(Protocol.java:98)
    02-15 10:21:55.064: W/System.err(2687):     at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:227)
    02-15 10:21:55.064: W/System.err(2687):     at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:172)
    02-15 10:21:55.064: W/System.err(2687):     ... 16 more

0 个答案:

没有答案