android - 如何使用 javamail API 在 android 中发送电子邮件

标签 android email

我想在不使用电子邮件客户端(无需用户交互)的情况下在 Android 应用程序中发送电子邮件。我有代码,我在我的应用程序中尝试过。但是当我尝试运行时,它显示了一条通知“消息已发送”。但是我没有收到任何来 self 在代码中提到的特定 id 的电子邮件。我也没有收到任何错误或警告。请任何人帮助我摆脱这个问题。谢谢

这是我的代码

    //Import files

    public class SendReport extends ActionBarActivity implements View.OnClickListener {

    Button button;
    EditText editText, editText2;
    String recipient;
    Session session = null;
    ProgressDialog pdialog = null;
    Context context = null;
    String textmessage = null;
    String username = "sender@gmail.com";
    String password = "senderpassword";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_report);
        context = this;
        button = (Button) findViewById(R.id.sendbutton);
        editText = (EditText) findViewById(R.id.textView2);
        editText2 = (EditText) findViewById(R.id.textView4);
        button.setOnClickListener(this);
        }

    @Override
    public void onClick(View v) {
        recipient = editText.getText().toString();
        textmessage = editText2.getText().toString();

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username,password);
            }
        });
        pdialog = ProgressDialog.show(context, "", "Sending Mail...", true);
        RetrieveFeedTask task = new RetrieveFeedTask();
        task.execute();
    }

 class RetrieveFeedTask extends AsyncTask<String, Void, String> {

       protected String doInBackground(String... params) {

           try
           {

               MimeMessage message = new MimeMessage(session);
               message.setFrom(new InternetAddress("anithabadhu@gmail.com"));
               message.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));
               message.setContent(textmessage,"text/html; charset=utf-8");
               Transport.send(message);
           }

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

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

           return null;
       }
        protected void onPostExecute(String feed) {
              pdialog.dismiss();
              editText2.setText("");
              editText.setText("");
              Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
        }
    }

最佳答案

我几个月前遇到过这个问题。

您必须允许安全性较低的应用访问您的 Google 帐户。

改变它here

现在您的电子邮件必须正确发送 ;)

更多信息:https://support.google.com/accounts/answer/6010255?hl=en

关于android - 如何使用 javamail API 在 android 中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28893198/

相关文章:

php - 如何使用 php 发送带有 pdf 附件的电子邮件?

android - Open103R、Open107v开发板——如何编程?

android - Edittext 的常量后缀,即使在添加字符后也会存在

java - 在android中排序列表?

javascript - 如何从发送电子邮件中删除特定数据

spring-boot - 454 4.7.0 Spring Boot 2.1.4.RELEASE 中登录尝试次数过多

android - 带有 edittext 和 imge 但只有一个按钮的 Gridview

android - 在 Android 上进行一键登录和注册时获取 "16: Cannot find a matching credential"

php - Outlook Rest Api 发送邮件请求返回状态 400

php - zend smtp 邮件在 100 多封邮件后崩溃