美文网首页
Android 发送邮件内容及主题丢失问题

Android 发送邮件内容及主题丢失问题

作者: Kingsley_Wu | 来源:发表于2020-01-08 14:38 被阅读0次

使用如下方法在很多手机上出现发送的内容及主题丢失:

public static void sendEmail(Context context, String receiver, String subject, String body, File file) {
        try {
            Intent it = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + receiver)));
            if (!TextUtils.isEmpty(subject)) {
                it.putExtra(Intent.EXTRA_SUBJECT, subject);
            }
            if (!TextUtils.isEmpty(body)) {
                it.putExtra(Intent.EXTRA_TEXT, body);
            }
            File file = new File(file);
            if (file.exists()) {
                it.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            context.startActivity(Intent.createChooser(it, "Send Email"));
        } catch (Exception e) {
            Log.e("sendEmail", e);
        }
    }

所以推荐使用如下发送邮件方法:

 public static void sendEmail(Context context, String receiver, String subject, String body, File file) {
        try {
            Intent it = new Intent(Intent.ACTION_SEND);
            it.putExtra(Intent.EXTRA_EMAIL, new String[]{receiver});
            it.setType("*/*");
            if (!TextUtils.isEmpty(subject)) {
                it.putExtra(Intent.EXTRA_SUBJECT, subject);
            }
            if (!TextUtils.isEmpty(body)) {
                it.putExtra(Intent.EXTRA_TEXT, body);
            }
            if (file != null && file.exists()) {
                it.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
            }
            context.startActivity(createEmailOnlyChooserIntent(context,it, receiver,"Send Email"));
        } catch (Exception e) {
            Log.e( "sendEmail", e);
        }
    }

通过匹配进行筛选:

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+receiver));
getPackageManager().queryIntentActivities(intent, 0);

方法 createEmailOnlyChooserIntent 如下:

public static Intent createEmailOnlyChooserIntent(Context context,Intent source,String receiver,String chooserTitle) {
        Stack<Intent> intents = new Stack<>();
        Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:"+receiver));
        List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(i, 0);
        for(ResolveInfo ri : activities) {
            Intent target = new Intent(source);
            target.setPackage(ri.activityInfo.packageName);
            intents.add(target);
        }
        if(!intents.isEmpty()) {
            Intent chooserIntent = Intent.createChooser(intents.remove(0), chooserTitle);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[0]));
            return chooserIntent;
        } else {
            return Intent.createChooser(source, chooserTitle);
        }
    }

最终效果很满意!!

相关文章

  • Android 发送邮件内容及主题丢失问题

    使用如下方法在很多手机上出现发送的内容及主题丢失: 所以推荐使用如下发送邮件方法: 通过匹配进行筛选: 方法 cr...

  • 9.邮件模板及发送

    邮件模板及发送

  • 2018-10-11

    文本邮件的发送 1.邮件发送流程 ​ 邮件的发送是主动行为:主要通过 MUA/邮件客户端软件,将邮件内容发送给对应...

  • Python之SMTP协议

    发邮件一般包含如下内容,发件人、收件人、邮件主题、邮件正文、附件,在解决如何用python发送邮件之前,需要了解如...

  • mail

    1. 邮件正文输入的三种方式 方式一 发送邮件给test@test.com,会先后要求输入邮件主题和内容。方式二 ...

  • android 发送邮件到QQEmail,邮件内容+附件形式

    之前写过一篇发送邮件到邮箱的,但是只支持文本发送,这篇文章将支持附件+文本内容 发送

  • Android邮件发送

    Android端发送邮件,比较简单,可以用于APP注册时候发送验证码什么的,需要一个用于发送邮件的邮箱,并且要将邮...

  • Android 发送邮件

    依赖 使用 登录认证类

  • Android发送邮件

    第一步:获取邮箱授权码 这里以QQ邮箱为例子 第二步: 集成jar 第三步:直接上代码

  • 用Python控制摄像头拍照并发邮件

    00 概述 前言 工具 思路 安装及导入包 设置参数 实现拍照 构造邮件内容 发送邮件 判断网络连接 开机自启 后...

网友评论

      本文标题:Android 发送邮件内容及主题丢失问题

      本文链接:https://www.haomeiwen.com/subject/laqsactx.html