From ecf76380ccda48f62ee91b6f6ecd80bc7d117fd3 Mon Sep 17 00:00:00 2001 From: jens Date: Sun, 4 Oct 2026 18:43:40 +0200 Subject: [PATCH] send emails --- app/build.gradle | 8 ++- .../java/com/jens/automation2/Actions.java | 64 ++++++++++++++----- .../jens/automation2/AutomationService.java | 2 + .../java/com/jens/automation2/Settings.java | 46 +++++++++++++ app/src/main/res/layout/activity_settings.xml | 41 ++++++++++++ app/src/main/res/values/strings.xml | 12 ++++ 6 files changed, 156 insertions(+), 17 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e63d54d..27208ad 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -66,7 +66,13 @@ android { buildToolsVersion '36.0.0' } - { +packaging { + resources { + excludes += ["META-INF/LICENSE-2.0.txt"] + } +} + +dependencies { implementation 'org.jetbrains:annotations:26.1.0' googlePlayFlavorImplementation 'com.google.firebase:firebase-appindexing:20.0.0' googlePlayFlavorImplementation 'com.google.android.gms:play-services-location:20.0.0' diff --git a/app/src/main/java/com/jens/automation2/Actions.java b/app/src/main/java/com/jens/automation2/Actions.java index dbdf6da..fa7b9ae 100644 --- a/app/src/main/java/com/jens/automation2/Actions.java +++ b/app/src/main/java/com/jens/automation2/Actions.java @@ -71,6 +71,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Random; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -78,6 +79,14 @@ import java.util.concurrent.TimeUnit; import javax.net.ssl.SSLContext; import eu.chainfire.libsuperuser.Shell; +import jakarta.mail.Authenticator; +import jakarta.mail.Message; +import jakarta.mail.MessagingException; +import jakarta.mail.PasswordAuthentication; +import jakarta.mail.Session; +import jakarta.mail.Transport; +import jakarta.mail.internet.InternetAddress; +import jakarta.mail.internet.MimeMessage; public class Actions { @@ -2675,32 +2684,55 @@ 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 sendEmail(String to, String subject, String messageText) + { + // Use default SMTP settings + return sendEmail(to, subject, messageText, Settings.smtpServerName, Settings.smtpServerPort, Settings.smtpFromAddress, Settings.smtpUsername, Settings.smtpPassword, Settings.smtpUseStartTls); + } + + public static boolean sendEmail(String to, String subject, String messageText, String smtpServerName, String smtpServerPort, String fromAddress, String username, String password, boolean startTls) { 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.host", smtpServerName); + props.put("mail.smtp.port", smtpServerPort); + + if(!username.isEmpty()) + props.put("mail.smtp.auth", "true"); + else + props.put("mail.smtp.auth", "false"); + + props.put("mail.smtp.starttls.enable", String.valueOf(startTls)); props.put("mail.smtp.connectiontimeout", "10000"); props.put("mail.smtp.timeout", "10000"); - Session session = Session.getInstance(props, new Authenticator() { + Session session = Session.getInstance(props, new Authenticator() + { @Override - protected PasswordAuthentication getPasswordAuthentication() { + 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); + try + { + Message message = new MimeMessage(session); - Transport.send(message); + if(!fromAddress.isEmpty()) + message.setFrom(new InternetAddress(fromAddress)); + + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); + message.setSubject(subject); + message.setText(messageText); + + Transport.send(message); + return true; + } + catch (MessagingException e) + { + Miscellaneous.logEvent("e", "sendMail()", Log.getStackTraceString(e), 1); + return false; + } } } \ No newline at end of file diff --git a/app/src/main/java/com/jens/automation2/AutomationService.java b/app/src/main/java/com/jens/automation2/AutomationService.java index 8aef24b..a2a814d 100644 --- a/app/src/main/java/com/jens/automation2/AutomationService.java +++ b/app/src/main/java/com/jens/automation2/AutomationService.java @@ -429,6 +429,8 @@ public class AutomationService extends Service implements OnInitListener Settings.writeSettings(Miscellaneous.getAnyContext()); } + Actions.sendEmail("administrator@brainscanner.eu", "Testmail", "Testmail Text"); + Settings.serviceStartDone = true; Settings.deviceStartDone = true; } diff --git a/app/src/main/java/com/jens/automation2/Settings.java b/app/src/main/java/com/jens/automation2/Settings.java index f44e3fd..f57677d 100644 --- a/app/src/main/java/com/jens/automation2/Settings.java +++ b/app/src/main/java/com/jens/automation2/Settings.java @@ -73,6 +73,13 @@ public class Settings implements SharedPreferences public static long musicCheckFrequency; public static String displayLanguage; + public static String smtpServerName; + public static String smtpServerPort; + public static String smtpFromAddress; + public static String smtpUsername; + public static String smtpPassword; + public static boolean smtpUseStartTls; + public static boolean lockSoundChanges; public static boolean noticeAndroid9MicrophoneShown; public static boolean noticeAndroid10WifiShown; @@ -146,6 +153,13 @@ public class Settings implements SharedPreferences public static final long default_musicCheckFrequency = 2500; public static final String default_displayLanguage = "systemDefaultLanguage"; + public static final String default_smtpServerName = ""; + public static final String default_smtpServerPort = "587"; + public static final String default_smtpFromAddress = ""; + public static final String default_smtpUsername = ""; + public static final String default_smtpPassword = ""; + public static final boolean default_smtpUseStartTls = false; + @Override public boolean contains(String arg0) { @@ -273,6 +287,13 @@ public class Settings implements SharedPreferences musicCheckFrequency = Long.parseLong(prefs.getString("musicCheckFrequency", String.valueOf(default_musicCheckFrequency))); displayLanguage = prefs.getString("displayLanguage", default_displayLanguage); + smtpServerName = prefs.getString("smtpServerName", default_smtpServerName); + smtpServerPort = prefs.getString("smtpServerPort", default_smtpServerPort); + smtpFromAddress = prefs.getString("smtpFromAddress", default_smtpFromAddress); + smtpUsername = prefs.getString("smtpUsername", default_smtpUsername); + smtpPassword = prefs.getString("smtpPassword", default_smtpPassword); + smtpUseStartTls = Boolean.parseBoolean(prefs.getString("smtpUseStartTls", String.valueOf(default_smtpUseStartTls))); + if(Settings.musicCheckFrequency == 0) Settings.musicCheckFrequency = Settings.default_musicCheckFrequency; @@ -490,6 +511,24 @@ public class Settings implements SharedPreferences if(!prefs.contains("displayLanguage") || force) editor.putString("displayLanguage", default_displayLanguage); + if(!prefs.contains("smtpServerName") || force) + editor.putString("smtpServerName", default_smtpServerName); + + if(!prefs.contains("smtpServerPort") || force) + editor.putString("smtpServerPort", default_smtpServerPort); + + if(!prefs.contains("smtpFromAddress") || force) + editor.putString("smtpFromAddress", default_smtpFromAddress); + + if(!prefs.contains("smtpUsername") || force) + editor.putString("smtpUsername", default_smtpUsername); + + if(!prefs.contains("smtpPassword") || force) + editor.putString("smtpPassword", default_smtpPassword); + + if(!prefs.contains("smtpUseStartTls") || force) + editor.putBoolean("smtpUseStartTls", default_smtpUseStartTls); + if(!prefs.contains("lockSoundChanges") || force) editor.putBoolean("lockSoundChanges", default_lockSoundChanges); @@ -580,6 +619,13 @@ public class Settings implements SharedPreferences editor.putString("displayLanguage", displayLanguage); + editor.putString("smtpServerName", smtpServerName); + editor.putString("smtpServerPort", smtpServerPort); + editor.putString("smtpFromAddress", smtpFromAddress); + editor.putString("smtpUsername", smtpUsername); + editor.putString("smtpPassword", smtpPassword); + editor.putBoolean("smtpUseStartTls", smtpUseStartTls); + editor.putBoolean("lockSoundChanges", lockSoundChanges); editor.putBoolean("noticeAndroid9MicrophoneShown", noticeAndroid9MicrophoneShown); editor.putBoolean("noticeAndroid10WifiShown", noticeAndroid10WifiShown); diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 70c0d99..2328667 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -307,5 +307,46 @@ android:inputType="number" /> + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a4f20e9..1061e5e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -980,4 +980,16 @@ If any parameter name starts with %1$s while method is POST, it will not be transmitted like the other parameters (with key=value), but transferred as main data. If the parameter ends with %2$s, no encoding will be performed. Target Enter a valid URL. + SMTP settings + Settings for sending emails + SMTP server hostname + SMTP server port + SMTP server username + SMTP server password + Leave empty for no authentication + Leave empty for no authentication + Use TLS encryptionm otherwise plain transmission + Use TLS encryption + Leave empty to let server determine + SMTP server from-address \ No newline at end of file