MailSender

Introduction: Android 快速实现发送邮件
More: Author   ReportBugs   
Tags:

简介

MailSender 基于JavaMail for Android开发,旨在帮助开发者在 Android 平台快速实现邮件发送

MailSender 特点

  • Kotlin 开发,兼容 Java 项目
  • 支持发送纯文本、html、SpannableString 内容邮件发送
  • 支持发送带附件邮件
  • 支持抄送,密送

集成

repositories {
   jcenter()    
}

implementation 'com.teprinciple:mailsender:1.2.0'

使用

kotlin 使用

// 创建邮箱
 val mail = Mail().apply {
    mailServerHost = "smtp.qq.com"
    mailServerPort = "587"
    fromAddress = "xxxxxxxx@foxmail.com"
    password = "xxxxxxxx"
    toAddress = arrayListOf("xxxxxxxx@qq.com")
    subject = "MailSender"
    content = "MailSender Android 快速实现发送邮件"
    attachFiles = arrayListOf(file)
 }

 // 发送邮箱
 MailSender.getInstance().sendMail(mail)

Java 使用

// 创建邮箱
Mail mail = new Mail();
mail.mailServerHost = "smtp.qq.com";
mail.mailServerPort = "587";
mail.fromAddress = "xxxxxxxx@foxmail.com";
mail.password = "xxxxxxxx";
mail.toAddress = arrayListOf("xxxxxxxx@qq.com");
mail.subject = "MailSender";
mail.content = "MailSender Android 快速实现发送邮件";
mail.attachFiles = arrayListOf(file);

 // 发送邮箱
 MailSender.getInstance().sendMail(mail);

发送 Html、SpannableString 格式的邮件

只需将 Mail 类中的 content,换成 html 或者 SpannableString

// html 内容的邮件
content = 
    """
        <p1 style = "color: red">MailSender</p1><br/>
        <p1 style = "color: blue">Android 快速实现发送邮件</p1><br/>
        <p1 style = "color: blue">https://github.com/teprinciple/MailSender</p1><br/>
        <p6 style = "color: gray">这是 html 内容的邮件</p1><br/>
        <img src="https://avatars2.githubusercontent.com/u/19629464?s=460&v=4">
    """

//SpannableString 内容的邮件
content = SpanUtils(this@MainActivity)
    .appendLine("MailSender").setFontSize(28, true).setForegroundColor(Color.RED)
    .appendLine("Android 快速实现发送邮件")
    .appendLine("https://github.com/teprinciple/MailSender").setForegroundColor(Color.BLUE)
    .appendLine("这是 SpannableString 内容的邮件").setForegroundColor(Color.parseColor("#efefef")).setFontSize(12, true)
    .create()

Mail 说明

属性 说明 是否必须
mailServerHost 发件邮箱服务器 true
mailServerPort 发件邮箱服务器端口 true
fromAddress 发件邮箱地址 true
password 发件箱授权码(密码) true
toAddress 直接收件人邮箱 true
ccAddress 抄送者邮箱 false
bccAddress 密送者邮箱 false
subject 邮件主题 false
content 邮件内容 false
attachFiles 附件 false
openSSL ssl 验证开关(是否打开依据邮箱提供商配置) false
sslFactory ssl 实现类 只在 openSSL=true 时生效 fjavax.net.ssl.SSLSocketFactory

Demo 体验

关于授权码的获取

下面是 qq 邮箱授权码获取 怎样获取授权码?

gMail 注意事项

如果一直报错 Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 可能你的 gMail 认证被阻止,在 google 账户中,打开“允许低安全应用”开关.

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools