我在我的android应用程序中创建了一个联系我们的活动。它包含4个编辑文本,例如姓名,电话号码,电子邮件和消息以及提交按钮。我必须做的是当我点击提交按钮时,所有四个编辑文本中的数据都应该通过电子邮件发送给我的邮件ID。任何人都可以建议我该怎么做?如果需要进一步的细节,请让我知道。 谢谢 以下是一些代码示例。如何在我们的电子邮件中获取与我们联系的数据
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();
}
}
我写了这段代码。它没有给出任何错误。应用程序也没有崩溃,但数据没有得到电子邮件。任何帮助?
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 = "[email protected]";
String sender = "[email protected]";
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;
}
我刚才想编写一些代码来这样做。但它不起作用。请除了我写的代码外。我的目标是发送所有关于邮件ID的数据,当我点击提交按钮时。 –