2014-03-13 56 views
0

我正在開發一個原生android應用程序,發送電子郵件到註冊的 郵件id.i不想讓任何客戶端發送郵件,它會直接發送郵件 一旦用戶點擊發送郵件按鈕我的佈局設計 感謝致謝電子郵件

+0

你必須從服務器端做到這一點。由於安全原因,它至少會要求發送按鈕。其餘的東西,你可以做的實際,即收件人和身體。 – Rahul

+0

@Rahul:謝謝,你是否有這樣的鏈接或代碼 –

+0

Intent email = new Intent(Intent.ACTION_SEND); \t email.putExtra(Intent.EXTRA_EMAIL,new String [] {「[email protected]」}); \t \t \t email.putExtra(Intent.EXTRA_SUBJECT,「subject」); \t email.putExtra(Intent.EXTRA_TEXT,「message」); \t email.setType(「message/rfc822」); (Intent.createChooser(email,「選擇一個電子郵件客戶端:」)); – Rahul

回答

0
import java.util.Properties; 

import javax.activation.DataHandler; 
import javax.mail.Message; 
import javax.mail.Session; 
import javax.mail.URLName; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.mail.util.ByteArrayDataSource; 

import android.accounts.Account; 
import android.accounts.AccountManager; 
import android.accounts.AccountManagerCallback; 
import android.accounts.AccountManagerFuture; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 

import com.sun.mail.smtp.SMTPTransport; 
import com.sun.mail.util.BASE64EncoderStream; 

public class GMailSender { 
    private Session session; 
    private String token; 


    public String getToken() { 
     return token; 
    } 

    public GMailSender(Activity ctx) { 
     super(); 
     initToken(ctx); 
    } 

    public void initToken(Activity ctx) { 

     AccountManager am = AccountManager.get(ctx); 

     Account[] accounts = am.getAccountsByType("com.google"); 
     for (Account account : accounts) { 
      Log.d("getToken", "account="+account); 
     } 

     Account me = accounts[0]; //You need to get a google account on the device, it changes if you have more than one 


     am.getAuthToken(me, "oauth2:https://mail.google.com/", null, ctx, new AccountManagerCallback<Bundle>(){ 
      @Override 
      public void run(AccountManagerFuture<Bundle> result){ 
       try{ 
        Bundle bundle = result.getResult(); 
        token = bundle.getString(AccountManager.KEY_AUTHTOKEN); 
        Log.d("initToken callback", "token="+token);  

       } catch (Exception e){ 
        Log.d("test", e.getMessage()); 
       } 
      } 
     }, null); 

     Log.d("getToken", "token="+token); 
    } 



    public SMTPTransport connectToSmtp(String host, int port, String userEmail, 
      String oauthToken, boolean debug) throws Exception { 

     Properties props = new Properties(); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.starttls.required", "true"); 
     props.put("mail.smtp.sasl.enable", "false"); 

     session = Session.getInstance(props); 
     session.setDebug(debug); 

     final URLName unusedUrlName = null; 
     SMTPTransport transport = new SMTPTransport(session, unusedUrlName); 
     // If the password is non-null, SMTP tries to do AUTH LOGIN. 
     final String emptyPassword = null; 

     /* enable if you use this code on an Activity (just for test) or use the AsyncTask 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     */ 

     transport.connect(host, port, userEmail, emptyPassword); 

     byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", 
       userEmail, oauthToken).getBytes(); 
     response = BASE64EncoderStream.encode(response); 

     transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235); 

     return transport; 
    } 

    public synchronized void sendMail(String subject, String body, String user, 
      String oauthToken, String recipients) { 
     try { 

      SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587, 
        user, oauthToken, true); 

      MimeMessage message = new MimeMessage(session); 
      DataHandler handler = new DataHandler(new ByteArrayDataSource(
        body.getBytes(), "text/plain")); 
      message.setSender(new InternetAddress(user)); 
      message.setSubject(subject); 
      message.setDataHandler(handler); 
      if (recipients.indexOf(',') > 0) 
       message.setRecipients(Message.RecipientType.TO, 
         InternetAddress.parse(recipients)); 
      else 
       message.setRecipient(Message.RecipientType.TO, 
         new InternetAddress(recipients)); 
      smtpTransport.sendMessage(message, message.getAllRecipients()); 

     } catch (Exception e) { 
      Log.d("test", e.getMessage(), e); 
     } 
    } 

} 

上面下載的mail.jar和activation.jar爲Android https://code.google.com/p/javamail-android/ 或嘗試這些鏈接: http://nilvec.com/sending-email-without-user-interaction-in-android/ Javamail api in android using XOauth http://google-mail-xoauth-tools.googlecode.com/svn/trunk/java/com/google/code/samples/xoauth/XoauthAuthenticator.java

+0

是的,我試了這個,它會發送郵件客戶端嗎?,但沒有客戶端,我可以直接發送? –

+0

試試這個代碼它工作.. – Rahul