在android中发送电子邮件“发送不起作用”

时间:2012-03-31 18:28:02

标签: android email

Sending Email in Android using JavaMail API without using the default/built-in app

我正在尝试根据上面的链接为我的android创建一个电子邮件应用程序,所以我创建了三个类,做了一个简单的布局,但我不能让它工作?当我在模拟器中启动应用程序并且布局出现时我按下发送没有响应。我怀疑问题在于“发送”。有小费吗?

我还在清单中补充说。 uses-permission android:name =“android.permission.INTERNET”

并做了一个简单的布局...每次按下发送按钮它都不发送,只打印出“按钮”!

package gaia.feedback.com;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class GaiaFeedbackActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Button send = (Button) this.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                try {   
                    GmailSender sender = new GmailSender("username@gmail.com", "password");
                    sender.sendMail("This is Subject",   
                            "This is Body",   
                            "user@gmail.com",   
                            "user@yahoo.com");   
                    System.out.println("buttonbutton");

                } catch (Exception e) {   
                    Log.e("SendMail", e.getMessage(), e);   
                    System.out.println("coolcool");
                } 

            }
        });

    }
}

2 个答案:

答案 0 :(得分:1)

如果您只需要发送邮件,可以使用以下行

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:"));
startActivity(Intent.createChooser(intent, "Send via..."));

答案 1 :(得分:1)

If you want to add subject and body then you can use the below method

private void sendMail(String subject,String body){
String mail = "mailto:?to=asd@gmail.com&subject="+subject+"&body="+body;
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(mail));
startActivity(Intent.createChooser(intent, "Send via..."));
}