Skip to content

Commit

Permalink
fix email ssl
Browse files Browse the repository at this point in the history
v3.2.0
  • Loading branch information
xiaoyuanhost committed Jan 6, 2021
1 parent a428b70 commit c218b90
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 47 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ APP下载 [https://pan.baidu.com/s/1kbelTFIf5nwkOY9g6itkvA]
![应用更新](pic/update-dingdingsecret.jpg "应用更新")

### 更新记录:
> [v3.2.0](pic/TSMS_release_20210106_3.2.0.apk) 1,增加邮箱SSL配置。2,邮箱测试结果通知
> [v3.1.0](pic/TSMS_release_20201231_3.1.0.apk) 1,界面重构。2,增加转发规则页面。3,增加发送方页面。4,升级配置页面
> [v2.1.0](pic/TSMS_release_20200806_2.1.0.apk) 1,增加新版钉钉群机器人安全设置中的加签
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.tim.tsms.transpondsms"
minSdkVersion 23
targetSdkVersion 28
versionCode 31
versionName "3.1.0"
versionCode 32
versionName "3.2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion app/release/output.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":31,"versionName":"3.1.0","enabled":true,"outputFile":"TSMS_release_20201231_3.1.0.apk","fullName":"release","baseName":"release"},"path":"TSMS_release_20201231_3.1.0.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":32,"versionName":"3.2.0","enabled":true,"outputFile":"TSMS_release_20210106_3.2.0.apk","fullName":"release","baseName":"release"},"path":"TSMS_release_20210106_3.2.0.apk","properties":{}}]
11 changes: 9 additions & 2 deletions app/src/main/java/com/tim/tsms/transpondsms/SenderActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class SenderActivity extends AppCompatActivity {
// 用于存储数据
private List<SenderModel> senderModels = new ArrayList<>();
private SenderAdapter adapter;
public static final int NOTIFY = 0x9731;
public static final int NOTIFY = 0x9731993;
//消息处理者,创建一个Handler的子类对象,目的是重写Handler的处理消息的方法(handleMessage())
private Handler handler = new Handler(){
@Override
Expand Down Expand Up @@ -245,6 +246,9 @@ private void setEmail(final SenderModel senderModel) {
if (emailSettingVo != null) editTextEmailHost.setText(emailSettingVo.getHost());
final EditText editTextEmailPort = view1.findViewById(R.id.editTextEmailPort);
if (emailSettingVo != null) editTextEmailPort.setText(emailSettingVo.getPort());

final Switch switchEmailSSl = view1.findViewById(R.id.switchEmailSSl);
if (emailSettingVo != null) switchEmailSSl.setChecked(emailSettingVo.getSsl());
final EditText editTextEmailFromAdd = view1.findViewById(R.id.editTextEmailFromAdd);
if (emailSettingVo != null) editTextEmailFromAdd.setText(emailSettingVo.getFromEmail());
final EditText editTextEmailPsw = view1.findViewById(R.id.editTextEmailPsw);
Expand Down Expand Up @@ -274,6 +278,7 @@ public void onClick(View view) {
EmailSettingVo emailSettingVonew = new EmailSettingVo(
editTextEmailHost.getText().toString(),
editTextEmailPort.getText().toString(),
switchEmailSSl.isChecked(),
editTextEmailFromAdd.getText().toString(),
editTextEmailPsw.getText().toString(),
editTextEmailToAdd.getText().toString()
Expand All @@ -289,6 +294,7 @@ public void onClick(View view) {
EmailSettingVo emailSettingVonew = new EmailSettingVo(
editTextEmailHost.getText().toString(),
editTextEmailPort.getText().toString(),
switchEmailSSl.isChecked(),
editTextEmailFromAdd.getText().toString(),
editTextEmailPsw.getText().toString(),
editTextEmailToAdd.getText().toString()
Expand Down Expand Up @@ -321,12 +327,13 @@ public void onClick(View view) {
public void onClick(View view) {
String host = editTextEmailHost.getText().toString();
String port = editTextEmailPort.getText().toString();
Boolean ssl = switchEmailSSl.isChecked();
String fromemail = editTextEmailFromAdd.getText().toString();
String pwd = editTextEmailPsw.getText().toString();
String toemail = editTextEmailToAdd.getText().toString();
if (!host.isEmpty() && !port.isEmpty() && !fromemail.isEmpty() && !pwd.isEmpty() && !toemail.isEmpty()) {
try {
SenderMailMsg.sendEmail(true,host,port,fromemail,pwd,toemail,"TranspondSms test", "test@" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
SenderMailMsg.sendEmail(handler,host,port,ssl,fromemail,pwd,toemail,"TranspondSms test", "test@" + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
} catch (Exception e) {
Toast.makeText(SenderActivity.this, "发送失败:" + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
public class EmailSettingVo implements Serializable {
private String host;
private String port;
private Boolean ssl=true;
private String fromEmail;
private String pwd;
private String toEmail;

public EmailSettingVo() {
}

public EmailSettingVo(String host, String port, String fromEmail, String pwd, String toEmail) {
public EmailSettingVo(String host, String port, Boolean ssl, String fromEmail, String pwd, String toEmail) {
this.host = host;
this.port = port;
this.ssl = ssl;
this.fromEmail = fromEmail;
this.pwd = pwd;
this.toEmail = toEmail;
Expand All @@ -32,6 +34,14 @@ public String getPort() {
return port;
}

public Boolean getSsl() {
return ssl;
}

public void setSsl(Boolean ssl) {
this.ssl = ssl;
}

public void setPort(String port) {
this.port = port;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.tim.tsms.transpondsms.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";
Expand All @@ -16,6 +22,8 @@ public class MailSenderInfo {
private String password;
// 是否需要身份验证
private boolean validate = true;
//开启ssl
private boolean ssl = true;
// 邮件主题
private String subject;
// 邮件的文本内容
Expand All @@ -27,11 +35,25 @@ public class MailSenderInfo {
* 获得邮件会话属性
*/
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");
return p;
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() {
Expand Down Expand Up @@ -113,5 +135,13 @@ public String getContent() {
public void setContent(String textContent) {
this.content = textContent;
}

public boolean isSsl() {
return ssl;
}

public void setSsl(boolean ssl) {
this.ssl = ssl;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static void senderSendMsg(SmsVo smsVo,SenderModel senderModel) {
EmailSettingVo emailSettingVo = JSON.parseObject(senderModel.getJsonSetting(), EmailSettingVo.class);
if(emailSettingVo!=null){
try {
SenderMailMsg.sendEmail(false, emailSettingVo.getHost(),emailSettingVo.getPort(),emailSettingVo.getFromEmail(),
SenderMailMsg.sendEmail(null, emailSettingVo.getHost(),emailSettingVo.getPort(),emailSettingVo.getSsl(),emailSettingVo.getFromEmail(),
emailSettingVo.getPwd(),emailSettingVo.getToEmail(),smsVo.getMobile(),smsVo.getSmsVoForSend());
}catch (Exception e){
Log.e(TAG, "senderSendMsg: SenderMailMsg error "+e.getMessage() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class SenderMailMsg {
// private static final String FROM_ADD = "[email protected]";
// private static final String FROM_PSW = "xx";

public static void sendEmail(final boolean handError,final String host, final String port, final String fromemail, final String pwd, final String toAdd, final String title, final String content) {
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+" fromemail:"+fromemail+" pwd:"+pwd+" toAdd:"+toAdd);
Log.d(TAG, "sendEmail: host:" + host + " port:" + port + " ssl:" + ssl + " fromemail:" + fromemail + " pwd:" + pwd + " toAdd:" + toAdd);
new Thread(new Runnable() {
@Override
public void run() {
Expand All @@ -50,6 +50,7 @@ public void run() {
mailInfo.setToAddress(toAdd);
mailInfo.setSubject(title);
mailInfo.setContent(content);
mailInfo.setSsl(ssl);

//这个类主要来发送邮件
// 判断是否需要身份认证
Expand Down Expand Up @@ -83,38 +84,37 @@ public void run() {

} catch (MessagingException ex) {
ex.printStackTrace();
Log.e(TAG, "error"+ex.getMessage());
if(handError){
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());
bundle.putString("DATA", ex.getMessage());
msg.setData(bundle);
(new Handler()).sendMessage(msg);
handError.sendMessage(msg);
}


}
if(handError){
if (handError != null) {
android.os.Message msg = new android.os.Message();
msg.what = NOTIFY;
Bundle bundle = new Bundle();
bundle.putString("DATA","发送成功");
bundle.putString("DATA", "发送成功");
msg.setData(bundle);
(new Handler()).sendMessage(msg);
handError.sendMessage(msg);
}

Log.e(TAG, "sendEmail success");//sms.sendHtmlMail(mailInfo);//发送html格式

} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
if(handError){
if (handError != null) {
android.os.Message msg = new android.os.Message();
msg.what = NOTIFY;
Bundle bundle = new Bundle();
bundle.putString("DATA",e.getMessage());
bundle.putString("DATA", e.getMessage());
msg.setData(bundle);
(new Handler()).sendMessage(msg);
handError.sendMessage(msg);
}

}
Expand All @@ -125,26 +125,24 @@ public void run() {

/**
* public void sendFileMail(View view) {
*
* 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());
* }
*
*
* <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());
* }
*/
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_alter_dialog_setview_email.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@
android:inputType=""
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="是否开启ssl" />

<Switch
android:id="@+id/switchEmailSSl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:ems="10"
android:checked="true"/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
Expand Down
Binary file added pic/TSMS_release_20210106_3.2.0.apk
Binary file not shown.

0 comments on commit c218b90

Please sign in to comment.