2012-08-11 272 views
0

我有一個收聽來電的接收器。從這個接收器我開始一個服務,將發送電子郵件(使用android意圖),但我得到一個例外(從日誌貓PLZ引用日誌)。從Android中的服務開始活動時出錯

了java.lang.RuntimeException:無法啓動服務 [email protected]意圖{ CMP = com.pack.android.email.service/com.pack.android。 service.EmailService }:android.util.AndroidRuntimeException:在Activity上下文之外調用 的startActivity()需要FLAG_ACTIVITY_NEW_TASK 標誌。這真的是你想要的嗎?

我已經設置了'FLAG_ACTIVITY_NEW_TASK'和'FLAG_FROM_BACKGROUND'。這不是用戶活動。我正嘗試使用intent.SENDTO/SEND啓動現有活動(發送電子郵件)。

奇怪的事情是錯誤:android.content.ActivityNotFoundException: 致無活動處理意向{行動= android.intent.action.SENDTO FLG = 0x10000004(有演員)}

我在這裏錯過了什麼?

使用Java Mail API發送電子郵件時,我面臨的另一個問題是郵件被正確發送,這看起來像是什麼,但不知何故郵件似乎永遠不會出現在郵箱中。我指的是下面的鏈接發送電子郵件沒有意圖: Send email without intent

這將是gr8,如果有人可以提供一些見解。

代碼如下: 呼叫接收器: -

public class CallReceiver extends BroadcastReceiver 
{ 
    private static final String LOG_TAG = "CallReceiver"; 
    private static final String ACTION = "android.intent.action.PHONE_STATE"; 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     System.out.println("--------Inside onReceive of CallReceiver--------"); 
     Log.d(LOG_TAG, "Inside onReceive"); 

     if(intent.getAction().equals(ACTION)){ 
      Log.d(LOG_TAG, "-----Criteria matched-----"); 
      Intent emailIntent = new Intent(context, EmailService.class); 
      emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
      context.startService(emailIntent); 
     } 
    } 
} 

電子郵件服務: -

public class EmailService extends Service { 

    private static final String LOG_TAG = "EmailService"; 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     Log.d(LOG_TAG, "-------On create called-------"); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     Log.d(LOG_TAG, "-----Inside On Start Command-----"); 
     MailService mailer = new MailService("[email protected]","[email protected]","Test","Hi This is 5554 from Mail Service", "<b>HtmlBody</b>"); 
     try { 
      boolean success = mailer.sendAuthenticated(); 
      Log.d(LOG_TAG, String.valueOf(success)); 
     } catch (Exception e) { 
      Log.e(LOG_TAG, "Failed sending email.", e); 
     } 

     try { 
      GMailSender sender = new GMailSender("%MyUserId%@gmail.com", "%Mypassword%"); 
      sender.sendMail("Test Subject", 
        "Hi, This is 5554 from Gmail Sender", 
        "[email protected]", 
        "[email protected]"); 
     } catch (Exception e) { 
      Log.e("SendMail", e.getMessage(), e); 
     } 

     /*Intent emailIntent = new Intent(Intent.ACTION_SENDTO); 
     emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
//  emailIntent.setType("plain/text"); 

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

     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
       "Email from Intent"); 

     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
       "Hi, This is 5554 from intent"); 

//  getApplicationContext().startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
     Log.d(LOG_TAG, "context = " + getApplicationContext()); 
     startActivity(emailIntent);*/ 


     return Service.START_STICKY; 
    } 

    @Override 
    public void onDestroy() 
    { 
     super.onDestroy(); 
     Log.d(LOG_TAG, "-------On destroy called-------"); 
    } 
} 

郵件服務:

public class MailService { 
    private static final String LOG_TAG = "MailService"; 
    private String toList; 
    private String ccList; 
    private String bccList; 
    private String subject; 
    final private static String SMTP_SERVER = "smtp.gmail.com"; 
    private String from; 
    private String txtBody; 
    private String htmlBody; 
    private String replyToList; 

