I have created one contact us activity in my android application. It contain 4 edit text such as name, phone number, email and message along with the submit button. What i have to do is when i will click on submit button then the data from all four edit text should be E-mailed to me on my mail id. Can anyone suggest how can i do so? If any further details are required then please let me know. Thanks Here is some code sample.
String Name = name.getText().toString();
String Phone = phone.getText().toString();
String Email = email.getText().toString();
String Message = message.getText().toString();
//check whether the msg empty or not
if (Name.length() != 0 && Phone.length() != 0 && Email.length() != 0) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.abcd.com/Server1.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "01"));
nameValuePairs.add(new BasicNameValuePair("message", Name));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
name.setText(""); //reset the message text field
phone.setText(""); //reset the message text field
email.setText(""); //reset the message text field
message.setText(""); //reset the message text field
Toast.makeText(getBaseContext(), "Sent", Toast.LENGTH_SHORT).show();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
//display message if text field is empty
Toast.makeText(getBaseContext(), "All fields are required", Toast.LENGTH_SHORT).show();
}
}
I have written this code. It is not giving any error. App is also not crashing but the data is not getting on email. Any Help?
public class ContactUs extends AppCompatActivity {
private EditText name, phone, email, msg;
private Button submitButton;
private TextInputLayout inputLayoutName, inputLayoutPhone, inputLayoutEmail, inputLayoutMessage;
EditText editText;
//New Code
Session session = null;
ProgressDialog pdialog = null;
Context context = null;
//private EditText name, phone, email, message;
String Name, Phone, Email, Msg;
String receiver = "xyz@gmail.com";
String sender = "abc@gmail.com";
String subject = "Test Mail";
String textMessage = "Hello To All";
String Data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_us);
initializeWidgets();
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isValid = true;
if (name.getText().toString().isEmpty()) {
inputLayoutName.setError("Name is mandatory");
isValid = false;
} else {
inputLayoutName.setErrorEnabled(false);
}
if (phone.getText().toString().isEmpty()) {
inputLayoutPhone.setError("Phone number is mandatory");
isValid = false;
} else {
inputLayoutPhone.setErrorEnabled(false);
}
if (email.getText().toString().isEmpty()) {
inputLayoutEmail.setError("Email is required");
isValid = false;
} else {
inputLayoutEmail.setErrorEnabled(false);
}
if (isValid) {
String Name = name.getText().toString();
String Phone = phone.getText().toString();
String Email = email.getText().toString();
String Message = msg.getText().toString();
Data = Name + Phone + Email + Message;
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.socketFactory.port", "587");
prop.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.port", "587");
session = Session.getDefaultInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication passwordAuthentication = new PasswordAuthentication(sender, "abc");
return passwordAuthentication;
} });
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(receiver));
message.setText(Data);
Transport.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}
name.setText("");
phone.setText("");
email.setText("");
msg.setText("");
Toast.makeText(getApplicationContext(), "Data Submitted Successfully", Toast.LENGTH_LONG).show();
}
});
}
private void initializeWidgets() {
name = (EditText) findViewById(R.id.name);
phone = (EditText) findViewById(R.id.phone);
email = (EditText) findViewById(R.id.email);
msg = (EditText) findViewById(R.id.message);
submitButton = (Button) findViewById(R.id.btnSubmit);
inputLayoutName = (TextInputLayout) findViewById(R.id.inputLayoutName);
inputLayoutPhone = (TextInputLayout) findViewById(R.id.inputLayoutPhone);
inputLayoutEmail = (TextInputLayout) findViewById(R.id.inputLayoutEmail);
inputLayoutMessage = (TextInputLayout) findViewById(R.id.inputLayoutMessage);
}
public boolean onSupportNavigateUp() {
onBackPressed();`enter code here`
return true;
}