Android 如何在我们的电子邮件中获取联系我们表单的数据
我在android应用程序中创建了一个“联系我们”活动。它包含4个编辑文本,如姓名、电话号码、电子邮件和消息以及提交按钮。我要做的是,当我点击提交按钮时,所有四个编辑文本中的数据都应该通过我的邮件id通过电子邮件发送给我。有人能建议我怎么做吗?如果需要任何进一步的细节,请让我知道。 谢谢 下面是一些代码示例Android 如何在我们的电子邮件中获取联系我们表单的数据,android,email,Android,Email,我在android应用程序中创建了一个“联系我们”活动。它包含4个编辑文本,如姓名、电话号码、电子邮件和消息以及提交按钮。我要做的是,当我点击提交按钮时,所有四个编辑文本中的数据都应该通过我的邮件id通过电子邮件发送给我。有人能建议我怎么做吗?如果需要任何进一步的细节,请让我知道。 谢谢 下面是一些代码示例 String Name = name.getText().toString(); String Phone = phone.g
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();
}
}
您可以找到一个解决方案,用于发送带有意图的电子邮件(需要用户操作)
您必须创建一个字符串
,其中电子邮件正文包含您要发送的所有数据,然后打开电子邮件客户端,用户可以手动发送消息
否则,您可以使用smtp配置在后台发送电子邮件,而无需按照说明执行用户操作。
这种方式发送电子邮件时用户不知道,但您必须知道电子邮件帐户的配置
Gmail的以下内容(复制自):
我刚刚试着写了一些代码来做到这一点。但它不起作用。请在我写的代码之外再写一些东西。我的目标是在我点击提交按钮时发送邮件id上的所有数据。电子邮件的代码是什么。我的意思是,你能建议一些代码在如果块看看张贴的链接。在那里,您将找到要添加的代码。根据你的需要选择代码,如果你喜欢背景动作或者不喜欢兄弟,你明白我的意思吗?我希望在联系人表单中填写的用户数据应发送到特定的邮件id。因此,谁将是此处的发件人,因为收件人是固定的。您应该传递
电子邮件,而不是收件人
。我看到的是你正在给“abc@gmail.com". 它应该是这样的:message.setRecipients(MimeMessage.RecipientType.TO,InternetAddress.parse(Email))代码>如果我误解了你的观点,请告诉我。代码消息.setRecipients(mimessage.RecipientType.TO,InternetAddress.parse(receiver));已经写好了。我正在从发送数据abc@gmail.com到xyz@gmail.com. 我的观点是,用户将是匿名的。它可以在表单中填写任何数据。当他点击提交按钮时,用户的数据应该在邮件id中接收xyz@gmail.com. 发送者可以是任何人。
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;
}
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Security;
import java.util.Properties;
public class GMailSender extends javax.mail.Authenticator {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
static {
Security.addProvider(new com.provider.JSSEProvider());
}
public GMailSender(String user, String password) {
this.user = user;
this.password = password;
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
try{
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
message.setSender(new InternetAddress(sender));
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));
Transport.send(message);
}catch(Exception e){
}
}
public class ByteArrayDataSource implements DataSource {
private byte[] data;
private String type;
public ByteArrayDataSource(byte[] data, String type) {
super();
this.data = data;
this.type = type;
}
public ByteArrayDataSource(byte[] data) {
super();
this.data = data;
}
public void setType(String type) {
this.type = type;
}
public String getContentType() {
if (type == null)
return "application/octet-stream";
else
return type;
}
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data);
}
public String getName() {
return "ByteArrayDataSource";
}
public OutputStream getOutputStream() throws IOException {
throw new IOException("Not Supported");
}
}
}