    private boolean authenticationRequired = false; 

    public MailService(String from, String toList, String subject, String txtBody, String htmlBody) { 
     this.txtBody = txtBody; 
     this.htmlBody = htmlBody; 
     this.subject = subject; 
     this.from = from; 
     this.toList = toList; 
     this.ccList = null; 
     this.bccList = null; 
     this.replyToList = null; 
     this.authenticationRequired = true; 
    } 

    public boolean sendAuthenticated() throws AddressException, MessagingException { 
     authenticationRequired = true; 
     return send(); 
    } 

    /** 
    * Send an e-mail 
    * 
    * @throws MessagingException 
    * @throws AddressException 
    */ 
    public boolean send() throws AddressException, MessagingException { 

     Log.d(LOG_TAG, "Inside Send !"); 
     Properties props = new Properties(); 

     // set the host smtp address 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.setProperty("mail.host", SMTP_SERVER); 
     props.put("mail.smtp.auth", "true"); // needed for gmail 
     props.put("mail.smtp.port", "465"); // gmail smtp port - 587 
//  props.put("mail.user", from); 

     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 

     Session session; 

     if (authenticationRequired) { 
      Authenticator auth = new SMTPAuthenticator(); 
      Log.d(LOG_TAG, "auth = "+auth); 
      session = Session.getDefaultInstance(props, auth); 
     } else { 
      session = Session.getDefaultInstance(props, null);   
     } 

     // get the default session 
     session.setDebug(true); 

     // create message 
     Message msg = new javax.mail.internet.MimeMessage(session); 

     // set from and to address 
     try { 
      msg.setFrom(new InternetAddress(from, from)); 
      msg.setReplyTo(new InternetAddress[]{new InternetAddress(from,from)}); 
     } catch (Exception e) { 
      msg.setFrom(new InternetAddress(from)); 
      msg.setReplyTo(new InternetAddress[]{new InternetAddress(from)}); 
     } 

     // set send date 
     msg.setSentDate(Calendar.getInstance().getTime()); 

     // parse the recipients TO address 
     java.util.StringTokenizer st = new java.util.StringTokenizer(toList, ","); 
     int numberOfRecipients = st.countTokens(); 

     javax.mail.internet.InternetAddress[] addressTo = new javax.mail.internet.InternetAddress[numberOfRecipients]; 

     int i = 0; 
     while (st.hasMoreTokens()) { 
      addressTo[i++] = new javax.mail.internet.InternetAddress(st 
        .nextToken()); 
     } 
     msg.setRecipients(javax.mail.Message.RecipientType.TO, addressTo); 

     // parse the replyTo addresses 
     if (replyToList != null && !"".equals(replyToList)) { 
      st = new java.util.StringTokenizer(replyToList, ","); 
      int numberOfReplyTos = st.countTokens(); 
      javax.mail.internet.InternetAddress[] addressReplyTo = new javax.mail.internet.InternetAddress[numberOfReplyTos]; 
      i = 0; 
      while (st.hasMoreTokens()) { 
       addressReplyTo[i++] = new javax.mail.internet.InternetAddress(
         st.nextToken()); 
      } 
      msg.setReplyTo(addressReplyTo); 
     } 

     // parse the recipients CC address 
     if (ccList != null && !"".equals(ccList)) { 
      st = new java.util.StringTokenizer(ccList, ","); 
      int numberOfCCRecipients = st.countTokens(); 

      javax.mail.internet.InternetAddress[] addressCC = new javax.mail.internet.InternetAddress[numberOfCCRecipients]; 

      i = 0; 
      while (st.hasMoreTokens()) { 
       addressCC[i++] = new javax.mail.internet.InternetAddress(st 
         .nextToken()); 
      } 

      msg.setRecipients(javax.mail.Message.RecipientType.CC, addressCC); 
     } 

     // parse the recipients BCC address 
     if (bccList != null && !"".equals(bccList)) { 
      st = new java.util.StringTokenizer(bccList, ","); 
      int numberOfBCCRecipients = st.countTokens(); 

      javax.mail.internet.InternetAddress[] addressBCC = new javax.mail.internet.InternetAddress[numberOfBCCRecipients]; 

      i = 0; 
      while (st.hasMoreTokens()) { 
       addressBCC[i++] = new javax.mail.internet.InternetAddress(st 
         .nextToken()); 
      } 

      msg.setRecipients(javax.mail.Message.RecipientType.BCC, addressBCC); 
     } 

     msg.setSubject(subject); 
     Multipart mp = new MimeMultipart("related"); 

     // set body message 
     MimeBodyPart bodyMsg = new MimeBodyPart(); 
     bodyMsg.setText(txtBody); 
     mp.addBodyPart(bodyMsg); 

     msg.setContent(mp); 

     // send it 
     try { 
      Address[] fromAddressArray = msg.getFrom(); 
      for(Address address : fromAddressArray) 
      { 
       Log.d(LOG_TAG,"from = " + address.toString()+", "); 
      } 

      Address[] recipientAddressArray = msg.getAllRecipients(); 
      for(Address address : recipientAddressArray) 
      { 
       Log.d(LOG_TAG,"recipient = " + address.toString()+", "); 
      } 

      Address[] replyToAddressArray = msg.getReplyTo(); 
      for(Address address : replyToAddressArray) 
      { 
       Log.d(LOG_TAG,"replyTo = " + address.toString()+", "); 
      } 

      javax.mail.Transport.send(msg); 
      return true; 
     } catch (Exception e) { 
      Log.e(LOG_TAG, e.getMessage()); 
      e.printStackTrace(); 
     } 
     return false; 
    } 

