send emails
This commit is contained in:
1 parent
72040b54ae
commit
41cfeb43fc
4 files changed
+159
-3
No files matched your search
+1
-1
@@ -77,7 +77,7 @@ android {
|
||||
implementation 'com.linkedin.dexmaker:dexmaker:2.28.6'
|
||||
implementation 'org.apache.commons:commons-lang3:3.20.0'
|
||||
|
||||
implementation 'org.simplejavamail:simple-java-mail:9.3.5'
|
||||
implementation 'com.sun.mail:android-mail:1.6.4'
|
||||
|
||||
//implementation "androidx.security:security-crypto:1.0.0"
|
||||
//implementation "androidx.security:security-identity-credential:1.0.0-alpha02"
|
||||
|
||||
@@ -2675,7 +2675,7 @@ public class Actions
|
||||
android.provider.Settings.Secure.putString(context.getContentResolver(), android.provider.Settings.Secure.LOCATION_MODE, String.valueOf(desiredState));
|
||||
}
|
||||
|
||||
public static sendEmail()
|
||||
/*public static sendEmail()
|
||||
{
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.host", smtpHost);
|
||||
@@ -2702,5 +2702,5 @@ public class Actions
|
||||
message.setText(body);
|
||||
|
||||
Transport.send(message);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.IBinder;
|
||||
import android.os.StrictMode;
|
||||
import android.speech.tts.TextToSpeech;
|
||||
import android.speech.tts.TextToSpeech.OnInitListener;
|
||||
import android.util.Log;
|
||||
@@ -36,11 +37,20 @@ import com.jens.automation2.receivers.DateTimeListener;
|
||||
import com.jens.automation2.receivers.PackageReplacedReceiver;
|
||||
import com.jens.automation2.receivers.PhoneStatusListener;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
||||
@SuppressLint("NewApi")
|
||||
public class AutomationService extends Service implements OnInitListener
|
||||
{
|
||||
@@ -429,6 +439,25 @@ public class AutomationService extends Service implements OnInitListener
|
||||
Settings.writeSettings(Miscellaneous.getAnyContext());
|
||||
}
|
||||
|
||||
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
|
||||
EmailService es = new EmailService(Settings.smtpServerName, 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);
|
||||
}
|
||||
catch (AddressException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
catch (MessagingException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
Settings.serviceStartDone = true;
|
||||
Settings.deviceStartDone = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.jens.automation2;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.Authenticator;
|
||||
import javax.mail.Message;
|
||||
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;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
|
||||
public class EmailService
|
||||
{
|
||||
private final String server;
|
||||
private final int port;
|
||||
|
||||
public static final String CLIENT_NAME = "Android StackOverflow programmatic email";
|
||||
|
||||
public EmailService(String server, int port)
|
||||
{
|
||||
this.server = server;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public static class Email
|
||||
{
|
||||
private final 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)
|
||||
{
|
||||
this.auth = auth;
|
||||
this.toList = toList;
|
||||
this.from = from;
|
||||
this.subject = subject;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public Authenticator getAuth()
|
||||
{
|
||||
return auth;
|
||||
}
|
||||
|
||||
public List<InternetAddress> getToList()
|
||||
{
|
||||
return toList;
|
||||
}
|
||||
|
||||
public Address getFrom()
|
||||
{
|
||||
return from;
|
||||
}
|
||||
|
||||
public String getSubject()
|
||||
{
|
||||
return subject;
|
||||
}
|
||||
|
||||
public String getBody()
|
||||
{
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
public static class UserPassAuthenticator extends Authenticator
|
||||
{
|
||||
private final String username;
|
||||
private final String password;
|
||||
|
||||
public UserPassAuthenticator(String username, String password)
|
||||
{
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication()
|
||||
{
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
public void send(Email email) throws MessagingException
|
||||
{
|
||||
Properties props = new Properties();
|
||||
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);
|
||||
props.put("mail.mime.charset", "UTF-8");
|
||||
|
||||
Session session = Session.getDefaultInstance(props, email.getAuth());
|
||||
MimeMessage msg = new MimeMessage(session);
|
||||
|
||||
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]));
|
||||
|
||||
msg.setReplyTo(new Address[]{email.getFrom()});
|
||||
|
||||
msg.addHeader("X-Mailer", CLIENT_NAME);
|
||||
msg.addHeader("Precedence", "bulk");
|
||||
msg.setSubject(email.getSubject());
|
||||
|
||||
MimeMultipart multipart = new MimeMultipart();
|
||||
MimeBodyPart bodyPart = new MimeBodyPart();
|
||||
bodyPart.setText(email.getBody(), "iso-8859-1");
|
||||
multipart.addBodyPart(bodyPart);
|
||||
|
||||
msg.setContent(multipart);
|
||||
Transport.send(msg);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user