📜  如何从您的 Android 应用程序发送电子邮件?

📅  最后修改于: 2022-05-13 01:55:23.623000             🧑  作者: Mango

如何从您的 Android 应用程序发送电子邮件?

在本文中,您将制作一个基本的 android 应用程序,可用于通过您的 android 应用程序发送电子邮件。

您可以借助带有额外字段的ACTION_SEND操作的Intent来执行此操作:

  • 您要向其发送邮件的电子邮件 ID
  • 电子邮件的主题
  • 电子邮件的正文

基本上,Intent 是一个简单的消息对象,用于在 android 组件之间进行通信,例如活动、内容提供者、广播接收器和服务,这里用于发送电子邮件。

该应用程序基本上包含一个带有 EditText 的活动,用于输入来自用户的电子邮件地址、主题和正文以及发送该电子邮件的按钮。

  • 步骤 1.activity_main.xml
    activity_main.xml 包含一个相对布局,其中包含三个用于接收邮件 ID 的编辑文本,另一个用于邮件主题,最后一个用于电子邮件正文,三个 TextViews 用于标签和一个用于启动意图或发送邮件的按钮:
    activity_main.xml
    
      
    
    
      
        
          
      
        
            
      
        
          
      
        
          
      
          
      
          
      
        
            
    


    MainActivity.java
    package com.geeksforgeeks.phonecall;
      
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;
    import android.widget.EditText;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.net.Uri;
    import android.widget.Button;
      
    public class MainActivity extends AppCompatActivity {
      
        // define objects for edit text and button
        Button button;
        EditText sendto, subject, body;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // Getting instance of edittext and button
            sendto = findViewById(R.id.editText1);
            subject = findViewById(R.id.editText2);
            body = findViewById(R.id.editText3);
            button = findViewById(R.id.button);
      
            // attach setOnClickListener to button
            // with Intent object define in it
            button.setOnClickListener(new OnClickListener() {
      
                @Override
                public void onClick(View view)
                {
                    String emailsend = sendto.getText().toString();
                    String emailsubject = subject.getText().toString();
                    String emailbody = body.getText().toString();
      
                    // define Intent object
                    // with action attribute as ACTION_SEND
                    Intent intent = new Intent(Intent.ACTION_SEND);
      
                    // add three fiels to intent using putExtra function
                    intent.putExtra(Intent.EXTRA_EMAIL,
                                    new String[] { emailsend });
                    intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
                    intent.putExtra(Intent.EXTRA_TEXT, emailbody);
      
                    // set type of intent
                    intent.setType("message/rfc822");
      
                    // startActivity with intent with chooser
                    // as Email client using createChooser function
                    startActivity(
                        Intent
                            .createChooser(intent,
                                           "Choose an Email client :"));
                }
            });
        }
    }


  • 步骤 2. MainActivity。Java
    在 Main 活动中创建 Intent 对象并将其操作定义为 ACTION_SEND 以发送电子邮件,Intent 还使用 putExtra函数添加了三个额外字段。这些字段是:
    • 收件人的电子邮件
    • 电子邮件的主题
    • 电子邮件正文

    setOnClickListener附加到带有意图对象的按钮,以使意图具有动作为 ACTION_SEND 以发送电子邮件和意图类型,如代码所示。

    以下是通过意图从 android 应用程序发送电子邮件的完整Java代码:

    主要活动。Java

    package com.geeksforgeeks.phonecall;
      
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.content.Intent;
    import android.widget.EditText;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.net.Uri;
    import android.widget.Button;
      
    public class MainActivity extends AppCompatActivity {
      
        // define objects for edit text and button
        Button button;
        EditText sendto, subject, body;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            // Getting instance of edittext and button
            sendto = findViewById(R.id.editText1);
            subject = findViewById(R.id.editText2);
            body = findViewById(R.id.editText3);
            button = findViewById(R.id.button);
      
            // attach setOnClickListener to button
            // with Intent object define in it
            button.setOnClickListener(new OnClickListener() {
      
                @Override
                public void onClick(View view)
                {
                    String emailsend = sendto.getText().toString();
                    String emailsubject = subject.getText().toString();
                    String emailbody = body.getText().toString();
      
                    // define Intent object
                    // with action attribute as ACTION_SEND
                    Intent intent = new Intent(Intent.ACTION_SEND);
      
                    // add three fiels to intent using putExtra function
                    intent.putExtra(Intent.EXTRA_EMAIL,
                                    new String[] { emailsend });
                    intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
                    intent.putExtra(Intent.EXTRA_TEXT, emailbody);
      
                    // set type of intent
                    intent.setType("message/rfc822");
      
                    // startActivity with intent with chooser
                    // as Email client using createChooser function
                    startActivity(
                        Intent
                            .createChooser(intent,
                                           "Choose an Email client :"));
                }
            });
        }
    }
    

输出: