send emails
This commit is contained in:
1 parent
4e88587b13
commit
0231051138
14 files changed
+280
-67
No files matched your search
@@ -240,6 +240,7 @@
|
||||
<activity android:name=".ActivityManageTriggerCalendar" />
|
||||
<activity android:name=".ActivityManageTriggerProximity" />
|
||||
<activity android:name=".ActivityManageActionSetSystemSetting" />
|
||||
<activity android:name=".ActivityManageActionSendEmail" />
|
||||
|
||||
<service
|
||||
android:name=".receivers.NotificationListener"
|
||||
|
||||
@@ -238,6 +238,7 @@
|
||||
<activity android:name=".ActivityManageTriggerCalendar" />
|
||||
<activity android:name=".ActivityManageTriggerProximity" />
|
||||
<activity android:name=".ActivityManageActionSetSystemSetting" />
|
||||
<activity android:name=".ActivityManageActionSendEmail" />
|
||||
|
||||
<service
|
||||
android:name=".receivers.NotificationListener"
|
||||
|
||||
@@ -229,6 +229,7 @@
|
||||
<activity android:name=".ActivityManageTriggerCalendar" />
|
||||
<activity android:name=".ActivityManageTriggerProximity" />
|
||||
<activity android:name=".ActivityManageActionSetSystemSetting" />
|
||||
<activity android:name=".ActivityManageActionSendEmail" />
|
||||
|
||||
<service
|
||||
android:name=".receivers.NotificationListener"
|
||||
|
||||
@@ -60,7 +60,8 @@ public class Action
|
||||
setLocationService,
|
||||
sendTextMessage,
|
||||
takeScreenshot,
|
||||
setSystemSetting;
|
||||
setSystemSetting,
|
||||
sendEmail;
|
||||
|
||||
public String getFullName(Context context)
|
||||
{
|
||||
@@ -134,6 +135,8 @@ public class Action
|
||||
return context.getResources().getString(R.string.setLocationServiceCapital);
|
||||
case setSystemSetting:
|
||||
return context.getResources().getString(R.string.setSystemSettingCapital);
|
||||
case sendEmail:
|
||||
return context.getResources().getString(R.string.sendEmail);
|
||||
default:
|
||||
return "Unknown";
|
||||
}
|
||||
@@ -643,6 +646,9 @@ public class Action
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
Actions.setSystemSetting(this.getParameter2(), AutomationService.getInstance());
|
||||
break;
|
||||
case sendEmail:
|
||||
Actions.sendEmailFromAction(this);
|
||||
break;
|
||||
default:
|
||||
Miscellaneous.logEvent("w", "Action", context.getResources().getString(R.string.unknownActionSpecified), 3);
|
||||
break;
|
||||
|
||||
@@ -65,6 +65,7 @@ import java.lang.reflect.Method;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.security.KeyStore;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
@@ -75,6 +76,7 @@ import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
import eu.chainfire.libsuperuser.Shell;
|
||||
@@ -2675,32 +2677,24 @@ public class Actions
|
||||
android.provider.Settings.Secure.putString(context.getContentResolver(), android.provider.Settings.Secure.LOCATION_MODE, String.valueOf(desiredState));
|
||||
}
|
||||
|
||||
/*public static sendEmail()
|
||||
public static boolean sendEmailFromAction(Action action)
|
||||
{
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.host", smtpHost);
|
||||
props.put("mail.smtp.port", "587");
|
||||
props.put("mail.smtp.auth", "true");
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.connectiontimeout", "10000");
|
||||
props.put("mail.smtp.timeout", "10000");
|
||||
try
|
||||
{
|
||||
String[] components = action.getParameter2().split(Action.actionParameter2Split, -1);
|
||||
EmailService es = new EmailService(Settings.smtpServerHostname, Integer.parseInt(Settings.smtpServerPort), Settings.smtpServerUseTls);
|
||||
List<InternetAddress> toAddressList = new ArrayList<>();
|
||||
toAddressList.add(new InternetAddress(components[0]));
|
||||
|
||||
Session session = Session.getInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
});
|
||||
|
||||
Message message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(fromAddress));
|
||||
message.setRecipients(
|
||||
Message.RecipientType.TO,
|
||||
InternetAddress.parse(recipientAddress)
|
||||
);
|
||||
message.setSubject(subject);
|
||||
message.setText(body);
|
||||
|
||||
Transport.send(message);
|
||||
}*/
|
||||
EmailService.Email em = new EmailService.Email(new EmailService.UserPassAuthenticator(Settings.smtpUsername, Settings.smtpPassword), new InternetAddress(Settings.smtpFromAddress), toAddressList, components[1], components[2]);
|
||||
es.send(em);
|
||||
Miscellaneous.logEvent("i", "sendEmail()", "Email sent.", 1);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Miscellaneous.logEvent("e", "sendEmail()", "Email could not be sent: " + Log.getStackTraceString(e), 1);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package com.jens.automation2;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.provider.ContactsContract;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.EditText;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.jens.automation2.Action.Action_Enum;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
|
||||
public class ActivityManageActionSendEmail extends Activity
|
||||
{
|
||||
Button bSaveSendEmail;
|
||||
TextView tvEmailServerConfigurationNotice;
|
||||
EditText etEmailRecipient, etEmailSubject, etEmailText;
|
||||
|
||||
public static boolean edit = false;
|
||||
public static Action resultingAction = null;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
Miscellaneous.setDisplayLanguage(this);
|
||||
Miscellaneous.setUiTheme(this);
|
||||
this.setContentView(R.layout.activity_manage_action_send_email);
|
||||
|
||||
tvEmailServerConfigurationNotice = (TextView)findViewById(R.id.tvEmailServerConfigurationNotice);
|
||||
etEmailRecipient = (EditText)findViewById(R.id.etEmailRecipient);
|
||||
etEmailSubject = (EditText)findViewById(R.id.etEmailSubject);
|
||||
etEmailText = (EditText)findViewById(R.id.etEmailText);
|
||||
bSaveSendEmail = (Button)findViewById(R.id.bSaveSendEmail);
|
||||
|
||||
if(getIntent().hasExtra(ActivityManageRule.intentNameActionParameter2))
|
||||
{
|
||||
String[] components = getIntent().getStringExtra(ActivityManageRule.intentNameActionParameter2).split(Action.actionParameter2Split, -1);
|
||||
etEmailRecipient.setText(components[0]);
|
||||
etEmailSubject.setText(components[1]);
|
||||
etEmailText.setText(components[2]);
|
||||
}
|
||||
|
||||
if(Settings.smtpServerHostname.isEmpty())
|
||||
tvEmailServerConfigurationNotice.setVisibility(View.VISIBLE);
|
||||
else
|
||||
tvEmailServerConfigurationNotice.setVisibility(View.GONE);
|
||||
|
||||
bSaveSendEmail.setOnClickListener(new OnClickListener()
|
||||
{
|
||||
@Override
|
||||
public void onClick(View v)
|
||||
{
|
||||
if(etEmailRecipient.getText().toString().length() > 0 && etEmailSubject.getText().toString().length() > 0 && etEmailText.getText().toString().length() > 0)
|
||||
{
|
||||
if (resultingAction == null)
|
||||
resultingAction = new Action();
|
||||
|
||||
String responseString = etEmailRecipient.getText().toString() + Action.actionParameter2Split + etEmailSubject.getText().toString() + Action.actionParameter2Split + etEmailText.getText().toString();
|
||||
|
||||
Intent answer = new Intent();
|
||||
|
||||
answer.putExtra(ActivityManageRule.intentNameActionParameter2, responseString);
|
||||
|
||||
setResult(RESULT_OK, answer);
|
||||
ActivityManageActionSendEmail.this.finish();
|
||||
}
|
||||
else
|
||||
Toast.makeText(getBaseContext(), getResources().getString(R.string.enterAllValues), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -151,6 +151,8 @@ public class ActivityManageRule extends Activity
|
||||
final static int requestCodeTriggerProximityEdit = 838;
|
||||
final static int requestCodeActionSetSystemSettingAdd = 839;
|
||||
final static int requestCodeActionSetSystemSettingEdit = 840;
|
||||
final static int requestCodeActionSendEmailAdd = 841;
|
||||
final static int requestCodeActionSendEmailEdit = 842;
|
||||
|
||||
public static ActivityManageRule getInstance()
|
||||
{
|
||||
@@ -2263,6 +2265,32 @@ public class ActivityManageRule extends Activity
|
||||
this.refreshActionList();
|
||||
}
|
||||
}
|
||||
else if(requestCode == requestCodeActionSendEmailAdd)
|
||||
{
|
||||
if(resultCode == RESULT_OK)
|
||||
{
|
||||
newAction.setParentRule(ruleToEdit);
|
||||
// newAction.setParameter1(data.getBooleanExtra(intentNameActionParameter1, false));
|
||||
newAction.setParameter2(data.getStringExtra(intentNameActionParameter2));
|
||||
ruleToEdit.getActionSet().add(newAction);
|
||||
this.refreshActionList();
|
||||
}
|
||||
}
|
||||
else if(requestCode == requestCodeActionSendEmailEdit)
|
||||
{
|
||||
if(resultCode == RESULT_OK)
|
||||
{
|
||||
ruleToEdit.getActionSet().get(editIndex).setParentRule(ruleToEdit);
|
||||
// ruleToEdit.getActionSet().get(editIndex).setParameter1(data.getBooleanExtra(intentNameActionParameter1, false));
|
||||
|
||||
if(data.hasExtra(intentNameActionParameter2))
|
||||
{
|
||||
ruleToEdit.getActionSet().get(editIndex).setParameter2(data.getStringExtra(intentNameActionParameter2));
|
||||
}
|
||||
|
||||
this.refreshActionList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected AlertDialog getActionTypeDialog()
|
||||
@@ -2343,14 +2371,15 @@ public class ActivityManageRule extends Activity
|
||||
items.add(new Item(typesLong[i].toString(), R.drawable.variable));
|
||||
else if(types[i].toString().equals(Action_Enum.setLocationService.toString()))
|
||||
items.add(new Item(typesLong[i].toString(), R.drawable.compass_small));
|
||||
else if(types[i].toString().equals(Action_Enum.sendEmail.toString()))
|
||||
items.add(new Item(typesLong[i].toString(), R.drawable.email));
|
||||
else if(types[i].toString().equals(Action_Enum.takeScreenshot.toString()))
|
||||
{
|
||||
if(ActivityPermissions.isServiceAvailable(Miscellaneous.getAnyContext(), "MyAccessibilityService"))
|
||||
items.add(new Item(typesLong[i].toString(), R.drawable.copier));
|
||||
}
|
||||
else if(types[i].toString().equals(Action_Enum.setSystemSetting.toString()))
|
||||
{ items.add(new Item(typesLong[i].toString(), R.drawable.gears_small));
|
||||
}
|
||||
items.add(new Item(typesLong[i].toString(), R.drawable.gears_small));
|
||||
else
|
||||
items.add(new Item(typesLong[i].toString(), R.drawable.placeholder));
|
||||
}
|
||||
@@ -2595,6 +2624,12 @@ public class ActivityManageRule extends Activity
|
||||
Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionSetSystemSetting.class);
|
||||
startActivityForResult(intent, requestCodeActionSetSystemSettingAdd);
|
||||
}
|
||||
else if(actionTypes[which].toString().equals(Action_Enum.sendEmail.toString()))
|
||||
{
|
||||
newAction.setAction(Action_Enum.sendEmail);
|
||||
Intent intent = new Intent(ActivityManageRule.this, ActivityManageActionSendEmail.class);
|
||||
startActivityForResult(intent, requestCodeActionSendEmailAdd);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -434,33 +434,6 @@ public class AutomationService extends Service implements OnInitListener
|
||||
Settings.writeSettings(Miscellaneous.getAnyContext());
|
||||
}
|
||||
|
||||
// try
|
||||
// {
|
||||
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
|
||||
EmailService es = new EmailService(Settings.smtpServerHostname, Integer.parseInt(Settings.smtpServerPort));
|
||||
|
||||
List addresses = new ArrayList();
|
||||
addresses.add(Settings.smtpFromAddress);
|
||||
try
|
||||
{
|
||||
EmailService.Email em = new EmailService.Email(new EmailService.UserPassAuthenticator(Settings.smtpUsername, Settings.smtpPassword), addresses, new InternetAddress(String.valueOf(addresses.get(0))), "test", "testbody");
|
||||
es.send(em);
|
||||
Miscellaneous.logEvent("i", "sendEmail()", "Email sent.", 1);
|
||||
}
|
||||
catch (AddressException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
catch (MessagingException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
// }
|
||||
// catch(Exception e)
|
||||
// {
|
||||
// Miscellaneous.logEvent("e", "sendMail()", Log.getStackTraceString(e), 1);
|
||||
// }
|
||||
|
||||
Settings.serviceStartDone = true;
|
||||
Settings.deviceStartDone = true;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import javax.mail.MessagingException;
|
||||
import javax.mail.PasswordAuthentication;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.Transport;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
@@ -20,28 +19,30 @@ public class EmailService
|
||||
{
|
||||
private final String server;
|
||||
private final int port;
|
||||
private final boolean useTls;
|
||||
|
||||
public static final String CLIENT_NAME = "Android StackOverflow programmatic email";
|
||||
|
||||
public EmailService(String server, int port)
|
||||
public EmailService(String server, int port, boolean useTls)
|
||||
{
|
||||
this.server = server;
|
||||
this.port = port;
|
||||
this.useTls = useTls;
|
||||
}
|
||||
|
||||
public static class Email
|
||||
{
|
||||
private final Authenticator auth;
|
||||
private Authenticator auth;
|
||||
private final List<InternetAddress> toList;
|
||||
private final Address from;
|
||||
private final String subject;
|
||||
private final String body;
|
||||
|
||||
public Email(Authenticator auth, List<InternetAddress> toList, Address from, String subject, String body)
|
||||
public Email(Authenticator auth, Address from, List<InternetAddress> toList, String subject, String body)
|
||||
{
|
||||
this.auth = auth;
|
||||
this.toList = toList;
|
||||
this.from = from;
|
||||
this.toList = toList;
|
||||
this.subject = subject;
|
||||
this.body = body;
|
||||
}
|
||||
@@ -93,12 +94,26 @@ public class EmailService
|
||||
public void send(Email email) throws MessagingException
|
||||
{
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.auth", "true");
|
||||
if(((UserPassAuthenticator)email.getAuth()).getPasswordAuthentication().getUserName().isEmpty())
|
||||
{
|
||||
props.put("mail.smtp.auth", "false");
|
||||
email.auth = null;
|
||||
}
|
||||
else
|
||||
props.put("mail.smtp.auth", "true");
|
||||
|
||||
props.put("mail.user", email.getFrom().toString());
|
||||
props.put("mail.smtp.host", server);
|
||||
props.put("mail.smtp.port", String.valueOf(port));
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.ssl.trust", server);
|
||||
|
||||
if(useTls)
|
||||
{
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
props.put("mail.smtp.ssl.trust", server);
|
||||
}
|
||||
else
|
||||
props.put("mail.smtp.starttls.enable", "false");
|
||||
|
||||
props.put("mail.mime.charset", "UTF-8");
|
||||
|
||||
Session session = Session.getDefaultInstance(props, email.getAuth());
|
||||
@@ -106,9 +121,8 @@ public class EmailService
|
||||
|
||||
msg.setFrom(email.getFrom());
|
||||
msg.setSentDate(Calendar.getInstance().getTime());
|
||||
Address[] toarray = new Address[]{email.getFrom()};
|
||||
msg.setRecipients(Message.RecipientType.TO, toarray);
|
||||
// msg.setRecipients(Message.RecipientType.TO, email.getToList().toArray(new InternetAddress[0]));
|
||||
Address[] toArray = new Address[]{email.getFrom()};
|
||||
msg.setRecipients(Message.RecipientType.TO, toArray);
|
||||
|
||||
msg.setReplyTo(new Address[]{email.getFrom()});
|
||||
|
||||
|
||||
@@ -111,6 +111,7 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.net.ssl.HostnameVerifier;
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
@@ -0,0 +1,91 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_margin="@dimen/default_margin" >
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="25dp"
|
||||
android:textStyle="bold"
|
||||
android:layout_marginBottom="@dimen/default_margin"
|
||||
android:text="@string/sendEmail" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvEmailServerConfigurationNotice"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/emailConfigurationNotice"
|
||||
android:textColor="#ea131b"
|
||||
android:layout_marginBottom="@dimen/default_margin" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/emailRecipient" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etEmailRecipient"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ems="10"
|
||||
android:inputType="textEmailAddress" >
|
||||
|
||||
<requestFocus />
|
||||
</EditText>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/emailSubject" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etEmailSubject"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ems="10"
|
||||
android:inputType="text" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/textToSend" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/etEmailText"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ems="10"
|
||||
android:inputType="text|textMultiLine" >
|
||||
</EditText>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvLegend"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/urlLegend" />
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/attachment" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/bSaveSendEmail"
|
||||
android:layout_marginTop="15dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/save" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</ScrollView>
|
||||
@@ -995,4 +995,10 @@
|
||||
<string name="smtpUsernameSummary">Leave empty for no authentication</string>
|
||||
<string name="smtpPasswordTitle">Password</string>
|
||||
<string name="smtpPasswordSummary">If username is empty, no authentication is used.</string>
|
||||
<string name="sendEmail">Send email</string>
|
||||
<string name="emailTo">Recipient</string>
|
||||
<string name="emailRecipient">Recipient</string>
|
||||
<string name="emailSubject">Subject</string>
|
||||
<string name="enterAllValues">Enter all values.</string>
|
||||
<string name="emailConfigurationNotice">Remember to configure an SMTP server in settings.</string>
|
||||
</resources>
|
||||
@@ -1,4 +1,5 @@
|
||||
* Added: Triggers and actions can now be cloned
|
||||
* Added: URL checker for triggerUrl action
|
||||
* Added: Send emails as action
|
||||
* Fixed: Crash when starting to add/edit a triggerURL action while the app or the system are set to the German language
|
||||
* Fixed: Crash when starting service in Google Play version
|
||||
Reference in new issue
Block a user