繁体   English   中英

我的程序在没有用户交互的情况下无法发送邮件

[英]my program not send mail without user interaction in android

代码正在运行,但问题是我无法仅从一个电子邮件ID发送邮件,如果我尝试从另一个电子邮件ID发送邮件,则邮件不会发送。 当我配置自己的acc发送邮件时,它可以正常工作,但对于其他电子邮件acc,则不起作用。

公共类MainActivity扩展了Activity {

String username="example@gmail.com";
String password="********";
String subject="registration confirmation";
String messageBody="Thanks for installing app";
String email="example1@gmail.com";


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);
}

}

问题是我试图用于发送邮件的其他电子邮件ID的身份验证。解决方案是,如果您使用gmail发送邮件,那么

  1. 登录您的Gmail ID
  2. 转到帐户
  3. 在登录下找到“访问不太安全的应用程序”
  4. 只需将此选项更改为允许即可。

    现在,您将能够使用正确的用户名,密码详细信息从其他gmail ID发送邮件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM