Android 发送邮件(简单很快啊)

    /**
     * 发送邮件
     * @param extraText 邮件内容
     */
    private fun sendEmail(extraText: String) {
        val emailIntent = Intent(Intent.ACTION_SEND)/*不带附件发送邮件*/
        emailIntent.type = "plain/text"/*邮件标题*/
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "这是标题")
        emailIntent.putExtra(Intent.EXTRA_EMAIL,arrayOf("目标的邮箱"))
        emailIntent.putExtra(Intent.EXTRA_TEXT, extraText) //发送的内容
        startActivity(Intent.createChooser(emailIntent, "分享"))
    }

代码很简单,直接复制,各个属性都在上边写明,然后修改就可以用。它主要是通过调用系统的mail发送邮件。如果你安装了QQ邮箱、gmail邮箱、163邮箱的android客户端,自己选一个,选中后会自动填充内容,如果你没有安装上述邮件客户端,那么,就调用系统的邮件客户端,部分会提示你绑定邮箱,你可以用自己选。

效果:

 

第二种:

/**
     * 发送邮件
     * @param extraText 邮件内容
     */
    private fun sendEmail3(extraText: String) {
        val intent = Intent(Intent.ACTION_SEND)
        intent.data = Uri.parse("mailto:")
        intent.type = "text/plain"
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享")
        intent.putExtra(Intent.EXTRA_EMAIL,arrayOf("support@jagat.io"))
        intent.putExtra(Intent.EXTRA_TEXT, extraText)
        startActivity(Intent.createChooser(intent, "分享一下"))
    }

这个原理也是一样,只是这个会调起来更多的选择,例如微博,淘宝啊之类的。我们标识的邮件(mailto),但是实际执行却成了分享文件,虽然其中也有邮件客户端,但是却增加了用户的操作步骤。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个发送邮件的基本代码示例,你需要在代码中填写你的邮箱地址和密码,以及收件人、主题和内容等信息: ```java public void sendEmail() { String email = "[email protected]"; String password = "yourPassword"; String recipient = "[email protected]"; String subject = "Test Email"; String message = "This is a test email from Android."; String[] recipients = {recipient}; Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("message/rfc822"); intent.putExtra(Intent.EXTRA_EMAIL, recipients); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, message); try { // Authenticate with your Gmail account Authenticator auth = new Authenticator() { // Override the getPasswordAuthentication method protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(email, password); } }; // Create a new session with Gmail SMTP server Session session = Session.getInstance(getProperties(), auth); // Send email using the created session Transport.send(new Message(session, recipients, subject, message)); Toast.makeText(this, "Email sent successfully", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(this, "Failed to send email", Toast.LENGTH_SHORT).show(); } } private Properties getProperties() { Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); return props; } ``` 注意:在使用此代码之前,请确保已经在你的 Android 项目中添加了邮件发送相关的依赖库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

&岁月不待人&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值