2013-10-03 72 views
0

我正在开发的应用程序中有一个模块发送电子邮件。我已经尝试过很多教程,但电子邮件没有发送,并且没有任何异常或错误。我在这里分享我用来发送电子邮件的所有代码。电子邮件不从Android应用程序发送

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 

      emailIntent.setType("plain/text"); 

      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ email.getText().toString()}); 

      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, name.getText()); 

      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, smessage.getText()); 

      Email.this.startActivity(Intent.createChooser(emailIntent, "Send mail...")); 

这个人是不是工作然后我试图

Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
         "mailto","[email protected]", null)); 
      emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT"); 
      emailIntent.setTypeAndNormalize("plain/text"); 
startActivity(Intent.createChooser(emailIntent, "Send email...")); 

它还didnt工作。然后我试图

  Intent feedback = new Intent(Intent.ACTION_SEND); 
      feedback.putExtra(Intent.EXTRA_EMAIL, new String[]{ to}); 
       //email.putExtra(Intent.EXTRA_CC, new String[]{ to}); 
       //email.putExtra(Intent.EXTRA_BCC, new String[]{to}); 
      feedback.putExtra(Intent.EXTRA_SUBJECT, subject); 
      feedback.putExtra(Intent.EXTRA_TEXT, message); 

       //need this to prompts email client only 
      feedback.setType("text/plain"); 

       startActivity(Intent.createChooser(feedback, "Choose an Email client :")); 

我已经尝试了所有这三种代码,但即使是单一的代码片段的工作,我对谷歌nexus测试,但没有奏效

这里是我的xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@drawable/bg"> 

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/header_bg" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0.28" 
     android:background="@drawable/back" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="38dp" 
     android:layout_weight="0.28" 
     android:paddingLeft="50dp" 
     android:paddingTop="15dp" 
     android:text="@string/ReceiptOrganizer" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout> 



<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/receipt_added_bg"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="20dp" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Name" /> 

     <EditText 
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textPersonName" > 

      <requestFocus /> 
     </EditText> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="20dp" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Email" /> 

     <EditText 
      android:id="@+id/email" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textEmailAddress" > 

      <requestFocus /> 
     </EditText> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="20dp" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Feedback" /> 

     <EditText 
      android:id="@+id/msg" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textMultiLine" /> 


    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="20dp" > 

     <Button 
      android:id="@+id/send" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left"/> 

    </LinearLayout> 

</LinearLayout> 

+0

有你定义权限在AndroidManifest.xml中? –

+0

@ geekdev786:请将类型设置为setType(“application/octet-stream”)而不是纯文本/文本,并且我还要求您确认您的清单文件中是否有Internet权限 –

+0

@dynamo我已定义 – geekdev786

回答

0

请将类型的setType而不是纯/文(“应用程序/八位字节流”),也请你确认是否在您的清单文件有网络权限。

尝试下面的代码段,其具有的setType广告应用/ octext流正如我上面

提到//创建用于类意图对象

Intent sendIntent; 

//由于我们要发送,我们正在使用ACTION_SEND

sendIntent = new Intent(Intent.ACTION_SEND); 
sendIntent.setType("application/octet-stream"); 
sendIntent.putExtra(Intent.EXTRA_EMAIL,new String[] {"[email protected]"}); 
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Test"); 
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hy Testing"); 
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file.getAbsolutePath())); 

startActivity(Intent.createChooser(sendIntent, "Send Mail")); 

,并添加以下权限

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+0

我认为这是问题10-03 03:32:50.689:D/InputEventConsistencyVerifier(842):在android.widget.LinearLayout {41900d98 V.E ..... ...... I。 0,60-240,120} – geekdev786

0

尝试

Intent send = new Intent(Intent.ACTION_SENDTO); 
     String uriText = "mailto:" + Uri.encode("[email protected]") + 
        "?subject=" + Uri.encode("the subject") + 
        "&body=" + Uri.encode("the body of the message"); 
     Uri uri = Uri.parse(uriText); 

     send.setData(uri); 
     startActivity(Intent.createChooser(send, "Send mail...")); 

在Internet访问清单中添加权限。

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
+0

我已经尝试过你的一个也它也不工作 – geekdev786

+0

我认为这是issuw 10-03 03:32:50.689:D/InputEventConsistencyVerifier(842):在android.widget.LinearLayout {41900d98 VE。 ... ......一世。 0,60-240,120} – geekdev786

0
String to = toEmail.getText().toString(); 
String subject = emailSubject.getText().toString(); 
String message = emailBody.getText().toString(); 
Intent email = new Intent(Intent.ACTION_SEND); 
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to }); 
email.putExtra(Intent.EXTRA_SUBJECT, subject); 
email.putExtra(Intent.EXTRA_TEXT, message); 
// need this to prompts email client only 
email.setType("message/rfc822"); 
startActivity(Intent.createChooser(email, "Choose an Email client")); 
+0

它没有工作 – geekdev786

+0

@ geekdev786你得到的错误或异常是什么 –

+0

没有错误没有异常 – geekdev786

相关问题