重写SMTP邮件发送

This commit is contained in:
pppscn 2021-02-19 14:27:43 +08:00
parent 9360bbb5e2
commit 6d89e25d38
18 changed files with 104 additions and 760 deletions

View File

@ -35,18 +35,16 @@ Android手机监听短信并根据指定规则转发到其他手机、钉钉机
### 功能列表:
| 功能 | 描述 |
| ---- | ---- |
| 转发监听 | 已实现 |
| 转发钉钉 | 单个钉钉群已实现 |
| 转发钉钉@某人 | 已实现 |
| 转发邮箱 | 单个邮箱已实现 |
| 转发Bark | 已实现,验证码/动态密码自动复制 |
| 转发企业微信群机器人 | 已实现 |
| 转发web页面 | 单个web页面已实现[向设置的url发送POST请求](doc/POST_WEB.md) |
| 转发规则 | (规则即:什么短信转发到哪里)已实现 |
| 兼容 | 已兼容6.xx、7.xx、8.xx、9.xx、10.xx |
- [x] 监听短信,按规则转发(规则:什么短信内容/来源转发到哪里)
- [x] 转发到钉钉机器人(支持:单个钉钉群,@某人)
- [x] 转发到邮箱支持SMTP
- [x] 转发到Bark支持验证码/动态密码自动复制)
- [x] 转发到webhook支持单个web页面[向设置的url发送POST请求](doc/POST_WEB.md)
- [x] 转发到企业微信群机器人(待验证)
- [x] 在线检测新版本、升级
- [x] 清理缓存
- [x] 兼容6.xx、7.xx、8.xx、9.xx、10.xx
### 使用流程:
1. 在Android手机上安装SmsForwarder 本APP后点击应用图标打开
@ -80,6 +78,8 @@ Android手机监听短信并根据指定规则转发到其他手机、钉钉机
## 更新记录:
> [v1.2.0](app/release/SmsForwarder_release_20210219_1.2.0.apk) 重写SMTP邮件发送推荐升级
> [v1.1.3](app/release/SmsForwarder_release_20210218_1.1.3.apk) AlertDialog增加滚动条避免参数过长时无法点击按钮
> [v1.1.2](app/release/SmsForwarder_release_20210218_1.1.2.apk) 获取系统(ROM)类别及版本号MIUI通知栏显示标题

View File

@ -6,12 +6,16 @@ apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
defaultConfig {
applicationId "com.idormy.sms.forwarder"
minSdkVersion 23
targetSdkVersion 28
versionCode 5
versionName "1.1.3"
versionCode 6
versionName "1.2.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
buildTypes {
@ -52,11 +56,6 @@ dependencies {
//fastjson
implementation "com.alibaba:fastjson:1.2.75"
//mail
implementation 'com.sun.mail:android-mail:1.6.0'
implementation 'com.sun.mail:android-activation:1.6.0'
implementation 'com.google.android.material:material:1.0.0'
//android8.0使api和动态代理隐藏的抽象类回调
//使ProxyBuilder类
//implementation 'com.linkedin.dexmaker:dexmaker-mockito:2.12.1'
@ -70,4 +69,7 @@ dependencies {
implementation 'com.github.xuexiangjys.XUpdateAPI:xupdate-easy:1.0.0'
implementation 'com.github.xuexiangjys.XUpdateAPI:xupdate-downloader-aria:1.0.0'
//EmailKit
implementation 'com.github.mailhu:emailkit:4.2.2'
}

Binary file not shown.

View File

@ -10,9 +10,9 @@
{
"type": "SINGLE",
"filters": [],
"versionCode": 5,
"versionName": "1.1.3",
"outputFile": "SmsForwarder_release_20210218_1.1.3.apk"
"versionCode": 6,
"versionName": "1.2.0",
"outputFile": "SmsForwarder_release_20210219_1.2.0.apk"
}
]
}

View File

@ -11,6 +11,7 @@ import android.util.Log;
import com.idormy.sms.forwarder.utils.SendHistory;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.smailnet.emailkit.EmailKit;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
@ -78,5 +79,7 @@ public class MyApplication extends Application {
}
SendHistory.init(this);
SettingUtil.init(this);
EmailKit.initialize(this);
}
}

