2017-06-05 9 views
0

我在我的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; 
} 
+0

我刚才想编写一些代码来这样做。但它不起作用。请除了我写的代码外。我的目标是发送所有关于邮件ID的数据,当我点击提交按钮时。 –

回答

0

Here你可以找到一个意向发送电子邮件的解决方案(需要用户操作)。

您必须创建一个String,其中包含要发送的所有数据的电子邮件正文,然后打开电子邮件客户端并且用户可以手动发送邮件。

否则,您可以使用您的smtp配置在后台发送电子邮件,无需用户操作,如here所述。 这种方式发送电子邮件时没有用户的知识,但你必须知道你的电子邮件帐户的配置。

以下的Gmail(从here复制):

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"); 
     } 
    } 
} 
+0

什么是电子邮件的代码。我的意思是你可以请建议一些代码,如果阻止 –

+0

看看张贴的链接。在那里,你会发现要添加的代码。根据你的需要选择代码,如果你喜欢后台操作 –

+0

哥哥你有没有明白我的观点?我希望填写联系表格的用户数据应发送到特定的邮件ID。那么谁将会是发件人,因为接收者已经修复。 –

相关问题