A small example of Android calling the system's email function

  • 2020-05-17 06:25:35
  • OfStack


package com.example.myapi.email;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class SendEmailUtils {
    private SendEmailUtils(){}
    private static SendEmailUtils instance;
    public static SendEmailUtils getInstance(){
        synchronized (SendEmailUtils.class) {
            if(instance == null){
                instance = new SendEmailUtils();
            }
        }
        return instance;
    }
    /**
     *  This is a 1 A simple test, do not support with attachments, multiple people, cc send and so on. 
     * @param context
     */
    public void sendEmail(Context context){
        Intent intent = new Intent();
        intent.setData(Uri.parse("mailto:"));
        /* Set the subject line of the email */
        intent.putExtra(Intent.EXTRA_SUBJECT, " Take it easy. It's just 1 A test! ");
        /* Set the content of the message */
        intent.putExtra(Intent.EXTRA_TEXT, " The test opens the system mailbox and automatically fills the mailbox with the sent title and content, and sends the mail. ");
        // Began to call 
        context.startActivity(intent); 
    }
    /**
     *  Send email with cc, BCC, BCC, and an attachment 
     * @param context
     */
    public void sendEmailDuo(Context context){
        Intent intent = new Intent(Intent.ACTION_SEND); 
//        intent.setData(Uri.parse("mailto:"));
        String[] tos = { "yw.1@163.com" }; 
        String[] ccs = { "yw.2@163.com" }; 
        String[] bccs = {"yw.3@163.com"}; 
        intent.putExtra(Intent.EXTRA_EMAIL, tos); // recipient 
        intent.putExtra(Intent.EXTRA_CC, ccs); // Cc this 
        intent.putExtra(Intent.EXTRA_BCC, bccs); // Close to send this 
        intent.putExtra(Intent.EXTRA_TEXT, " Email content "); 
        intent.putExtra(Intent.EXTRA_SUBJECT, " Email title "); 
        intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/a.jpg")); 
        intent.setType("image/*"); 
        intent.setType("message/rfc882"); 
        Intent.createChooser(intent, "Choose Email Client"); 
        context.startActivity(intent); 

    }
    /**
     *  Multiple attachment sending 
     * @param conext
     */
    public void sendFujian(Context conext){
        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
        String[] tos = { "way.ping.li@gmail.com" }; 
        String[] ccs = { "way.ping.li@gmail.com" }; 
        intent.putExtra(Intent.EXTRA_EMAIL, tos); 
        intent.putExtra(Intent.EXTRA_CC, ccs); 
        intent.putExtra(Intent.EXTRA_TEXT, "body"); 
        intent.putExtra(Intent.EXTRA_SUBJECT, "subject"); 
        List<Uri> imageUris = new ArrayList<Uri>(); 
        imageUris.add(Uri.parse("file:///mnt/sdcard/a.jpg")); 
        imageUris.add(Uri.parse("file:///mnt/sdcard/b.jpg")); 
        intent.putExtra(Intent.EXTRA_STREAM, imageUris);
        intent.setType("image/*"); 
        intent.setType("message/rfc882"); 
        Intent.createChooser(intent, "Choose Email Client"); 
        conext.startActivity(intent); 
    }

}


Related articles: