Skip to content

Commit

Permalink
新增:邮件发送支持IMAP协议(界面部分)
Browse files Browse the repository at this point in the history
  • Loading branch information
pppscn committed Nov 25, 2021
1 parent bb2d028 commit a25ba3e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 62 deletions.
45 changes: 24 additions & 21 deletions app/src/main/java/com/idormy/sms/forwarder/SenderActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,9 @@ private void setEmail(final SenderModel senderModel) {
final EditText editTextEmailPsw = view1.findViewById(R.id.editTextEmailPsw);
final EditText editTextEmailToAdd = view1.findViewById(R.id.editTextEmailToAdd);
final EditText editTextEmailTitle = view1.findViewById(R.id.editTextEmailTitle);
final RadioGroup radioGroupEmailProtocol = view1.findViewById(R.id.radioGroupEmailProtocol);
if (emailSettingVo != null) {
radioGroupEmailProtocol.check(emailSettingVo.getEmailProtocolCheckId());
editTextEmailHost.setText(emailSettingVo.getHost());
editTextEmailPort.setText(emailSettingVo.getPort());
switchEmailSSl.setChecked(emailSettingVo.getSsl());
Expand All @@ -384,21 +386,31 @@ private void setEmail(final SenderModel senderModel) {
final AlertDialog show = alertDialog71.show();

buttonEmailOk.setOnClickListener(view -> {
String protocol = radioGroupEmailProtocol.getCheckedRadioButtonId() == R.id.radioEmailProtocolSmtp ? "SMTP" : "IMAP";
String host = editTextEmailHost.getText().toString().trim();
String port = editTextEmailPort.getText().toString().trim();
boolean ssl = switchEmailSSl.isChecked();
String fromEmail = editTextEmailFromAdd.getText().toString().trim();
String pwd = editTextEmailPsw.getText().toString().trim();
String toEmail = editTextEmailToAdd.getText().toString().trim();

String title = editTextEmailTitle.getText().toString().trim();
if (title.isEmpty()) title = "SmsForwarder Title";

String nickname = editTextEmailNickname.getText().toString().trim();
if (nickname.isEmpty()) nickname = "SmsForwarder";
if (host.isEmpty() || port.isEmpty() || fromEmail.isEmpty() || pwd.isEmpty() || toEmail.isEmpty()) {
Toast.makeText(SenderActivity.this, R.string.invalid_email, Toast.LENGTH_LONG).show();
return;
}

EmailSettingVo emailSettingVoNew = new EmailSettingVo(protocol, host, port, ssl, fromEmail, nickname, pwd, toEmail, title);

if (senderModel == null) {
SenderModel newSenderModel = new SenderModel();
newSenderModel.setName(editTextEmailName.getText().toString().trim());
newSenderModel.setType(TYPE_EMAIL);
newSenderModel.setStatus(STATUS_ON);
EmailSettingVo emailSettingVoNew = new EmailSettingVo(
editTextEmailHost.getText().toString().trim(),
editTextEmailPort.getText().toString().trim(),
switchEmailSSl.isChecked(),
editTextEmailFromAdd.getText().toString().trim(),
editTextEmailNickname.getText().toString().trim(),
editTextEmailPsw.getText().toString().trim(),
editTextEmailToAdd.getText().toString().trim(),
editTextEmailTitle.getText().toString().trim()
);
newSenderModel.setJsonSetting(JSON.toJSONString(emailSettingVoNew));
SenderUtil.addSender(newSenderModel);
initSenders();
Expand All @@ -407,16 +419,6 @@ private void setEmail(final SenderModel senderModel) {
senderModel.setName(editTextEmailName.getText().toString().trim());
senderModel.setType(TYPE_EMAIL);
senderModel.setStatus(STATUS_ON);
EmailSettingVo emailSettingVoNew = new EmailSettingVo(
editTextEmailHost.getText().toString().trim(),
editTextEmailPort.getText().toString().trim(),
switchEmailSSl.isChecked(),
editTextEmailFromAdd.getText().toString().trim(),
editTextEmailNickname.getText().toString().trim(),
editTextEmailPsw.getText().toString().trim(),
editTextEmailToAdd.getText().toString().trim(),
editTextEmailTitle.getText().toString().trim()
);
senderModel.setJsonSetting(JSON.toJSONString(emailSettingVoNew));
SenderUtil.updateSender(senderModel);
initSenders();
Expand All @@ -434,6 +436,7 @@ private void setEmail(final SenderModel senderModel) {
show.dismiss();
});
buttonEmailTest.setOnClickListener(view -> {
String protocol = radioGroupEmailProtocol.getCheckedRadioButtonId() == R.id.radioEmailProtocolSmtp ? "SMTP" : "IMAP";
String host = editTextEmailHost.getText().toString().trim();
String port = editTextEmailPort.getText().toString().trim();
boolean ssl = switchEmailSSl.isChecked();
Expand All @@ -449,7 +452,7 @@ private void setEmail(final SenderModel senderModel) {

if (!host.isEmpty() && !port.isEmpty() && !fromEmail.isEmpty() && !pwd.isEmpty() && !toEmail.isEmpty()) {
try {
SenderMailMsg.sendEmail(0, handler, host, port, ssl, fromEmail, nickname, pwd, toEmail, title, R.string.test_content + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
SenderMailMsg.sendEmail(0, handler, protocol, host, port, ssl, fromEmail, nickname, pwd, toEmail, title, R.string.test_content + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
} catch (Exception e) {
Toast.makeText(SenderActivity.this, getString(R.string.failed_to_fwd) + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.idormy.sms.forwarder.model.vo;

import com.idormy.sms.forwarder.R;

import java.io.Serializable;

import lombok.Data;
Expand All @@ -14,11 +16,13 @@ public class EmailSettingVo implements Serializable {
private String pwd;
private String toEmail;
private String title;
private String protocol;

public EmailSettingVo() {
}

public EmailSettingVo(String host, String port, Boolean ssl, String fromEmail, String nickname, String pwd, String toEmail, String title) {
public EmailSettingVo(String protocol, String host, String port, Boolean ssl, String fromEmail, String nickname, String pwd, String toEmail, String title) {
this.protocol = protocol;
this.host = host;
this.port = port;
this.ssl = ssl;
Expand All @@ -29,4 +33,11 @@ public EmailSettingVo(String host, String port, Boolean ssl, String fromEmail, S
this.title = title;
}

public int getEmailProtocolCheckId() {
if (protocol == null || protocol.equals("SMTP")) {
return R.id.radioEmailProtocolSmtp;
} else {
return R.id.radioEmailProtocolImap;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static void senderSendMsg(Handler handError, SmsVo smsVo, SenderModel sen
EmailSettingVo emailSettingVo = JSON.parseObject(senderModel.getJsonSetting(), EmailSettingVo.class);
if (emailSettingVo != null) {
try {
SenderMailMsg.sendEmail(logId, handError, emailSettingVo.getHost(), emailSettingVo.getPort(), emailSettingVo.getSsl(), emailSettingVo.getFromEmail(), emailSettingVo.getNickname(),
SenderMailMsg.sendEmail(logId, handError, emailSettingVo.getProtocol(), emailSettingVo.getHost(), emailSettingVo.getPort(), emailSettingVo.getSsl(), emailSettingVo.getFromEmail(), emailSettingVo.getNickname(),
emailSettingVo.getPwd(), emailSettingVo.getToEmail(), smsVo.getTitleForSend(emailSettingVo.getTitle()), smsVo.getSmsVoForSend(smsTemplate));
} catch (Exception e) {
LogUtil.updateLog(logId, 0, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
public class SenderMailMsg extends SenderBaseMsg {
private static final String TAG = "SenderMailMsg";

public static void sendEmail(final long logId, final Handler handError, final String host, final String port, final boolean ssl, final String fromEmail, final String nickname, final String pwd, final String toAdd, final String title, final String content) {
public static void sendEmail(final long logId, final Handler handError, final String protocol, final String host, final String port, final boolean ssl, final String fromEmail, final String nickname, final String pwd, final String toAdd, final String title, final String content) {

Log.d(TAG, "sendEmail: host:" + host + " port:" + port + " ssl:" + ssl + " fromEmail:" + fromEmail + " nickname:" + nickname + " pwd:" + pwd + " toAdd:" + toAdd);
Log.d(TAG, "sendEmail: protocol:" + protocol + " host:" + host + " port:" + port + " ssl:" + ssl + " fromEmail:" + fromEmail + " nickname:" + nickname + " pwd:" + pwd + " toAdd:" + toAdd);

Observable
.create((ObservableEmitter<Object> emitter) -> {
Expand Down
133 changes: 101 additions & 32 deletions app/src/main/res/layout/alert_dialog_setview_email.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@
tools:ignore="LabelFor,TextFields" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:text="@string/account"
android:textSize="14sp"
android:textColor="@color/colorBlueGreyDark"
tools:ignore="HardcodedText" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand All @@ -43,18 +53,18 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email_host" />
android:text="@string/email_account" />

<EditText
android:id="@+id/editTextEmailHost"
android:id="@+id/editTextEmailFromAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="14"
android:inputType="text"
android:inputType="textEmailAddress"
android:text=""
tools:ignore="LabelFor,TextFields" />
tools:ignore="LabelFor" />
</LinearLayout>

<LinearLayout
Expand All @@ -67,54 +77,91 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/smtp_port" />
android:text="@string/email_password" />

<EditText
android:id="@+id/editTextEmailPort"
android:id="@+id/editTextEmailPsw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="5"
android:inputType="text"
android:ems="14"
android:inputType="textPassword"
android:text=""
tools:ignore="LabelFor,TextFields" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enable_ssl" />
android:text="@string/email_nickname" />

<Switch
android:id="@+id/switchEmailSSl"
<EditText
android:id="@+id/editTextEmailNickname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
tools:ignore="UseSwitchCompatOrMaterialXml" />
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="14"
android:inputType="text"
android:text=""
tools:ignore="LabelFor,TextFields" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:text="@string/servers"
android:textSize="14sp"
android:textColor="@color/colorBlueGreyDark"
tools:ignore="HardcodedText" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:visibility="gone">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email_account" />
android:paddingTop="5dp"
android:text="@string/email_protocol" />

<EditText
android:id="@+id/editTextEmailFromAdd"
<RadioGroup
android:id="@+id/radioGroupEmailProtocol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="14"
android:inputType="textEmailAddress"
android:text=""
tools:ignore="LabelFor" />
android:orientation="horizontal">

<RadioButton
android:id="@+id/radioEmailProtocolSmtp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/email_smtp" />

<RadioButton
android:id="@+id/radioEmailProtocolImap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email_imap" />

</RadioGroup>

</LinearLayout>

<LinearLayout
Expand All @@ -127,10 +174,10 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email_nickname" />
android:text="@string/email_host" />

<EditText
android:id="@+id/editTextEmailNickname"
android:id="@+id/editTextEmailHost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
Expand All @@ -151,20 +198,42 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email_password" />
android:text="@string/smtp_port" />

<EditText
android:id="@+id/editTextEmailPsw"
android:id="@+id/editTextEmailPort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="3dp"
android:autofillHints=""
android:ems="14"
android:inputType="textPassword"
android:ems="5"
android:inputType="text"
android:text=""
tools:ignore="LabelFor,TextFields" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enable_ssl" />

<Switch
android:id="@+id/switchEmailSSl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:text="@string/email"
android:textSize="14sp"
android:textColor="@color/colorBlueGreyDark"
tools:ignore="HardcodedText" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -241,7 +310,7 @@
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:background="@color/colorBlueGrey"
android:text="@string/insert_sender"
tools:ignore="ButtonStyle,NestedWeights" />

Expand All @@ -253,7 +322,7 @@
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:background="@color/colorBlueGrey"
android:text="@string/insert_extra"
tools:ignore="ButtonStyle,NestedWeights" />

Expand All @@ -265,7 +334,7 @@
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:background="@color/colorBlueGrey"
android:text="@string/insert_time"
tools:ignore="ButtonStyle,NestedWeights" />

Expand All @@ -277,7 +346,7 @@
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:background="@color/colorBlueGrey"
android:text="@string/insert_device_name"
tools:ignore="ButtonStyle,NestedWeights" />

Expand Down
Loading

0 comments on commit a25ba3e

Please sign in to comment.