MailSender基于JavaMail for Android开发,旨在帮助开发者在Android平台快速实现邮件发送
- Kotlin开发,兼容Java项目
- 支持发送纯文本、html、SpannableString内容邮件发送
- 支持发送带附件邮件
- 支持抄送,密送
repositories {
jcenter()
}
implementation 'com.teprinciple:mailsender:1.2.0'
// 创建邮箱
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)
// 创建邮箱
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);
只需将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()
属性 | 说明 | 是否必须 |
---|---|---|
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 |
下面是qq邮箱授权码获取 怎样获取授权码?
如果一直报错 Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 可能你的gMail认证被阻止,在google账户中,打开“允许低安全应用”开关.