2012-11-29 73 views
0

我正在開發基於表單的應用程序。該表格包含客戶詳細信息。當他點擊提交按鈕時,表單的細節被包含在電子郵件正文中。如何發送郵件正文中包含表單詳細信息?

下面是我的代碼:

Orders.java

import android.app.Activity;  
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class Order extends Activity{ 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.order); 
     final EditText name = (EditText)findViewById(R.id.username); 
     final EditText mail = (EditText)findViewById(R.id.email); 
     final EditText phone = (EditText)findViewById(R.id.phone); 
     final EditText product = (EditText)findViewById(R.id.product); 
     final String _name = name.getText().toString(); 
     final String _mail = mail.getText().toString(); 
     final String _phone = phone.getText().toString(); 
     final String _product = product.getText().toString(); 
     System.out.println(_name); 
     System.out.println(_mail); 
     System.out.println(_phone); 
     System.out.println(_product); 
     Button email = (Button) findViewById(R.id.Button01); 
     email.setOnClickListener(new OnClickListener(){ 
        public void onClick(View v){ 
        String[] recipients = new String[]{"[email protected]", "",}; 
         StringBuilder body = new StringBuilder(); 
         StringBuilder body1 = new StringBuilder(); 
         body1.append("To: " + recipients); 
         body.append("Name: "+name.getText().toString()); 
         body.append("\n\n\nMail: "+mail.getText().toString()); 
         body.append("\n\n\nPhone: "+phone.getText().toString()); 
         body.append("\n\n\nProduct: "+product.getText().toString()); 
         Intent i = new Intent(android.content.Intent.ACTION_SEND); 
         i.setType("text/plain"); 
         i.putExtra(android.content.Intent.EXTRA_EMAIL, body1.toString()); 
         i.putExtra(android.content.Intent.EXTRA_SUBJECT, "Customer Details"); 
         i.putExtra(android.content.Intent.EXTRA_TEXT, body.toString()); 
startActivity(i); 
} 
}); 
} 
} 

xml文件是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" >  

    <EditText 
     android:layout_width="60dip" 
     android:layout_height="wrap_content" 
     android:id="@+id/edit"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/btn" 
     android:text="Click"/>  
</LinearLayout> 

我得到它在程序給出的郵件ID 「從」字段,但我希望那個在「:」字段中...

+0

那麼你的問題是什麼? – Elemental

+0

deatails應該郵寄給管理員... – Abhay

+0

你有什麼問題?發送電子郵件或獲取身體細節? – ThePCWizard

回答

1
StringBuilder body = new StringBuilder(); 
     body.append("Name: "+nameField.getText().toString()); 
     body.append("\nAge: "+ageField.getText().toString()); 
     body.append("\nGender: "+genderField.getText().toString()); 

     // Sending to admin 
     Intent i = new Intent(Intent.ACTION_SEND); 
     i.setType("text/plain"); 
     i.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "Customer Details"); 
     i.putExtra(Intent.EXTRA_TEXT, body.toString()); 
     startActivity(i); 

希望這有助於!

1

您必須使用StringBuilder將管理你的字符串。 mail應該是html格式,所以你必須使用html支持的類的方法,如Html.fromHtml(sb.toString());

我只是顯示一些文字,將追加到字符串和發送到郵件正文區域,您可以在那裏發送表單。

public StringBuilder sb; 
sb= new StringBuilder(); 

    // This is Mail Format That will be show in Body area and send to be customer. 

    sb.append("<p><b><font color=\"#8CC248\">Title</b></p>"); 
    sb.append("<p><b><font color=\"#000000\">Dear,"+ edittextvalue in string mode +",</b></p>"); 

現在這個發送到郵件onClick事件

Intent i2 = new Intent(android.content.Intent.ACTION_SEND);  
i2.setType("text/html"); 
i2.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml(sb.toString())); 

請參閱您的輸出。

相關問題