不使用意图的电子邮件发送应用程序无效

时间:2016-01-10 13:08:15

标签: android email

我想开发一个应用程序,用户可以从任何地方发送电子邮件(不使用Intent),另一方面,我可以通过我的Gmail ID接收它。

这是我的代码,当我点击按钮时..

switch (v.getId()) {
    case R.id.bBack:
        finish();
    break;
    case R.id.bSend:
        // send an email
        sendEmail();
    break;
}

我使用的其他课程是......

private void sendEmail() {
    mName = name.getText().toString();
    mEmail = "something@gmail.com";
    mPassword = "password";
    mQuery = query.getText().toString();

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

    // jis email id se mail bhejni hai.. wo email id & passwrd..
    session = Session.getDefaultInstance(props, new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(mEmail, mPassword);
        }
    });
    pdialog = ProgressDialog.show(this, "", "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(mEmail));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(RecTo));

            message.setSubject(subject);
            message.setContent(mQuery, "text/plain; charset=utf-8");

            Transport.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        if (mName == "" || mEmail == "" || mPassword == "" || subject == ""
                || mQuery == "") {
            Toast.makeText(getApplicationContext(), "Enter Details",
                    Toast.LENGTH_LONG).show();
        } else {
            name.setText("");
            phone.setText("");
            query.setText("");

            Toast.makeText(getApplicationContext(), "Message Sent",
                    Toast.LENGTH_LONG).show();
        }
        pdialog.dismiss();
    }
}

问题是我的代码既不发送电子邮件也不显示任何错误。

2 个答案:

答案 0 :(得分:1)

最后,经过长时间的研究,找到了一个可能的答案。

简单地说,在我的android代码中,我使用了AsyncTask并从一个表单中传递了我的EditText的值,如下所示:

String url_select = "http://link_to_my_website/my_page.php?phone="+phone.getText().toString()+"&name="+name.getText().toString()+"&message="+query.getText().toString()+"&sender="+email.getText().toString();

在PHP页面上捕获这些变量,其中包含PHP邮件功能。

PHP代码:

<?php
$email_phone_no=$_REQUEST['phone'];
$email_name=$_REQUEST['name'];
$email_message=$_REQUEST['message'];
$sender=$_REQUEST['sender'];

$email_to = $sender;
$subject = "My Subject";

$company="My Company";

$from = $company . ' <' . "info@my_website_link.com" . '>';

$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-Type: text/html; charset=ISO-8859-1' . "\r\n";
$headers .= "X-Priority: 3\r\n";
$headers .= "X-Mailer: PHP". phpversion() ."\r\n" ;

$headers .= 'From: My Company<info@my_website_link.com>' . "\r\n";

mail($email_to, $subject, $email_message, $headers);

echo"Email has been sent";
?>

答案 1 :(得分:0)

将java邮件api集成到您的code.JavaMail API提供了一个独立于平台且与协议无关的框架来构建邮件和消息传递应用程序。 你可以在这里下载: https://code.google.com/p/javamail-android/downloads/list

您可以使用smtp发送电子邮件: Sending mail in android without intents using SMTP

供参考: http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android