4

I am developing an android app. In my android app I am having feed back form. and I take a email id from user as input. and i want when a user clicks on submit button the email should send. I dont want that user should add his/her password for that.

I have checked the default method by Intent and the second method in the below link ..

Second Method

Community
  • 1
  • 1
Riddhi Shah
  • 477
  • 7
  • 26
  • I had to do something like this in my app... The big problem with @Hajjat's answer is that it opens up tons of possible apps. The solution I used was to ask for their email, and then have the app send a HTTP request (with the email & their feedback) to a page I made, and then had the page send an email using the email that was provided. – Joseph Boyle Mar 15 '14 at 14:29

3 Answers3

6

If you want the email to be sent from the account of the user of the app, a proper authentication would be required.

As a result, either you could send an intent to email app (as you mentioned) or you could do it yourself (as in second method mentioned by you).

But the second method obviously requires password since you are sending an email on behalf of the user (from their account). Gmail (or any mail service provider as a matter of fact) won't allow that without a password.

So to answer your question in a nutshell, no. You can not do it without a password.

In stead, you could use a work around. You can send all emails sent through your apps from a single mail id (which is created by you so you know the password). Now in the content of the email, you can store the email id of the user from whom you are asking for the feedback.

This way, you just have to ask for the mail id (no password) and you also get their contact information if you want to contact them back.

Hope this helps.
Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
  • hey what if I want to configure some company email address like xyz@abc.com does the second method will work if I change the smtp client and all – Riddhi Shah Mar 15 '14 at 18:29
  • Yes.. it will. The second method will work for any smtp client (as long as api is available) since you have complete access control over the account. So it will definitely work for a company email. – Tanmay Patil Mar 15 '14 at 19:45
  • hello. Hey i configured my company email address and set up smtp but i am not able to send email through my android app. I am facing this error.javax.mail.MessagingException: Could not connect to SMTP host: mail.kunjinc.org, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. and when I try to change port number even though I am not able to send email. – Riddhi Shah Apr 12 '14 at 11:46
  • Sorry for late response. This might help you. http://developer.android.com/training/articles/security-ssl.html#HttpsExample If the problem persists, it might be a good idea to post another question with code. Good luck. – Tanmay Patil Apr 16 '14 at 05:11
2

I had the same question and found a solution that I modified to work for me. You can do a search in stack overflow but solution I used was from sending email without using defaut my token was being retrieved after the email was sent out. Anyway here is snippet of a working sample I created.

  1. I imported the following jars:
    compile files('libs/activation.jar') compile files('libs/additionnal.jar') compile files('libs/mail.jar')

  2. I had the following permission requests <uses-permission android:name="android.permission.GET_ACCOUNTS"> </uses-permission> <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"> </uses-permission> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.USE_CREDENTIALS"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

  3. Also for my scenario I asked the user for their user account. If you know it then you can skip this step. the snippet I have for this can be done another way using helper methods provided by studio libs however I just did it via a dialog.

    public Account[] getGoogleAccounts(){ return accounts = acctManager.getAccountsByType("com.google"); }

    public void getGoogleAccountsDialog(){
    if( getGoogleAccounts().length>1) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Select Google Account:")
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                })
                .setItems(convertAccountTo(getGoogleAccounts()), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        setSelectedAcct(accounts[which]);
                        initToken(activity);
                        dialog.dismiss();
                    }
                });
        builder.create();
        builder.show();
    }
    else if(getGoogleAccounts().length==1){
        setSelectedAcct(getGoogleAccounts()[0]);
    }
    else{
        Toast.makeText(context,"No google account(s) exists on this device.",Toast.LENGTH_LONG);
    }
    

    }'

  4. because this is a working sample i just have dummy text setup to fire the email immediately after the name is selected. However you will modify this code for you suiting.

Was the token is obtained I send the email request which is the getAndUseAuthTokenInAsynTask()

public void initToken(Activity ctx) {
    acctManager.getAuthToken(getSelectedAcct(), "oauth2:https://mail.google.com/", null, activity, new AccountManagerCallback<Bundle>(){
        @Override
        public void run(AccountManagerFuture<Bundle> result){
            try{
                Bundle bundle = result.getResult();
                token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                getAndUseAuthTokenInAsyncTask();
                Log.d("initToken callback", "token="+token);

            } catch (Exception e){
                Log.d("test", e.getMessage());
            }
        }
    }, null);

}
  1. lastly the remainder of the calls

    public synchronized void sendMail (String subject, String body, String user, String oauthToken, String recipients) { try {

        SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com", 587,
                user, oauthToken, true);
    
        MimeMessage message = new MimeMessage(session);
        DataHandler handler = new DataHandler(new ByteArrayDataSource(
                body.getBytes(), "text/plain"));
        message.setSender(new InternetAddress(user));
        message.setSubject(subject);
        message.setDataHandler(handler);
        if (recipients.indexOf(',') > 0)
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipients));
        else
            message.setRecipient(Message.RecipientType.TO,
                    new InternetAddress(recipients));
        smtpTransport.sendMessage(message, message.getAllRecipients());
    
    } catch (Exception e) {
        Log.d("test", e.getMessage(), e);
    }
    

    }

    public SMTPTransport connectToSmtp(String host, int port, String userEmail, String oauthToken, boolean debug) throws Exception {

    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.starttls.required", "true");
    props.put("mail.smtp.sasl.enable", "false");
    
    session = Session.getInstance(props);
    session.setDebug(debug);
    
    final URLName unusedUrlName = null;
    SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
    // If the password is non-null, SMTP tries to do AUTH LOGIN.
    final String emptyPassword = null;
    
    transport.connect(host, port, userEmail, emptyPassword);
    
    byte[] response = String.format("user=%s\1auth=Bearer %s\1\1",
            userEmail, token).getBytes();
    response = BASE64EncoderStream.encode(response);
    
    transport.issueCommand("AUTH XOAUTH2 " + new String(response), 235);
    
    return transport;
    

    }

Hope this helps someone else. Keep in mind that the sending of the mail should not be done on the main thread.

Community
  • 1
  • 1
JenniferG
  • 602
  • 1
  • 5
  • 13
0

Not sure if useful to you, but have you considered also using the built-in email functionality? This wouldn't even require the user to enter their user id nor password, but of course they'll leave your app to the email client to send the email.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"recipient@email.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sample mail");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is a sample mail..");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send mail client :"));

(Btw: This would show many other apps along the email clients. If you're interested in such a solution, I can post some code to filter out all apps but email clients)

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Hajjat
  • 300
  • 2
  • 9