0

I want to develop an application from which user can send an email (without using Intent) from anywhere and on the other hand it can be received by me on my Gmail id.

Here's my code, when I click on button..

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

And the other class I used is..

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

The problem is that my code is neither sending emails nor showing any errors.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Zaid S Ahmed
  • 53
  • 1
  • 7

2 Answers2

1

Finally, found one possible answer to this question after a long research.

Simply, in my android code I used AsyncTask and passed the values of my EditText from a form in a url like this:

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

And catch these variables on a PHP page which has PHP mail function in it.

PHP Code:

<?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";
?>
Zaid S Ahmed
  • 53
  • 1
  • 7
0

Integrated java mail api to your code.JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. You can download it from here: https://code.google.com/p/javamail-android/downloads/list

or

you can send email by using smtp: Sending mail in android without intents using SMTP

For Reference: http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android

Community
  • 1
  • 1
  • Thanks @Azfaar.. I checked both the links you gave, but bro its not working for me.. :( – Zaid S Ahmed Jan 11 '16 at 05:48
  • The reference link you gave.. I used that code initially.. The problem is that its neither showing any error nor sending an email.. :( And the other link is crashing the application as I click on SEND button.. :( – Zaid S Ahmed Jan 11 '16 at 05:58
  • check out this question : http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a – Azfaar kabir Siddiqui Jan 11 '16 at 06:09
  • another good working tutorial for your scenario using javamail api :http://mrbool.com/how-to-work-with-java-mail-api-in-android/27800 – Azfaar kabir Siddiqui Jan 11 '16 at 06:10
  • again bro I am having the same issue.. :( I have used this link's solution too : stackoverflow.com/questions/2020088/ but no luck.. same,, no error and no email sent.. :( and the other link crashes my app as i click on button.. :( – Zaid S Ahmed Jan 11 '16 at 09:21