View File

@ -345,7 +345,6 @@ public class SenderActivity extends AppCompatActivity {
adapter.update(senderModels);
}
show.dismiss();

View File

@ -13,12 +13,6 @@ import java.util.Set;
public class BuildProperties {
private static BuildProperties ourInstance;
public static BuildProperties getInstance() throws IOException {
if (ourInstance == null) ourInstance = new BuildProperties();
return ourInstance;
}
private final Properties properties;
private BuildProperties() throws IOException {
@ -26,6 +20,11 @@ public class BuildProperties {
properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
}
public static BuildProperties getInstance() throws IOException {
if (ourInstance == null) ourInstance = new BuildProperties();
return ourInstance;
}
public boolean containsKey(final Object key) {
return properties.containsKey(key);
}

View File

@ -1,139 +0,0 @@
package com.idormy.sms.forwarder.utils;
import java.util.Properties;
public class MailInfo {
private String mailServerHost;// 发送邮件的服务器的IP
private String mailServerPort;// 发送邮件的服务器的端口
private String fromAddress;// 邮件发送者的地址
private String toAddress; // 邮件接收者的地址
private String userName;// 登陆邮件发送服务器的用户名
private String password;// 登陆邮件发送服务器的密码
private boolean validate = true;// 是否需要身份验证
private boolean ssl = true;// ssl
private String subject;// 邮件主题
private String content;// 邮件的文本内容
private String[] attachFileNames;// 邮件附件的文件名
public String toString() {
return "mailServerHost:" + this.mailServerHost
+ "mailServerPort:" + this.mailServerPort
+ "fromAddress:" + this.fromAddress
+ "toAddress:" + this.toAddress
+ "userName:" + this.userName
+ "password:" + this.password
+ "subject:" + this.subject
+ "content:" + this.content;
}
/**
* 获得邮件会话属性
*/
public Properties getProperties() {
Properties p = new Properties();
p.put("mail.smtp.host", this.mailServerHost);
p.put("mail.smtp.port", this.mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
// 设置SSL加密(未采用SSL时端口一般为25可以不用设置采用SSL时端口为465需要显示设置)
if (ssl) {
p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
p.put("mail.smtp.socketFactory.fallback", "false");
p.put("mail.smtp.socketFactory.port", this.mailServerPort);
}
// props.setProperty("mail.smtp.port", "465");
// props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.setProperty("mail.smtp.socketFactory.fallback", "false");
// props.setProperty("mail.smtp.socketFactory.port", "465");
return p;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public void ssl(boolean ssl) {
this.ssl = ssl;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
}

View File

@ -1,205 +0,0 @@
package com.idormy.sms.forwarder.utils;
import android.util.Log;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* 发送器
*/
public class MailSender {
private static String TAG = "MailSender";
/**
* 以HTML格式发送邮件
*
* @param mailInfo 待发送的邮件信息
*/
public static boolean sendHtmlMail(MailInfo mailInfo) {
Log.d(TAG, "sendHtmlMail MailInfo " + mailInfo.toString());
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
// 如果需要身份认证则创建一个密码验证器
if (mailInfo.isValidate()) {
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
// Message.RecipientType.TO属性表示接收者的类型为TO
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// MiniMultipart类是一个容器类包含MimeBodyPart类型的对象
Multipart mainPart = new MimeMultipart();
// 创建一个包含HTML内容的MimeBodyPart
BodyPart html = new MimeBodyPart();
// 设置HTML内容
html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
mainPart.addBodyPart(html);
// 将MiniMultipart对象设置为邮件内容
mailMessage.setContent(mainPart);
// 发送邮件
Transport.send(mailMessage);
return true;
} catch (MessagingException ex) {
ex.printStackTrace();
}
return false;
}
/**
* 以文本格式发送邮件
*
* @param mailInfo 待发送的邮件的信息
*/
public boolean sendTextMail(final MailInfo mailInfo) {
Log.d(TAG, "sendTextMail MailInfo " + mailInfo.toString());
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// 根据session创建一个邮件消息
Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
SendHistory.addHistory("Email mailInfo" + mailInfo.toString());
return true;
} catch (MessagingException ex) {
SendHistory.addHistory("Email Fail mailInfo" + mailInfo.toString());
Log.d(TAG, "sendTextMail MailInfo " + ex.getMessage());
ex.printStackTrace();
}
return false;
}
/**
* 发送带附件的邮件
*
* @param info
* @return
*/
public boolean sendFileMail(MailInfo info, File file) {
Log.d(TAG, "sendFileMail MailInfo " + info.toString());
Message attachmentMail = createAttachmentMail(info, file);
try {
Transport.send(attachmentMail);
return true;
} catch (MessagingException e) {
e.printStackTrace();
return false;
}
}
/**
* 创建带有附件的邮件
*
* @return
*/
private Message createAttachmentMail(final MailInfo info, File file) {
Log.d(TAG, "createAttachmentMail MailInfo " + info.toString());
//创建邮件
MimeMessage message = null;
Properties pro = info.getProperties();
try {
Session sendMailSession = Session.getInstance(pro, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(info.getUserName(), info.getPassword());
}
});
message = new MimeMessage(sendMailSession);
// 设置邮件的基本信息
//创建邮件发送者地址
Address from = new InternetAddress(info.getFromAddress());
//设置邮件消息的发送者
message.setFrom(from);
//创建邮件的接受者地址并设置到邮件消息中
Address to = new InternetAddress(info.getToAddress());
//设置邮件消息的接受者, Message.RecipientType.TO属性表示接收者的类型为TO
message.setRecipient(Message.RecipientType.TO, to);
//邮件标题
message.setSubject(info.getSubject());
// 创建邮件正文为了避免邮件正文中文乱码问题需要使用CharSet=UTF-8指明字符编码
MimeBodyPart text = new MimeBodyPart();
text.setContent(info.getContent(), "text/html;charset=UTF-8");
// 创建容器描述数据关系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
// 创建邮件附件
MimeBodyPart attach = new MimeBodyPart();
FileDataSource ds = new FileDataSource(file);
DataHandler dh = new DataHandler(ds);
attach.setDataHandler(dh);
attach.setFileName(MimeUtility.encodeText(dh.getName()));
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
} catch (Exception e) {
Log.e("TAG", "创建带附件的邮件失败");
e.printStackTrace();
}
// 返回生成的邮件
return message;
}
}

View File

@ -1,147 +0,0 @@
package com.idormy.sms.forwarder.utils;
import android.util.Log;
import com.sun.mail.util.MailSSLSocketFactory;
import java.security.GeneralSecurityException;
import java.util.Properties;
public class MailSenderInfo {
private static String TAG = "MailSenderInfo";
// 发送邮件的服务器的IP和端口
private String mailServerHost;
private String mailServerPort = "25";
// 邮件发送者的地址
private String fromAddress;
// 邮件接收者的地址
private String toAddress;
// 登陆邮件发送服务器的用户名和密码
private String userName;
private String password;
// 是否需要身份验证
private boolean validate = true;
//开启ssl
private boolean ssl = true;
// 邮件主题
private String subject;
// 邮件的文本内容
private String content;
// 邮件附件的文件名
private String[] attachFileNames;
/**
* 获得邮件会话属性
*/
public Properties getProperties() {
Properties props = new Properties();
props.put("mail.smtp.host", this.mailServerHost);
props.put("mail.smtp.port", this.mailServerPort);
props.put("mail.smtp.auth", validate ? "true" : "false");
if (ssl) {
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Log.i(TAG, "set ssl success");
} catch (GeneralSecurityException e) {
Log.e(TAG, "set ssl fail: " + e.getMessage());
e.printStackTrace();
}
}
return props;
}
public String getMailServerHost() {
return mailServerHost;
}
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
public String getMailServerPort() {
return mailServerPort;
}
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
public boolean isValidate() {
return validate;
}
public void setValidate(boolean validate) {
this.validate = validate;
}
public String[] getAttachFileNames() {
return attachFileNames;
}
public void setAttachFileNames(String[] fileNames) {
this.attachFileNames = fileNames;
}
public String getFromAddress() {
return fromAddress;
}
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getToAddress() {
return toAddress;
}
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String textContent) {
this.content = textContent;
}
public boolean isSsl() {
return ssl;
}
public void setSsl(boolean ssl) {
this.ssl = ssl;
}
}

