send emails
This commit is contained in:
1 parent
72040b54ae
commit
ecf76380cc
6 files changed
+156
-17
No files matched your search
+7
-1
@@ -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'
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -307,5 +307,46 @@
|
||||
android:inputType="number" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory
|
||||
android:summary="@string/smtpSettingsSummary"
|
||||
android:title="@string/smtpSettingsTitle">
|
||||
|
||||
<EditTextPreference
|
||||
android:key="smtpServerName"
|
||||
android:summary="@string/musicCheckFrequencySummary"
|
||||
android:title="@string/smtpServerName"
|
||||
android:inputType="text" />
|
||||
|
||||
<EditTextPreference
|
||||
android:key="smtpServerPort"
|
||||
android:summary="@string/musicCheckFrequencySummary"
|
||||
android:title="@string/smtpServerPort"
|
||||
android:inputType="number" />
|
||||
|
||||
<EditTextPreference
|
||||
android:key="smtpFromAddress"
|
||||
android:summary="@string/smtpServerFromAddressSummary"
|
||||
android:title="@string/smtpServerFromAddress"
|
||||
android:inputType="text" />
|
||||
|
||||
<EditTextPreference
|
||||
android:key="smtpUsername"
|
||||
android:summary="@string/smtpServerUsernameSummary"
|
||||
android:title="@string/smtpServerUsername"
|
||||
android:inputType="text" />
|
||||
|
||||
<EditTextPreference
|
||||
android:key="smtpPassword"
|
||||
android:summary="@string/smtpServerPasswordSummary"
|
||||
android:title="@string/smtpServerPassword"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
<CheckBoxPreference
|
||||
android:key="smtpUseStartTls"
|
||||
android:summary="@string/smtpServerUseTlsSummary"
|
||||
android:title="@string/smtpServerUseTls" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
||||
@@ -980,4 +980,16 @@
|
||||
<string name="httpParametersExplanation">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.</string>
|
||||
<string name="target">Target</string>
|
||||
<string name="enter_valid_url">Enter a valid URL.</string>
|
||||
<string name="smtpSettingsTitle">SMTP settings</string>
|
||||
<string name="smtpSettingsSummary">Settings for sending emails</string>
|
||||
<string name="smtpServerName">SMTP server hostname</string>
|
||||
<string name="smtpServerPort">SMTP server port</string>
|
||||
<string name="smtpServerUsername">SMTP server username</string>
|
||||
<string name="smtpServerPassword">SMTP server password</string>
|
||||
<string name="smtpServerUsernameSummary">Leave empty for no authentication</string>
|
||||
<string name="smtpServerPasswordSummary">Leave empty for no authentication</string>
|
||||
<string name="smtpServerUseTlsSummary">Use TLS encryptionm otherwise plain transmission</string>
|
||||
<string name="smtpServerUseTls">Use TLS encryption</string>
|
||||
<string name="smtpServerFromAddressSummary">Leave empty to let server determine</string>
|
||||
<string name="smtpServerFromAddress">SMTP server from-address</string>
|
||||
</resources>
|
||||
Reference in new issue
Block a user