    /** 
    * SimpleAuthenticator is used to do simple authentication when the SMTP 
    * server requires it. 
    */ 
    private static class SMTPAuthenticator extends javax.mail.Authenticator { 

     @Override 
     protected PasswordAuthentication getPasswordAuthentication() { 

      String username = "%MyUserId%@gmail.com"; 
      String password = "%Mypassword%"; 

      return new PasswordAuthentication(username, password); 
     } 
    } 

    public String getToList() { 
     return toList; 
    } 

    public void setToList(String toList) { 
     this.toList = toList; 
    } 

    public String getCcList() { 
     return ccList; 
    } 

    public void setCcList(String ccList) { 
     this.ccList = ccList; 
    } 

    public String getBccList() { 
     return bccList; 
    } 

    public void setBccList(String bccList) { 
     this.bccList = bccList; 
    } 

    public String getSubject() { 
     return subject; 
    } 

    public void setSubject(String subject) { 
     this.subject = subject; 
    } 

    public void setFrom(String from) { 
     this.from = from; 
    } 

    public void setTxtBody(String body) { 
     this.txtBody = body; 
    } 

    public void setHtmlBody(String body) { 
     this.htmlBody = body; 
    } 

    public String getReplyToList() { 
     return replyToList; 
    } 

    public void setReplyToList(String replyToList) { 
     this.replyToList = replyToList; 
    } 

    public boolean isAuthenticationRequired() { 
     return authenticationRequired; 
    } 

    public void setAuthenticationRequired(boolean authenticationRequired) { 
     this.authenticationRequired = authenticationRequired; 
    } 

} 

的Gmail發件人:

public class GMailSender extends javax.mail.Authenticator { 

    private static final String LOG_TAG = "GmailSenderService"; 
    private String mailhost = "smtp.gmail.com"; 
    private String popMailHost = "pop.gmail.com"; 
    private String user; 
    private String password; 
    private Session session; 

    static { 
     Security.addProvider(new JSSEProvider()); 
    } 

