如何将数据库文件附加到android中的电子邮件?

时间:2011-05-06 04:13:40

标签: android email attachment

HI我正在尝试通过附加数据库发送电子邮件,我收到的邮件没有附加以下是我的代码。 谁能帮我..?尝试{

                    String host = "smtp.gmail.com";
                    String from = "abc@.com";
                    String pass = "aaaaadd";
                    Properties props = System.getProperties();
                    props.put("mail.smtp.starttls.enable", "true"); // added this line
                    props.put("mail.smtp.host", host);
                    props.put("mail.smtp.user", from);
                    props.put("mail.smtp.password", pass);
                    props.put("mail.smtp.port", "587");
                    props.put("mail.smtp.auth", "true");

                    String[] to = {"to@mail.com"}; // added this line

                    Session session = Session.getDefaultInstance(props, null);

                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(from));

                    InternetAddress[] toAddress = new InternetAddress[to.length];

                    // To get the array of addresses
                    for( int i=0; i < to.length; i++ ) { 
                        toAddress[i] = new InternetAddress(to[i]);
                    }


                    for( int i=0; i < toAddress.length; i++) {
                        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
                    }

                    message.setSubject("sending in a group");
                    message.setText("Welcome to JavaMail");//The exception is thrown here   


                    FileDataSource fds = new FileDataSource(new File("/data/data/packagename/databases/dbname.txt"));
                    //fds.getFile();
                    Log.v("File name is",  fds.getFile().toString());
                    Log.v("File size is", fds.getContentType());
                    message.setDataHandler(new DataHandler(fds));

                    message.setFileName("Callist.db");          
                    message.setDisposition(Part.ATTACHMENT);


                    Transport transport = session.getTransport("smtp");
                    transport.connect(host, from, pass);
                    transport.sendMessage(message, message.getAllRecipients());

                    transport.close();
                     Toast.makeText(getApplicationContext(), "E mail Sent", Toast.LENGTH_SHORT).show();

             } 
             catch(Exception e){
                 Toast.makeText(getApplicationContext(), " .."+e.toString(), Toast.LENGTH_LONG).show();
                              }


        } 

2 个答案:

答案 0 :(得分:0)

电子邮件程序无法访问应用程序的私人文件,在这种情况下,是您尝试发送的数据库。将数据库复制到SD卡中的文件夹并从那里附加。

答案 1 :(得分:-1)

您可以使用以下代码将文件复制到SD卡:

File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
    String currentDBPath = "\\data\\application.package\\databases\\name";
    String backupDBPath = "name";
    File currentDB = new File(data, currentDBPath);
    File backupDB = new File(sd, backupDBPath);
    if (currentDB.exists()) {
        FileChannel src;
    try {
    src = new FileInputStream(currentDB).getChannel();
        FileChannel dst = new FileOutputStream(backupDB).getChannel();
        try {
        dst.transferFrom(src, 0, src.size());
    src.close();
            dst.close();
        } catch (IOException e) {                                                    
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }

请记住添加以下权限:android:name =“android.permission.WRITE_EXTERNAL_STORAGE”