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

发布于 2024-11-05 20:52:06 字数 2834 浏览 3 评论 0原文

您好,我正在尝试通过附加数据库发送电子邮件,我收到邮件,但没有附加以下是我的代码。 任何人都可以帮助我..? 尝试{

                    String host = "smtp.gmail.com";
                    String from = "[email protected]";
                    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 = {"[email protected]"}; // 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();
                              }


        } 

HI I am trying to send an email by attaching a database fie, i am getting mail without attaching the fallowing is my code.
can any one help me..?
try{

                    String host = "smtp.gmail.com";
                    String from = "[email protected]";
                    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 = {"[email protected]"}; // 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();
                              }


        } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2024-11-12 20:52:06

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

The email program can't access your application's private files, in this case, the database you are trying to send. Copy the database to a folder in the SD card and attach it from there.

萌辣 2024-11-12 20:52:06

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

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"

You can copy the file to the SDCard using the following code:

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

Remember to add the following permission: android:name="android.permission.WRITE_EXTERNAL_STORAGE"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文