使用JavaMail附加文件

时间:2011-12-30 20:07:17

标签: java javamail

我的问题是什么?

我已经创建了SD图像,还将文件添加到DCIM相机..什么都没有..

我有这个错误: java.io.FileNotFoundException

这是我的发送代码:

  Button addImage = (Button) findViewById(R.id.button1); 
    addImage.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
        Envialo m = new Envialo("expertcr@gmail.com", "LOquillo97"); 

        String[] toArr = {"expertcr@hotmail.com", "expertcr@gmail.com"}; 
        m.setTo(toArr); 
        m.setFrom("expertcr@gmail.com"); 
        m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
        m.setBody("Email body. Testing from My APP"); 
        try { 
            m.addAttachment("file:///sdcard/DCIM/Camera/test.jpg");

            //Log.e("MailApp", "Attachment");
            if(m.send()) { 
              Toast.makeText(ABC.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
            } else { 
              Toast.makeText(ABC.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
            } 
          } catch(Exception e) { 
            Toast.makeText(ABC.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
            Log.e("MailApp", "Could not send email", e); 
          } 

      } 
    });

这是我的CatLog:

12-30 11:25:26.682: I/global(1342): Default buffer size used in BufferedOutputStream constructor. It would be better to be explicit if an 8k buffer is required.
12-30 11:25:26.682: I/global(1342): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
12-30 11:25:26.832: I/global(1342): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
12-30 11:25:27.852: E/MailApp(1342): Could not send email
12-30 11:25:27.852: E/MailApp(1342): javax.mail.MessagingException: IOException while sending message;
12-30 11:25:27.852: E/MailApp(1342):   nested exception is:
12-30 11:25:27.852: E/MailApp(1342):    java.io.FileNotFoundException: /sdcard/DCIM/Camera/test.jpg
12-30 11:25:27.852: E/MailApp(1342):    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.Transport.send0(Transport.java:189)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.Transport.send(Transport.java:118)
12-30 11:25:27.852: E/MailApp(1342):    at org.cw.LLC.Envialo.send(Envialo.java:99)
12-30 11:25:27.852: E/MailApp(1342):    at org.cw.LLC.ABC$1.onClick(ABC.java:30)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.View.performClick(View.java:2344)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.View.onTouchEvent(View.java:4133)
12-30 11:25:27.852: E/MailApp(1342):    at android.widget.TextView.onTouchEvent(TextView.java:6510)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.View.dispatchTouchEvent(View.java:3672)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
12-30 11:25:27.852: E/MailApp(1342):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
12-30 11:25:27.852: E/MailApp(1342):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
12-30 11:25:27.852: E/MailApp(1342):    at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
12-30 11:25:27.852: E/MailApp(1342):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
12-30 11:25:27.852: E/MailApp(1342):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
12-30 11:25:27.852: E/MailApp(1342):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 11:25:27.852: E/MailApp(1342):    at android.os.Looper.loop(Looper.java:123)
12-30 11:25:27.852: E/MailApp(1342):    at android.app.ActivityThread.main(ActivityThread.java:4203)
12-30 11:25:27.852: E/MailApp(1342):    at java.lang.reflect.Method.invokeNative(Native Method)
12-30 11:25:27.852: E/MailApp(1342):    at java.lang.reflect.Method.invoke(Method.java:521)
12-30 11:25:27.852: E/MailApp(1342):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-30 11:25:27.852: E/MailApp(1342):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-30 11:25:27.852: E/MailApp(1342):    at dalvik.system.NativeStart.main(Native Method)
12-30 11:25:27.852: E/MailApp(1342): Caused by: java.io.FileNotFoundException: /sdcard/DCIM/Camera/test.jpg
12-30 11:25:27.852: E/MailApp(1342):    at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:231)
12-30 11:25:27.852: E/MailApp(1342):    at java.io.FileInputStream.<init>(FileInputStream.java:80)
12-30 11:25:27.852: E/MailApp(1342):    at javax.activation.FileDataSource.getInputStream(FileDataSource.java:110)
12-30 11:25:27.852: E/MailApp(1342):    at javax.activation.DataHandler.writeTo(DataHandler.java:318)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:874)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:444)
12-30 11:25:27.852: E/MailApp(1342):    at com.sun.mail.handlers.multipart_mixed.writeTo(multipart_mixed.java:102)
12-30 11:25:27.852: E/MailApp(1342):    at javax.activation.ObjectDataContentHandler.writeTo(DataHandler.java:897)
12-30 11:25:27.852: E/MailApp(1342):    at javax.activation.DataHandler.writeTo(DataHandler.java:330)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.internet.MimeBodyPart.writeTo(MimeBodyPart.java:1403)
12-30 11:25:27.852: E/MailApp(1342):    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1745)
12-30 11:25:27.852: E/MailApp(1342):    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:636)
12-30 11:25:27.852: E/MailApp(1342):    ... 25 more

2 个答案:

答案 0 :(得分:1)

文件/sdcard/DCIM/Camera/test.jpg不存在。确保它是。

答案 1 :(得分:1)

您的应用程序是否有权访问外部存储空间?

以下标记应位于清单中:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>

看起来您正在使用自定义类来创建MIME邮件。关注发布“addAttachment(...)”的工作原理摘要?