本文介绍了如何使用JavaMail API发送电子邮件在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发在Android应用程序的电子邮件,而无需使用电子邮件客户端(没有用户交互)。我有code和我想在我的应用程序。但是,当我试图运行,这表明通知消息派。但我没有收到我的code提到的特定ID的任何电子邮件。我没有得到任何错误或警告了。请谁能帮助我走出这个问题。谢谢

这是我的code

  //导入文件    公共类SendReport扩展ActionBarActivity实现View.OnClickListener {    Button按钮;
    EDITTEXT的EditText,editText2;
    串收件人;
    会话的会话= NULL;
    ProgressDialog pdialog = NULL;
    上下文的背景下= NULL;
    字符串的TextMessage = NULL;
    字符串username [email protected];
    字符串密码=senderpassword;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_send_report);
        上下文=这一点;
        按钮=(按钮)findViewById(R.id.sendbutton);
        EDITTEXT =(EditText上)findViewById(R.id.textView2);
        editText2 =(EditText上)findViewById(R.id.textView4);
        button.setOnClickListener(本);
        }    @覆盖
    公共无效的onClick(视图v){
        。收件人= editText.getText()的toString();
        。的TextMessage = editText2.getText()的toString();        属性道具=新特性();
        props.put(mail.smtp.auth,真);
        props.put(mail.smtp.starttls.enable,真);
        props.put(mail.smtp.host,smtp.gmail.com);
        props.put(mail.smtp.port,587);
        会议=作为Session.getDefaultInstance(道具,新javax.mail.Authenticator(){
            受保护的PasswordAut​​hentication的getPasswordAut​​hentication(){
                返回新的PasswordAut​​hentication(用户名,密码);
            }
        });
        pdialog = ProgressDialog.show(背景下,,发送邮件...,真正的);
        RetrieveFeedTask任务=新RetrieveFeedTask();
        task.execute();
    } 类RetrieveFeedTask扩展的AsyncTask<弦乐,太虚,字符串> {       保护字符串doInBackground(字符串... PARAMS){           尝试
           {               的MimeMessage消息=新的MimeMessage(会话);
               message.setFrom(新网际地址([email protected]));
               message.setRecipients(javax.mail.Message.RecipientType.TO,InternetAddress.parse(收件人));
               message.setContent(TextMessage中,text / html的;字符集= UTF-8);
               Transport.send(消息);
           }           赶上(MessagingException E)
           {
               e.printStackTrace();
           }           赶上(例外五)
           {
               e.printStackTrace();
           }           返回null;
       }
        保护无效onPostExecute(字符串供稿){
              pdialog.dismiss();
              editText2.setText();
              editText.setText();
              Toast.makeText(getApplicationContext(),消息派,Toast.LENGTH_SHORT).show();
        }
    }


解决方案

我在几个月前有这个问题。

您必须允许不够安全的应用访问您的谷歌帐户。

更改在这里

I want to send an email in android app without using email client(without user interaction). I have code and I tried it in my app. But when I tried to run, it showed a notification "Message sent". But I did not receive any email from the particular id which I mentioned in the code. I did not get any error or warnings too. Please anyone help me to get out of this problem. Thanks

Here is my code

    //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 = "[email protected]";
    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("[email protected]"));
               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();
        }
    }
解决方案

I had this problem some months ago.

You have to allowing less secure apps to access your google account.

Change it here

Now your email is correctly sent ;)

Info : https://support.google.com/accounts/answer/6010255?hl=en

这篇关于如何使用JavaMail API发送电子邮件在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:33