Android: Get response and send email (from gmail account)

I created an application to register a user and then send email back to him if the registration is successful. The registration process is OK but i get a NullpointerException.

MainAtivity.java:

public class MainActivity extends Activity { private EditText e_name; private EditText e_location; private Spinner e_bloodGroup; private EditText e_mobile; private EditText e_email; private EditText e_password; private Switch e_userType; private ProgressDialog pDialog; String M_username, M_location, M_bloodType, M_email, M_password; int M_mobile; boolean M_userType; private TextView tv; private SoapObject response; private static final String SOAP_ACTION = "http://*****.org/InsertRecord"; //private static final String METHOD_NAME_1 = "InsertRecord"; private static final String NAMESPACE = "http://*****.org/"; private static final String URL= "http://*********************/Service.asmx?wsdl"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); e_name = (EditText) findViewById(R.id.txtUsername); e_location = (EditText) findViewById(R.id.txtLocation); e_bloodGroup = (Spinner) findViewById(R.id.spnrBloodG); e_mobile = (EditText) findViewById(R.id.txtMobile); e_email = (EditText) findViewById(R.id.txtEmail); e_password = (EditText) findViewById(R.id.txtPassword); e_userType = (Switch) findViewById(R.id.switch1); Spinner spinner = (Spinner) findViewById(R.id.spnrBloodG); ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Blood_Groups, android.R.layout.simple_dropdown_item_1line); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); final ImageButton Signup = (ImageButton) this.findViewById(R.id.btnSignup); Signup.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { M_username = e_name.getText().toString(); M_location = e_location.getText().toString(); M_bloodType = e_bloodGroup.getSelectedItem().toString(); M_mobile = Integer.parseInt(e_mobile.getText().toString()); M_email = e_email.getText().toString(); M_password = e_password.getText().toString(); M_userType = e_userType.isChecked(); try { new myAsyncTask(M_username, M_location, M_bloodType, M_mobile, M_email, M_password, M_userType).execute(); } catch (Exception e) { tv.setText(e.toString()); } String emailBody = "You have been registered SUCCESSFULLY./n Please reply to any blood request. Thank you."; List<String> mailList = new ArrayList<>(); mailList.add(M_email); GMail Mail = new GMail("[email protected]", "*****", mailList, "(noreply) Registered Successfully", emailBody); try { Mail.sendEmail(); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }); } private class myAsyncTask extends AsyncTask<String, String, String> { private String name, location, bloodType, email, password; private int mobile; boolean userType; public myAsyncTask(String _name, String _location, String _bloodType, int _mobile, String _email, String _password, boolean _userType) { name = _name; location = _location; bloodType = _bloodType; mobile = _mobile; email = _email; password = _password; userType = _userType; } @Override protected void onPostExecute(String result) { super.onPostExecute(result); tv.setText(result); } @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Please Wait.."); pDialog.setIndeterminate(false); pDialog.setCancelable(true); pDialog.show(); } protected void onProgressUpdate(Integer... values) { } protected String doInBackground(String... params) { SoapObject request = new SoapObject(NAMESPACE, "InsertRecord"); Donor donor = new Donor(1, name, location, bloodType, mobile, email, password, userType); request.addProperty("d", donor); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.dotNet = true; envelope.implicitTypes = true; envelope.setAddAdornments(false); envelope.setOutputSoapObject(request); HttpTransportSE httpTransport = getHttpTransportSE(URL); try { httpTransport.call(SOAP_ACTION, envelope); // Get the SoapResult from the envelope body. response = (SoapObject) envelope.getResponse(); String requestt = httpTransport.requestDump; String response = httpTransport.responseDump; SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse(); return response.toString(); } catch (HttpResponseException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.toString(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.toString(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); return e.toString(); } //send request } } static private final HttpTransportSE getHttpTransportSE(String requestURL) { HttpTransportSE ht = new HttpTransportSE(Proxy.NO_PROXY, requestURL, 60000); ht.debug = true; //ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"); return ht; } }

Donor.java:

public class Donor implements KvmSerializable { private int id; private String name; private String location; private String bloodType; private int mobile; private String email; private String password; private boolean userType; public Donor(){} public Donor(int id, String name, String location, String bloodType, int mobile, String email, String password, boolean userType) { this.id = id; this.name = name; this.location = location; this.bloodType = bloodType; this.mobile = mobile; this.email = email; this.password = password; this.userType = userType; } public Object getProperty(int arg0) { switch(arg0) { case 0: return id; case 1: return name; case 2: return location; case 3: return bloodType; case 4: return mobile; case 5: return email; case 6: return password; case 7: return userType; } return null; } public int getPropertyCount() { return 7; } public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) { switch(index) { case 0: info.type = PropertyInfo.INTEGER_CLASS; info.name = "id"; break; case 1: info.type = PropertyInfo.STRING_CLASS; info.name = "name"; break; case 2: info.type = PropertyInfo.STRING_CLASS; info.name = "location"; break; case 3: info.type = PropertyInfo.STRING_CLASS; info.name = "bloodType"; break; case 4: info.type = PropertyInfo.INTEGER_CLASS; info.name = "mobile"; break; case 5: info.type = PropertyInfo.STRING_CLASS; info.name = "email"; break; case 6: info.type = PropertyInfo.STRING_CLASS; info.name = "password"; break; case 7: info.type = PropertyInfo.BOOLEAN_CLASS; info.name = "userType"; break; default:break; } } @Override public String getInnerText() { return null; } @Override public void setInnerText(String s) { } public void setProperty(int index, Object value) { switch(index) { case 0: id = Integer.parseInt(value.toString()); break; case 1: name = value.toString(); break; case 2: location = value.toString(); break; case 3: bloodType = value.toString(); break; case 4: mobile = Integer.parseInt(value.toString()); break; case 5: email = value.toString(); break; case 6: password = value.toString(); break; case 7: userType = Boolean.parseBoolean(value.toString()); break; default: break; } } }

