2

I am developing an app in which I want to send mail from app without user intervention (without using intents), and it is not good to take the user credentials (without using Java mail API), but how to bind the body of the mail from Android application to Web API? Here is my code:

 public void onClick(View v) {
 if (v == btnSend) {
 String url = "http://app.xyz.com/api/SendMail/[email protected]" + "&Subject=" + "&Body=";
       if (url != null) {
            email = editTextEmail.getText().toString();
            subject = editTextSubject.getText().toString();
            message = editTextMessage.getText().toString();
            final Intent emailIntent = new Intent( android.content.Intent.ACTION_SEND);
            emailIntent.setType("plain/text");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
            new String[]{"[email protected]"});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject);
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
            this.startActivity(Intent.createChooser(emailIntent, "Sending email..."));

           }
           }catch (Throwable t) {
            Toast.makeText(this,
           "Request failed try again: " + t.toString(),
            Toast.LENGTH_LONG).show();
       }
    }
22
  • you can send using SMTP Commented Nov 11, 2016 at 7:11
  • Possible duplicate of Sending Email in Android using JavaMail API without using the default/built-in app Commented Nov 11, 2016 at 7:13
  • While posting this question i have searched in stack overflow.But i don't get the proper method.First understand my post.while using Java Mail API,it will ask user to enter name and password.And i don't want to user password in my app because of security. Commented Nov 11, 2016 at 7:43
  • Miss that user name and password is for SMTP so you can add it statically Commented Nov 11, 2016 at 7:46
  • Please explain me how to implement Commented Nov 11, 2016 at 7:49

1 Answer 1

0

Finally I got solution.In order send mail using Amazon SES.

We should have Amazon credentials.Use that Credentials and write a controller in .net using web API and call the URL in our android application.

Below is my android code.

public void onCreate(Bundle savedInstanceState)
   {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.feed_back);
    scanBtn = (Button) findViewById(R.id.button1);
    myAwesomeTextview = (TextView) findViewById(R.id.myAwesomeTextview);
    scanBtn.setOnClickListener(new View.OnClickListener()
    {
    public void onClick(View view)
    {
    mEdit = (EditText) findViewById(R.id.editText1);
    mText = (TextView) findViewById(R.id.textView2);
    Subject = mEdit.getText().toString();
    mEdit1 = (EditText) findViewById(R.id.editText2);
    mText1 = (TextView) findViewById(R.id.textView3);
    Body = mEdit1.getText().toString();
    Log.d("###$Request URL", Subject + "");
    Log.d("###$Request URL", Body + "");
    SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE);
    String  e = sharedpreferences.getString(main.email,"");
    String url ="http://app.xyz.com/Api/SendMail/SendMail?Emailid="+ e + "&Subject="+ Subject+"&Body=" +Body;
    AQuery mAQuery = new AQuery(FeedBack.this);
    mAQuery.ajax(url, String.class, new AjaxCallback<String>()
    {
    @Override
    public void callback(String url, String data, AjaxStatus status)
    {
    super.callback(url, data, status);
    if (BuildConfig.DEBUG)
    {
        Log.d("###$Request URL", url + "");
        Log.d("###$Response ", data + "");
        Log.d("###$Status Message : ", status.getMessage() + "");
        Log.d("###$Status Code : ", status.getCode() + "");

    }
    if(status.getCode()!=-101)
    {
       String StringData = "" + data;
       try 
       {
         JSONObject json = new JSONObject(StringData);
         String sd,hd;
         sd = json.getString("Subject");
         hd=  json.getString("Body");

       } 
       catch (Exception a) 
       {
          Toast toast = Toast.makeText(FeedBack.this,"Mail Sending", Toast.LENGTH_LONG);
          toast.setGravity(Gravity.CENTER, 0, 0);
          toast.show();
       }
       }
       else
       {
         Toast toast = Toast.makeText(FeedBack.this,"Please Check Your Internet Connection", Toast.LENGTH_LONG);
         toast.setGravity(Gravity.CENTER, 0, 0);
         toast.show();

       }
        }
        });
        }
     });
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.