2016-12-27 49 views
0

我想創建一個android的smtp應用程序,我想要訪問內部存儲(存儲.db或.db.crypt文件),從那裏我想要該文件並通過電子郵件發送。下面是 是我的代碼,其中一切工作正常。我能夠獲得該文件併發送它。但假設我的原始文件名是「file1.db」,但通過smtp發送後,它會收到「file1」,這只是一個文件。 任何人有想法? PLZ告訴我如何發送該數據庫文件?Android的smtp附件的db文件

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( 「文件1」);'和'messageBodyPart.setFileName( 「file1.db」);'可能幫助您 –

回答

0

這將這樣的伎倆你..

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