GMail.java:

package exercise.com.myapplication; import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import android.util.Log; public class GMail extends javax.mail.Authenticator{ final String emailPort = "587";// gmail's smtp port final String smtpAuth = "true"; final String starttls = "true"; final String emailHost = "smtp.gmail.com"; String fromEmail; String fromPassword; List toEmailList; String emailSubject; String emailBody; Properties emailProperties=new Properties(); Session mailSession; MimeMessage emailMessage; public GMail() { } public GMail(String fromEmail, String fromPassword, List toEmailList, String emailSubject, String emailBody) { this.fromEmail = fromEmail; this.fromPassword = fromPassword; this.toEmailList = toEmailList; this.emailSubject = emailSubject; this.emailBody = emailBody; //emailProperties = System.getProperties(); emailProperties.put("mail.smtp.port", emailPort); emailProperties.put("mail.smtp.auth", smtpAuth); emailProperties.put("mail.smtp.starttls.enable", starttls); Log.i("GMail", "Mail server properties set."); } public MimeMessage createEmailMessage() throws AddressException, MessagingException, UnsupportedEncodingException { mailSession = Session.getDefaultInstance(emailProperties, null); // mailSession.setDebug(true); emailMessage = new MimeMessage(mailSession); emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail)); for (Object toEmail : toEmailList) { //Log.i("GMail","toEmail: "+toEmail); emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress((String) toEmail)); } emailMessage.setSubject(emailSubject); emailMessage.setText(emailBody);// for a text email return emailMessage; } public void sendEmail() throws AddressException, MessagingException, UnsupportedEncodingException { Transport transport = mailSession.getTransport("smtp"); transport.connect(emailHost, fromEmail, fromPassword); createEmailMessage(); //Log.i("GMail","allrecipients: "+emailMessage.getAllRecipients()); transport.sendMessage(emailMessage, emailMessage.getAllRecipients()); transport.close(); //Log.i("GMail", "Email sent successfully."); } }

Can you please help me how to get the response and send email correctly? Thanks in advance

Category:android Views:0 Time:2018-05-11

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.188 (s). 11 q(s)