使用应用发送邮件

时间:2015-06-19 14:31:40

标签: android javamail

我想从我的应用程序按下按钮后发送邮件。但是这个应用什么都不做,这个代码有什么问题?

public void onClick(View v) {       

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    session = Session.getDefaultInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("test@gmail.com", "14lalal");
        }
    });

    pDialog = ProgressDialog.show(MainActivity.sActivity , "", "Sending Mail...", true);

    RetreiveFeedTask task = new RetreiveFeedTask();
    task.execute();
}

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

    @Override
    protected String doInBackground(String... params) {

        try{
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("test@gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("test2@gmail.com"));
            message.setSubject("lala");
            message.setText("gldf");
            Transport.send(message);
        } catch(MessagingException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        pDialog.dismiss();
        Toast.makeText(mContext, "Message sent", Toast.LENGTH_LONG).show();
    }
}
}

AlertDialog显示正确,但我没有收到任何邮件。

0 个答案:

没有答案