2015-04-17 53 views
0

代碼發送郵件正在運行,但問題是,讀音字只能從一個電子郵件ID發送郵件,如果我嘗試從另一個電子郵件ID發送郵件,則郵件沒有發送。 當我配置自費來發送郵件,然後它工作正常,但對其他電子郵件ACC它不工作。我的程序沒有用戶交互的Android

公共類MainActivity擴展活動{

String username="[email protected]"; 
String password="********"; 
String subject="registration confirmation"; 
String messageBody="Thanks for installing app"; 
String email="[email protected]"; 


public void send(View view) 
{ 
    sendMail(email,subject,messageBody); 
} 




private void sendMail(String email, String subject, String messageBody) { 
    Session session = createSessionObject(); 

    try { 
     Message message = createMessage(email, subject, messageBody, session); 
     new SendMailTask().execute(message); 
    } catch (AddressException e) { 
     e.printStackTrace(); 
    } catch (MessagingException e) { 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 
} 

private Session createSessionObject() { 

    Properties properties = new Properties(); 
    properties.put("mail.smtp.auth", "true"); 
    properties.put("mail.smtp.starttls.enable", "true"); 
    properties.put("mail.smtp.host", "smtp.gmail.com"); 
    properties.put("mail.smtp.port", "587"); 

    return Session.getInstance(properties, new javax.mail.Authenticator() { 
     protected PasswordAuthentication getPasswordAuthentication() { 
      return new PasswordAuthentication(username, password); 
     } 
    }); 
} 

private Message createMessage(String email, String subject,String messageBody, Session session) throws MessagingException, UnsupportedEncodingException { 
    Message message = new MimeMessage(session); 
    message.setFrom(new InternetAddress(username,username)); 
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(email,email)); 
    message.setSubject(subject); 
    message.setText(messageBody); 
    return message; 
} 

private class SendMailTask extends AsyncTask<Message, Void, Void> { 
    private ProgressDialog progressDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Sending mail", true, false); 
    } 

    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     progressDialog.dismiss(); 
     Context context = getApplicationContext(); 
     CharSequence text = "u r successfully registered "; 
     int duration = Toast.LENGTH_SHORT; 

     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 

    } 

    @Override 
    protected Void doInBackground(Message... messages) { 
     try { 
      Transport.send(messages[0]); 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 



} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

+0

它可能只適用於您的郵件,因爲這是您的手機配置的帳戶。 – Psypher

+0

開始我還以爲一樣ü說,但我在其他Android手機試過,同樣的事情又發生了....您使用的口令是什麼 –

+0

?它是一個OUTH令牌或用於登錄的強密碼? – Psypher

回答

0

的問題是其他電子郵件的ID的,我是想用來發送郵件的認證.. 解決方案是如果烏拉圭回合使用Gmail用於發送郵件,然後

  1. 登錄自己的Gmail ID
  2. 轉到帳戶
  3. 找到
  4. 下籤署「不夠安全的應用接入」只是改變這個選項允許..

    現在ü將能夠從不同的Gmail ID的發送郵件與正確的用戶名,密碼細節..

相關問題