    public GMailSender(String user, String password) { 
     this.user = user; 
     this.password = password; 

     Properties props = new Properties(); 
     props.setProperty("mail.transport.protocol", "smtp"); 
     props.setProperty("mail.host", mailhost); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.port", "465"); 
     props.put("mail.smtp.socketFactory.port", "465"); 
     props.put("mail.smtp.socketFactory.class", 
       "javax.net.ssl.SSLSocketFactory"); 
     props.put("mail.smtp.socketFactory.fallback", "false"); 
     props.setProperty("mail.smtp.quitwait", "false"); 

     session = Session.getDefaultInstance(props, this); 
     session.setDebug(true); 
    } 

    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(user, password); 
    } 

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception { 
     try{ 
     MimeMessage message = new MimeMessage(session); 
     DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain")); 
     message.setSender(new InternetAddress(sender)); 
     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)); 

     Address[] fromAddressArray = message.getFrom(); 
     for(Address address : fromAddressArray) 
     { 
      Log.d(LOG_TAG,"from = " + address.toString()+", "); 
     } 

     Address[] recipientAddressArray = message.getAllRecipients(); 
     for(Address address : recipientAddressArray) 
     { 
      Log.d(LOG_TAG,"recipient = " + address.toString()+", "); 
     } 

     Address[] replyToAddressArray = message.getReplyTo(); 
     for(Address address : replyToAddressArray) 
     { 
      Log.d(LOG_TAG,"replyTo = " + address.toString()+", "); 
     } 

     Transport.send(message); 
     }catch(Exception e){ 

     } 
    } 

    public class ByteArrayDataSource implements DataSource { 
     private byte[] data; 
     private String type; 

     public ByteArrayDataSource(byte[] data, String type) { 
      super(); 
      this.data = data; 
      this.type = type; 
     } 

     public ByteArrayDataSource(byte[] data) { 
      super(); 
      this.data = data; 
     } 

     public void setType(String type) { 
      this.type = type; 
     } 

     public String getContentType() { 
      if (type == null) 
       return "application/octet-stream"; 
      else 
       return type; 
     } 

     public InputStream getInputStream() throws IOException { 
      return new ByteArrayInputStream(data); 
     } 

     public String getName() { 
      return "ByteArrayDataSource"; 
     } 

     public OutputStream getOutputStream() throws IOException { 
      throw new IOException("Not Supported"); 
     } 
    } 
} 

登錄貓:

