Android 如何在没有模拟器电子邮件应用程序的情况下发送电子邮件

How to send email without emulator email app in Android

是否可以在 Android 中发送电子邮件,而无需使用 gmail、hotmail 等电子邮件客户端。我可以仅使用 Intent class 直接发送电子邮件吗?

现在我正在使用内置于电子邮件应用程序中的模拟器。

另外,我怎样才能让用户以简单的方式将文件附加到电子邮件中? 我想使用一个按钮在模拟器上打开一个包含图像的文件夹,并让用户选择一个要附加的文件。

这是我的代码:

public class MainActivity extends ActionBarActivity {

private EditText textEmail;
private EditText textSubject;
private EditText textMessage;
private Button   btnSend, btnAttach;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textEmail = (EditText)findViewById(R.id.editText);
    textSubject = (EditText)findViewById(R.id.editText2);
    textMessage = (EditText)findViewById(R.id.editText3);
    btnSend = (Button)findViewById(R.id.button);
    btnAttach =(Button)findViewById(R.id.button2);//not implemented yet

    btnSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SendEmail();
            textEmail.setText("");
            textSubject.setText("");
            textMessage.setText("");
        }
    });
}

protected void SendEmail(){

    String toEmail = textEmail.getText().toString();
    String theSubject = textSubject.getText().toString();
    String theMessage = textMessage.getText().toString();

    Intent email = new Intent(Intent.ACTION_SEND);
    email.setData(Uri.parse("mailto:"));
    email.putExtra(Intent.EXTRA_EMAIL,toEmail);
    email.putExtra(Intent.EXTRA_SUBJECT,theSubject);
    email.putExtra(Intent.EXTRA_TEXT,theMessage);
    email.setType("message/rfc822");

        // the user can choose the email client
        startActivity(Intent.createChooser(email, "Send email"));

}

}

简答 - 没有。 Intent 请求 OS 运行 已为其注册的应用程序。如果您没有电子邮件应用程序,那么根据定义将没有应用程序来处理您的 Intent。

只有意图 class 没有,但是当 gmail 和 hotmail 等其他应用程序专门用于发送电子邮件时,为什么要重新发明轮子呢?这些客户还将处理将文件附加到电子邮件的情况,但如果您希望自己处理并将其与电子邮件意图一起传递,请查看 Google's tutorial on sharing files.

这里有一个关于这个主题的 previous discussion

ehd 的回答依赖于第 3 方 java 库邮件包装器。如果您不信任该包装器,请考虑将标准 Linux 邮件实用程序(即 sendMail)添加到您的项目并创建 JNI 层以与其通信。