2016-12-27 5 views
0

私は内部ストレージ(.dbまたは.db.cryptファイルが保存されている場所)にアクセスしたい、そこから取得したいアンドロイドsmtpアプリケーションを作成しようとしています。そのファイルを電子メールで送信します。下の は私のコードですが、すべて正常に動作しています。私はそのファイルを取得して送信することができます。私の元のファイル名は "file1.db"だと仮定しますが、smtpを通して送信した後、それは単なるファイルである "file1"として受信します。いずれかのアイデアがある ?どのように私はそのDBファイルを送信できますか?android smtp with db file

import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Environment; 
import android.widget.Toast; 

import java.io.File; 
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.activation.DataSource; 
import javax.activation.FileDataSource; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Multipart; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeBodyPart; 
import javax.mail.internet.MimeMessage; 
import javax.mail.internet.MimeMultipart; 

//Class is extending AsyncTask because this class is going to perform a networking operation 
public class SendMail extends AsyncTask<Void,Void,Void> { 
//Declaring Variables 
private Context context; 
private Session session; 

//Information to send email 
private String email; 
private String subject; 
private String message; 

//Progressdialog to show while sending email 
private ProgressDialog progressDialog; 

//Class Constructor 
public SendMail(Context context, String email, String subject, String message){ 
    //Initializing variables 
    this.context = context; 
    this.email = email; 
    this.subject = subject; 
    this.message = message; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    //Showing progress dialog while sending email 
    progressDialog = ProgressDialog.show(context,"Sending message","Please wait...",false,false); 
} 

@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 
    //Dismissing the progress dialog 
    progressDialog.dismiss(); 
    //Showing a success message 
    Toast.makeText(context,"Message Sent",Toast.LENGTH_LONG).show(); 
} 

@Override 
protected Void doInBackground(Void... params) { 
    //Creating properties 
    Properties props = new Properties(); 

    //Configuring properties for gmail 
    //If you are not using gmail you may need to change the values 
    props.put("mail.smtp.host", "smtp.gmail.com"); 
    props.put("mail.smtp.socketFactory.port", "465"); 
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.port", "465"); 

    //Creating a new session 
    session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       //Authenticating the password 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(Config.EMAIL, Config.PASSWORD); 
       } 
      }); 

    try { 
     //Creating MimeMessage object 
     MimeMessage mm = new MimeMessage(session); 

     //Setting sender address 
     mm.setFrom(new InternetAddress(Config.EMAIL)); 
     //Adding receiver 
     mm.addRecipient(Message.RecipientType.TO, new InternetAddress(email)); 
     //Adding subject 
     mm.setSubject(subject); 
     //Adding message 
     mm.setText(message); 

     Multipart multipart = new MimeMultipart(); 


     MimeBodyPart messageBodyPart = new MimeBodyPart(); 

     DataSource source = new FileDataSource(new File(Environment.getExternalStorageDirectory().getAbsoluteFile()+File.separator+"PasswordSafe.db")); 
     messageBodyPart.setDataHandler(new DataHandler(source)); 
     messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT); 
     messageBodyPart.setFileName("file1"); 

     multipart.addBodyPart(messageBodyPart); 

     mm.setContent(multipart); 


     //Sending email 
     Transport.send(mm); 

    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } 
    return null; 
}} 
+0

'messageBodyPart.setFileName( "FILE1")を置き換える;' 'messageBodyPart.setFileName( "file1.db")と;'役立つかもしれません君は –

答えて

0

これはあなたのためのトリックを行います。..

String fileName = name + " yourFile"+".db";