08-15 00:04:23.676: D/EmailService(522): -------On create called------- 
08-15 00:04:23.686: D/EmailService(522): -----Inside On Start Command----- 
08-15 00:04:23.686: D/MailService(522): Inside Send ! 
08-15 00:04:23.706: D/MailService(522): auth = [email protected] 
08-15 00:04:23.716: I/System.out(522): DEBUG: setDebug: JavaMail version 1.4.1 
08-15 00:04:23.776: D/MailService(522): from = "[email protected]" <[email protected]>, 
08-15 00:04:23.776: D/MailService(522): recipient = [email protected], 
08-15 00:04:23.776: D/MailService(522): recipient = [email protected], 
08-15 00:04:23.776: D/MailService(522): replyTo = "[email protected]" <[email protected]>, 
08-15 00:04:23.896: I/System.out(522): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1] 
08-15 00:04:23.986: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true 
08-15 00:04:24.001: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true 
08-15 00:04:24.006: I/System.out(522): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false 
08-15 00:04:25.226: I/System.out(522): 220 mx.google.com ESMTP fu4sm8557775igc.4 
08-15 00:04:25.226: I/System.out(522): DEBUG SMTP: connected to host "smtp.gmail.com", port: 465 
08-15 00:04:25.246: I/System.out(522): EHLO localhost 
08-15 00:04:25.566: I/System.out(522): 250-mx.google.com at your service, [117.219.113.96] 
08-15 00:04:25.566: I/System.out(522): 250-SIZE 35882577 
08-15 00:04:25.566: I/System.out(522): 250-8BITMIME 
08-15 00:04:25.566: I/System.out(522): 250-AUTH LOGIN PLAIN XOAUTH 
08-15 00:04:25.566: I/System.out(522): 250 ENHANCEDSTATUSCODES 
08-15 00:04:25.566: I/System.out(522): DEBUG SMTP: Found extension "SIZE", arg "35882577" 
08-15 00:04:25.566: I/System.out(522): DEBUG SMTP: Found extension "8BITMIME", arg "" 
08-15 00:04:25.566: I/System.out(522): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH" 
08-15 00:04:25.576: I/System.out(522): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
08-15 00:04:25.576: I/System.out(522): DEBUG SMTP: Attempt to authenticate 
08-15 00:04:25.576: I/System.out(522): AUTH LOGIN 
08-15 00:04:26.085: I/System.out(522): 334 VXNlcm5hbWU6 
08-15 00:04:26.085: I/System.out(522): MjRhZGl0aHlhQGdtYWlsLmNvbQ== 
08-15 00:04:26.408: I/System.out(522): 334 UGFzc3dvcmQ6 
08-15 00:04:26.408: I/System.out(522): I2EsZmF3a2VzKzE= 
08-15 00:04:27.365: I/System.out(522): 235 2.7.0 Accepted 
08-15 00:04:27.386: I/System.out(522): DEBUG SMTP: use8bit false 
08-15 00:04:27.488: I/System.out(522): MAIL FROM:<[email protected]> 
08-15 00:04:27.991: I/System.out(522): 250 2.1.0 OK fu4sm8557775igc.4 
08-15 00:04:27.996: I/System.out(522): RCPT TO:<[email protected]> 
08-15 00:04:28.326: I/System.out(522): 250 2.1.5 OK fu4sm8557775igc.4 
08-15 00:04:28.326: I/System.out(522): RCPT TO:<[email protected]> 
08-15 00:04:28.705: I/System.out(522): 250 2.1.5 OK fu4sm8557775igc.4 
08-15 00:04:28.705: I/System.out(522): DEBUG SMTP: Verified Addresses 
08-15 00:04:28.705: I/System.out(522): DEBUG SMTP: [email protected] 
08-15 00:04:28.736: I/System.out(522): DEBUG SMTP: [email protected] 
08-15 00:04:28.736: I/System.out(522): DATA 
08-15 00:04:29.586: I/System.out(522): 354 Go ahead fu4sm8557775igc.4 
08-15 00:04:29.656: I/System.out(522): Date: Wed, 15 Aug 2012 00:04:23 +0530 (GMT+05:30) 
08-15 00:04:29.656: I/System.out(522): From: "[email protected]" <[email protected]> 
08-15 00:04:29.656: I/System.out(522): Reply-To: "[email protected]" <[email protected]> 
08-15 00:04:29.656: I/System.out(522): To: [email protected], [email protected] 
08-15 00:04:29.656: I/System.out(522): Message-ID: <[email protected]> 
08-15 00:04:29.656: I/System.out(522): Subject: Test 
08-15 00:04:29.656: I/System.out(522): MIME-Version: 1.0 
08-15 00:04:29.656: I/System.out(522): Content-Type: multipart/related; 
08-15 00:04:29.656: I/System.out(522): boundary="----=_Part_0_1079137608.1344969263768" 
08-15 00:04:29.656: I/System.out(522): 
08-15 00:04:29.656: I/System.out(522): ------=_Part_0_1079137608.1344969263768 
08-15 00:04:29.656: I/System.out(522): Content-Type: text/plain; charset=us-ascii 
08-15 00:04:29.656: I/System.out(522): Content-Transfer-Encoding: 7bit 
08-15 00:04:29.656: I/System.out(522): 
08-15 00:04:29.656: I/System.out(522): Hi This is 5554 from Mail Service 
08-15 00:04:29.656: I/System.out(522): ------=_Part_0_1079137608.1344969263768-- 
08-15 00:04:29.666: I/System.out(522): . 
08-15 00:04:31.030: I/System.out(522): 250 2.0.0 OK 1344969267 fu4sm8557775igc.4 
08-15 00:04:31.035: I/System.out(522): QUIT 
08-15 00:04:31.055: D/EmailService(522): true 
08-15 00:04:31.075: I/System.out(522): DEBUG: setDebug: JavaMail version 1.4.1 
08-15 00:04:31.095: D/GmailSenderService(522): from = [email protected], 
08-15 00:04:31.105: D/GmailSenderService(522): recipient = [email protected], 
08-15 00:04:31.115: D/GmailSenderService(522): replyTo = [email protected], 
08-15 00:04:31.145: I/System.out(522): DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc.,1.4.1] 
08-15 00:04:31.145: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true 
08-15 00:04:31.156: I/System.out(522): DEBUG SMTP: useEhlo true, useAuth true 
08-15 00:04:31.156: I/System.out(522): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false 
08-15 00:04:32.478: I/System.out(522): 220 mx.google.com ESMTP bp8sm22904456igb.12 
08-15 00:04:32.498: I/System.out(522): DEBUG SMTP: connected to host "smtp.gmail.com", port: 465 
08-15 00:04:32.498: I/System.out(522): EHLO localhost 
08-15 00:04:32.905: I/System.out(522): 250-mx.google.com at your service, [117.219.113.96] 
08-15 00:04:32.905: I/System.out(522): 250-SIZE 35882577 
08-15 00:04:32.905: I/System.out(522): 250-8BITMIME 
08-15 00:04:32.925: I/System.out(522): 250-AUTH LOGIN PLAIN XOAUTH 
08-15 00:04:32.925: I/System.out(522): 250 ENHANCEDSTATUSCODES 
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "SIZE", arg "35882577" 
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "8BITMIME", arg "" 
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH" 
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg "" 
08-15 00:04:32.960: I/System.out(522): DEBUG SMTP: Attempt to authenticate 
08-15 00:04:32.960: I/System.out(522): AUTH LOGIN 
08-15 00:04:33.527: I/System.out(522): 334 VXNlcm5hbWU6 
08-15 00:04:33.527: I/System.out(522): MjRhZGl0aHlhQGdtYWlsLmNvbQ== 
08-15 00:04:33.915: I/System.out(522): 334 UGFzc3dvcmQ6 
08-15 00:04:33.915: I/System.out(522): I2EsZmF3a2VzKzE= 
08-15 00:04:34.785: I/System.out(522): 235 2.7.0 Accepted 
08-15 00:04:34.807: I/System.out(522): DEBUG SMTP: use8bit false 
08-15 00:04:34.815: I/System.out(522): MAIL FROM:<[email protected]> 
08-15 00:04:35.239: I/System.out(522): 250 2.1.0 OK bp8sm22904456igb.12 
08-15 00:04:35.245: I/System.out(522): RCPT TO:<[email protected]> 
08-15 00:04:35.635: I/System.out(522): 250 2.1.5 OK bp8sm22904456igb.12 
08-15 00:04:35.635: I/System.out(522): DEBUG SMTP: Verified Addresses 
08-15 00:04:35.646: I/System.out(522): DEBUG SMTP: [email protected] 
08-15 00:04:35.666: I/System.out(522): DATA 
08-15 00:04:36.539: I/System.out(522): 354 Go ahead bp8sm22904456igb.12 
08-15 00:04:36.605: I/System.out(522): Sender: [email protected] 
08-15 00:04:36.605: I/System.out(522): To: [email protected] 
08-15 00:04:36.605: I/System.out(522): Message-ID: <[email protected]> 
08-15 00:04:36.605: I/System.out(522): Subject: Test Subject 
08-15 00:04:36.605: I/System.out(522): MIME-Version: 1.0 
08-15 00:04:36.605: I/System.out(522): Content-Type: text/plain; charset=us-ascii 
08-15 00:04:36.605: I/System.out(522): Content-Transfer-Encoding: 7bit 
08-15 00:04:36.605: I/System.out(522): 
08-15 00:04:36.605: I/System.out(522): Hi, This is 5554 from Gmail Sender 
08-15 00:04:36.605: I/System.out(522): . 
08-15 00:04:37.896: I/System.out(522): 250 2.0.0 OK 1344969274 bp8sm22904456igb.12 
08-15 00:04:37.896: I/System.out(522): QUIT 
+0

