• Android简单发送邮件(可带附件)


    项目中遇到了一个发送邮件的功能,在网上查了很多,资料也很多。这里就不一一介绍了,只是写出我使用的方案(最简单的)


    Intent email = new Intent(android.content.Intent.ACTION_SEND);
    //邮件发送类型:无附件,纯文本
    email.setType("plain/text");
    //邮件接收者(数组,可以是多位接收者)
    String[] emailReciver = new String[]{"123@qq.com","456@163.com"};
    
    String  emailTitle = "标题";
    String emailContent = "内容";
    //设置邮件地址
     email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
    //设置邮件标题
    email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
    //设置发送的内容
    email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
     //调用系统的邮件系统
    startActivity(Intent.createChooser(email, "请选择邮件发送软件"));



    代码很简单,直接复制,然后修改就可以用(本人亲自试过了)。就像我说的,这是最最简单的方式了。它主要是通过调用系统的mail发送邮件。他的好处就是简单,方便。如果你安装了QQ邮箱、gmail邮箱、163邮箱的android客户端,那么在发送时,会提示你选择使用哪一个。如果你没有安装上述邮件客户端,那么,就调用系统的邮件客户端了。


    下面在写一个可以发送附件的代码,当然也是这种最简单的方式


    Intent email = new Intent(android.content.Intent.ACTION_SEND);
    // 附件  
     File file = new File(Environment.getExternalStorageDirectory().getPath()+ File.separator + "simplenote"+ File.separator+"note.xml");
    //邮件发送类型:带附件的邮件
    email.setType("application/octet-stream");
     //邮件接收者(数组,可以是多位接收者)
    String[] emailReciver = new String[]{"123@qq.com","456@163.com"};
    
    String  emailTitle = "标题";
    String emailContent = "内容";
    //设置邮件地址
    email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
    //设置邮件标题
     email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
    //设置发送的内容
    email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
    //附件
    email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
     //调用系统的邮件系统
    startActivity(Intent.createChooser(email, "请选择邮件发送软件"));



    通过两段代码的比较,就明白了。

    
    
    
  • 相关阅读:
    写在毕业季前
    使用Github Page鼓励自己每日编程
    win8/Metro开发系列一 Xaml布局
    AlertDialog详解
    安卓项目文件目录
    Andriod布局之LinearLayout
    Andriod定时任务
    android 设置布局横屏竖屏
    Android默认启动程序问题
    Android全屏显示
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3225807.html
Copyright © 2020-2023  润新知