2014-09-19 35 views
0

在我的android應用程序有一個按鈕發送郵件,當我點擊發送按鈕使用這行Gmail將打開,然後我點擊發送郵件。爲什麼郵件會在我的應用程序關閉的同時成功發送?我需要留在同一頁面?當我調用intent.createChooser發送郵件時自動關閉應用程序?

try 
{ 
     String extpath=Environment.getExternalStorageDirectory() +"/NewFolder/DBName"; 
     File pathp=new File(extpath); 
     Log.d("New Path", pathp.toString()); 

     long fileSize = pathp.length(); 
     if(fileSize > 0) 
     { 

      final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

      String address = "[email protected]"; 
      String subject = "Database"; 
      String emailtext = "Please check the attached database and save it"; 

      emailIntent.setType("application/octet-stream"); 

      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { address }); 

      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 

      emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + pathp)); 

      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext); 

      startActivity(Intent.createChooser(emailIntent, "Send Mail...")); 
     } 
     else 
     { 
      Log.d("Error", "Attachment didn't attach "); 
     } 
    } 
    catch (Throwable t) 
    { 
     Log.d("Error on sending mail", t.toString()); 
    } 

當我運行此代碼郵件發送,然後應用程序將關閉。我不想關閉應用程序。提前幫助我感謝。

+1

發佈了一些相關的代碼 – kgandroid 2014-09-19 12:15:47

+0

@kgandroid我更新了我的問題,好心的讀了它 – Sri 2014-09-19 12:23:53

+0

@Sri你有什麼錯誤? – 2014-09-19 12:34:51

回答

0
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 

Button button; 
Session session; 
ProgressDialog dialog; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    SendMailBySite bySite=new SendMailBySite(); 
    bySite.execute(); 

} 


class SendMailBySite extends AsyncTask<Void, Void, Void>{ 



    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     dialog=new ProgressDialog(MainActivity.this); 
     dialog.setMessage("Sending mail....."); 
     dialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // TODO Auto-generated method stub 

      final String user="[email protected]";//change accordingly 
      final String password="XXXXXXX";//change accordingly 

      String to="[email protected]";//change accordingly 

      //Get the session object 
      Properties props = new Properties(); 
      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"); 

      Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(user,password); 
       } 
      }); 

      //Compose the message 
      try { 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(user)); 
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to)); 
      message.setSubject("Mobiappsdevelper"); 
      message.setText("This is simple program of sending email using JavaMail API"); 

      //send the message 
      Transport.send(message); 



      } catch (MessagingException e) {e.printStackTrace();} 


     return null; 


    } 
    @Override 
    protected void onPostExecute(Void result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     dialog.dismiss(); 
     Toast.makeText(getApplicationContext(), "message sent successfully...", 3000).show(); 
    } 
    } 
} 
+0

謝謝我解決了 – Sri 2014-09-19 13:01:41

+0

如果你投給我,這將非常有用! – 2014-09-19 13:10:23

相關問題