我的意圖中已經有了這些標誌。這不是用戶活動。我正嘗試使用意向INTENT.SENDTO啓動現有的活動。 我注意到的奇怪的事情是錯誤:引起:android.content.ActivityNotFoundException:沒有找到處理Intent的活動{act = android.intent.action.SENDTO flg = 0x10000004(有額外)} – Adithya

+0

從logcat發佈stacktrace 。同時發佈您用來啓動服務的代碼。也發佈代碼在服務的'onCreate()' –

+0

只是爲了添加..我試圖從模擬器發送電子郵件。我已成功使用意向從模擬器發送電子郵件。只是,當我從後臺使用java郵件API嘗試時,似乎沒有發生任何事情。 – Adithya

回答

4

,你得到

android.content.ActivityNotFoundException: No Activity found to handle Intent { 
    act=android.intent.action.SENDTO flg=0x10000004 (has extras) } 

的原因是因爲你從來沒有設置數據在意圖。 Android會嘗試查找可處理「SENDTO」操作和您提供的數據的活動。但是您沒有提供任何數據,因此無法找到合適的活動。你有這樣的代碼:

Intent emailIntent = new Intent(Intent.ACTION_SENDTO); 
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
emailIntent.addFlags(Intent.FLAG_FROM_BACKGROUND); 
// emailIntent.setType("plain/text"); 
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
      new String[] { "[email protected]" }); 
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, 
      "Email from Intent"); 
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, 
      "Hi, This is 5554 from intent"); 
