Back to project page info-mailer.
The source code is released under:
MIT License
If you think the Android project info-mailer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package hu.thewolf.infomailer; // w w w .j av a 2 s. c o m import android.app.IntentService; import android.content.Intent; import android.util.Log; import android.util.Pair; public class NotifierService extends IntentService { public static final String SUBJECT = "SUBJECT"; public static final String BODY = "BODY"; public static final String MAIL_USER_NAME = "example@gmail.com"; //change here public static final String MAIL_USER_PASS = "ex4mple"; //change here public static final String MAIL_FROM_ALIAS = "Ex Ample"; //change here public static final String MAIL_TO_EMAIL = "example@example.com"; //change here private static Pair<String, String> prevMail = null; private static final Object LOCK = new Object(); /** * A constructor is required, and must call the super IntentService(String) * constructor with a name for the worker thread. */ public NotifierService() { super("NotifierService"); } /** * The IntentService calls this method from the default worker thread with * the intent that started the service. When this method returns, * IntentService stops the service, as appropriate. */ @Override protected void onHandleIntent(Intent intent) { final String subject = intent.getStringExtra(SUBJECT); final String body = intent.getStringExtra(BODY); synchronized (LOCK) { Pair<String, String> mailData = new Pair<String, String>(subject, body); try { if (!mailData.equals(prevMail)) { sendEmail(MAIL_TO_EMAIL, subject, body); prevMail = mailData; } } catch (Exception e) { Log.e("E-MAIL SENDING", "FAILED", e); e.printStackTrace(); } } } public static void sendEmail(String to, String subject, String message) throws Exception { Mail mail = new Mail(MAIL_USER_NAME, MAIL_USER_PASS); mail.setFromAlias(MAIL_FROM_ALIAS); if (subject != null && subject.length() > 0) { mail.setSubject(subject); } else { mail.setSubject("Error: no subject!"); } if (message != null && message.length() > 0) { mail.setBody(message); } else { mail.setBody("Error: no body!"); } mail.setTo(new String[] { to }); // if (attachements != null) { // for (String attachement : attachements) { // mail.addAttachment(attachement); // } // } if (mail.send()) { Log.i("E-MAIL SENDING", "SUCCESSFUL"); } else { Log.w("E-MAIL SENDING", "NOT SUCCESSFUL"); } } }