Zaid S Ahmed
Zaid S Ahmed

Reputation: 53

Email sending application without using intent is not working

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 = "[email protected]";
    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.

Upvotes: 0

Views: 509

Answers (2)

Zaid S Ahmed
Zaid S Ahmed

Reputation: 53

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

Upvotes: 1

Azfaar kabir Siddiqui
Azfaar kabir Siddiqui

Reputation: 780

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

Upvotes: 0

Related Questions