// getApplicationContext().startActivity(Intent.createChooser(emailIntent, "Send mail...")); 
Log.d(LOG_TAG, "context = " + getApplicationContext()); 
startActivity(emailIntent); 

,但你需要調用emailIntent.setdataAndType()emailIntent.setData()1 and emailIntent.setType()`。

+0

謝謝,但我從啓動器啓動電子郵件活動時並未使用setData。那次我能夠成功發送郵件。 無論如何,我想從背景發送電子郵件,但一直沒有能夠這樣做!任何幫助將是偉大的 – Adithya

+0

從您發佈的日誌,它看起來像電子郵件發送和接受。它是否着陸在垃圾郵件文件夾中?您可以嘗試將電子郵件發送到其他服務,並查看是否有效。 –

+0

嘿..我總是收到郵件..它不是在垃圾郵件,但是它是在一些其他文件夾,因爲我的過濾器:) ..感謝很多。 – Adithya

0

看這個行中登錄:

android.util.AndroidRuntimeException:從外部 一個活動的上下文中調用startActivity()需要FLAG_ACTIVITY_NEW_TASK 標誌。這真的是你想要的嗎?

其中明確指出您需要FLAG_ACTIVITY_NEW_TASK才能從Service啓動Activity。所以加

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.addFlags(Intent.FLAG_FROM_BACKGROUND); 

啓動活動從服務

+0

我在我的代碼中有這些標誌。 – Adithya

0

使用FLAG_ACTIVITY_NEW_TASK

Intent myIntent = new Intent(getBaseContext(), myActivity.class); 
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_FROM_BACKGROUND); 
getApplication().startActivity(myIntent); 
+0

我的意圖中已經有了這些標誌。這不是用戶活動。我正嘗試使用意向INTENT.SENDTO啓動現有的活動。 – Adithya