View File

@ -1,21 +0,0 @@
package com.idormy.sms.forwarder.utils;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class MyAuthenticator extends Authenticator {
String userName = null;
String password = null;
public MyAuthenticator() {
}
public MyAuthenticator(String username, String password) {
this.userName = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
}

View File

@ -15,13 +15,6 @@ import java.io.IOException;
* 链接https://www.jianshu.com/p/bb1f765a425f
*/
public class OSUtils {
public enum ROM_TYPE {
MIUI_ROM,
FLYME_ROM,
EMUI_ROM,
OTHER_ROM
}
/**
* MIUI ROM标识
* <p>
@ -57,10 +50,8 @@ public class OSUtils {
//MIUI标识
private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
//EMUI标识
private static final String KEY_EMUI_VERSION_CODE = "ro.build.version.emui";
//Flyme标识
private static final String KEY_FLYME_ID_FALG_KEY = "ro.build.display.id";
private static final String KEY_FLYME_ID_FALG_VALUE_KEYWORD = "Flyme";
@ -99,4 +90,11 @@ public class OSUtils {
}
return rom_type;
}
public enum ROM_TYPE {
MIUI_ROM,
FLYME_ROM,
EMUI_ROM,
OTHER_ROM
}
}

View File

@ -1,85 +0,0 @@
package com.idormy.sms.forwarder.utils;
import android.util.Log;
import java.io.File;
public class SendMailUtil {
private static String TAG = "SendMailUtil";
//qq
// private static final String HOST = "smtp.qq.com";
// private static final String PORT = "587";
// private static final String FROM_ADD = "teprinciple@foxmail.com"; //发送方邮箱
// private static final String FROM_PSW = "lfrlpganzjrwbeci";//发送方邮箱授权码
// //163
// private static final String HOST = "smtp.163.com";
// private static final String PORT = "465"; //nossl 25或者ssl465 994
// private static final String FROM_ADD = "xxxxxx@163.com";
// private static final String FROM_PSW = "xx";
public static void send(final File file, String toAdd, String title, String content) {
Log.d(TAG, "send file to " + toAdd);
final MailInfo mailInfo = creatMail(toAdd, title, content);
final MailSender sms = new MailSender();
new Thread(new Runnable() {
@Override
public void run() {
sms.sendFileMail(mailInfo, file);
}
}).start();
}
public static void send(String toAdd, String title, String content) {
Log.d(TAG, "send to " + toAdd);
final MailInfo mailInfo = creatMail(toAdd, title, content);
final MailSender sms = new MailSender();
new Thread(new Runnable() {
@Override
public void run() {
sms.sendTextMail(mailInfo);
}
}).start();
}
private static MailInfo creatMail(String toAdd, String title, String content) {
Log.d(TAG, "creatMail to " + toAdd);
final MailInfo mailInfo = new MailInfo();
mailInfo.setMailServerHost(SettingUtil.get_send_util_email(Define.SP_MSG_SEND_UTIL_EMAIL_HOST_KEY));
mailInfo.setMailServerPort(SettingUtil.get_send_util_email(Define.SP_MSG_SEND_UTIL_EMAIL_PORT_KEY));
mailInfo.setValidate(true);
mailInfo.ssl(true);
mailInfo.setUserName(SettingUtil.get_send_util_email(Define.SP_MSG_SEND_UTIL_EMAIL_FROMADD_KEY)); // 你的邮箱地址
mailInfo.setPassword(SettingUtil.get_send_util_email(Define.SP_MSG_SEND_UTIL_EMAIL_PSW_KEY));// 您的邮箱密码
mailInfo.setFromAddress(SettingUtil.get_send_util_email(Define.SP_MSG_SEND_UTIL_EMAIL_FROMADD_KEY)); // 发送的邮箱
mailInfo.setToAddress(SettingUtil.get_send_util_email(Define.SP_MSG_SEND_UTIL_EMAIL_TOADD_KEY)); // 发到哪个邮件去
mailInfo.setSubject(title); // 邮件主题
mailInfo.setContent(content); // 邮件文本
return mailInfo;
}
}
/**
* public void sendFileMail(View view) {
* <p>
* File file = new File(Environment.getExternalStorageDirectory()+File.separator+"test.txt");
* OutputStream os = null;
* try {
* os = new FileOutputStream(file);
* String str = "hello world";
* byte[] data = str.getBytes();
* os.write(data);
* } catch (FileNotFoundException e) {
* e.printStackTrace();
* } catch (IOException e) {
* e.printStackTrace();
* }finally{
* try {
* if (os != null)os.close();
* } catch (IOException e) {
* }
* }
* SendMailUtil.send(file,editText.getText().toString());
* }
*/

View File

@ -59,6 +59,8 @@ public class SendUtil {
Log.i(TAG, "send_msg smsVo:" + smsVo);
RuleUtil.init(context);
LogUtil.init(context);
//初始化 EmailKit
//EmailKit.initialize(context);
List<RuleModel> rulelist = RuleUtil.getRule(null, null);
if (!rulelist.isEmpty()) {
@ -144,6 +146,8 @@ public class SendUtil {
}
}
//EmailKit.destroy();
}
public static void senderSendMsg(SmsVo smsVo, SenderModel senderModel) {

View File

@ -4,97 +4,42 @@ import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.smailnet.emailkit.Draft;
import com.smailnet.emailkit.EmailKit;
import static com.idormy.sms.forwarder.SenderActivity.NOTIFY;
public class SenderMailMsg {
private static String TAG = "SenderMailMsg";
//qq
// private static final String HOST = "smtp.qq.com";
// private static final String PORT = "587";
// private static final String FROM_ADD = "teprinciple@foxmail.com"; //发送方邮箱
// private static final String FROM_PSW = "lfrlpganzjrwbeci";//发送方邮箱授权码
// //163
// private static final String HOST = "smtp.163.com";
// private static final String PORT = "465"; //nossl 25或者ssl465 994
// private static final String FROM_ADD = "xxxxxx@163.com";
// private static final String FROM_PSW = "xx";
public static void sendEmail(final Handler handError, final String host, final String port, final boolean ssl, final String fromemail, final String pwd, final String toAdd, final String title, final String content) {
Log.d(TAG, "sendEmail: host:" + host + " port:" + port + " ssl:" + ssl + " fromemail:" + fromemail + " pwd:" + pwd + " toAdd:" + toAdd);
new Thread(new Runnable() {
try {
//初始化框架
//EmailKit.initialize(this);
//配置发件人邮件服务器参数
EmailKit.Config config = new EmailKit.Config()
.setSMTP(host, Integer.parseInt(port), ssl) //设置SMTP服务器主机地址端口和是否开启ssl
.setAccount(fromemail) //发件人邮箱
.setPassword(pwd); //密码或授权码
//设置一封草稿邮件
Draft draft = new Draft()
.setNickname("SmsForwarder") //发件人昵称
.setTo(toAdd) //收件人邮箱
.setSubject(title) //邮件主题
.setText(content); //邮件正文
//使用SMTP服务发送邮件
EmailKit.useSMTPService(config)
.send(draft, new EmailKit.GetSendCallback() {
@Override
public void run() {
try {
final MailSenderInfo mailInfo = new MailSenderInfo();
mailInfo.setMailServerHost(host);
mailInfo.setMailServerPort(port);
mailInfo.setValidate(true);
mailInfo.setUserName(fromemail); //你的邮箱地址
mailInfo.setPassword(pwd);//您的邮箱密码
mailInfo.setFromAddress(fromemail);//和上面username的邮箱地址一致
mailInfo.setToAddress(toAdd);
mailInfo.setSubject(title);
mailInfo.setContent(content);
mailInfo.setSsl(ssl);
//这个类主要来发送邮件
// 判断是否需要身份认证
MyAuthenticator authenticator = null;
Properties pro = mailInfo.getProperties();
if (mailInfo.isValidate()) {
// 如果需要身份认证则创建一个密码验证器
authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
}
// 根据邮件会话属性和密码验证器构造一个发送邮件的session
Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
try {
// 根据session创建一个邮件消息
final Message mailMessage = new MimeMessage(sendMailSession);
// 创建邮件发送者地址
Address from = new InternetAddress(mailInfo.getFromAddress());
// 设置邮件消息的发送者
mailMessage.setFrom(from);
// 创建邮件的接收者地址并设置到邮件消息中
Address to = new InternetAddress(mailInfo.getToAddress());
mailMessage.setRecipient(Message.RecipientType.TO, to);
// 设置邮件消息的主题
mailMessage.setSubject(mailInfo.getSubject());
// 设置邮件消息发送的时间
mailMessage.setSentDate(new Date());
// 设置邮件消息的主要内容
String mailContent = mailInfo.getContent();
mailMessage.setText(mailContent);
// 发送邮件
Transport.send(mailMessage);
} catch (MessagingException ex) {
ex.printStackTrace();
Log.e(TAG, "error" + ex.getMessage());
if (handError != null) {
android.os.Message msg = new android.os.Message();
msg.what = NOTIFY;
Bundle bundle = new Bundle();
bundle.putString("DATA", ex.getMessage());
msg.setData(bundle);
handError.sendMessage(msg);
}
}
public void onSuccess() {
Log.i(TAG, "发送成功!");
if (handError != null) {
android.os.Message msg = new android.os.Message();
msg.what = NOTIFY;
@ -103,8 +48,24 @@ public class SenderMailMsg {
msg.setData(bundle);
handError.sendMessage(msg);
}
}
Log.e(TAG, "sendEmail success");//sms.sendHtmlMail(mailInfo);//发送html格式
@Override
public void onFailure(String errMsg) {
Log.i(TAG, "发送失败,错误:" + errMsg);
if (handError != null) {
android.os.Message msg = new android.os.Message();
msg.what = NOTIFY;
Bundle bundle = new Bundle();
bundle.putString("DATA", "发送失败,错误:" + errMsg);
msg.setData(bundle);
handError.sendMessage(msg);
}
}
});
//销毁框架
EmailKit.destroy();
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
@ -118,31 +79,6 @@ public class SenderMailMsg {
}
}
}
}).start();
}
}
/**
* public void sendFileMail(View view) {
* <p>
* File file = new File(Environment.getExternalStorageDirectory()+File.separator+"test.txt");
* OutputStream os = null;
* try {
* os = new FileOutputStream(file);
* String str = "hello world";
* byte[] data = str.getBytes();
* os.write(data);
* } catch (FileNotFoundException e) {
* e.printStackTrace();
* } catch (IOException e) {
* e.printStackTrace();
* }finally{
* try {
* if (os != null)os.close();
* } catch (IOException e) {
* }
* }
* SenderMailMsg.send(file,editText.getText().toString());
* }
*/

View File

@ -117,7 +117,7 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录密码" />
android:text="登录密码/授权码" />
<EditText
android:id="@+id/editTextEmailPsw"

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<background android:drawable="@color/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" />
</adaptive-icon>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@color/ic_launcher_background"/>
<background android:drawable="@color/ic_launcher_background" />
<foreground android:drawable="@mipmap/ic_launcher_foreground" />
</adaptive-icon>