diff options
author | Luke Miller <Luke@LukesDesktop.(none)> | 2010-10-29 01:44:57 +0100 |
---|---|---|
committer | Luke Miller <Luke@LukesDesktop.(none)> | 2010-10-29 01:44:57 +0100 |
commit | e0299d13f0b5b763afcc1f6bd24c70d2dcc99532 (patch) | |
tree | 0ea3858b6a79fba86a8cfa1a673f6f510188648b /workspace/BlaMail/src |
blamail
Diffstat (limited to 'workspace/BlaMail/src')
-rw-r--r-- | workspace/BlaMail/src/com/blatech/blamail/BlaMail.java | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java new file mode 100644 index 0000000..864afec --- /dev/null +++ b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java @@ -0,0 +1,184 @@ +/* Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Sun Microsystems nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package com.blatech.blamail; + +import java.util.Properties; + +import javax.mail.Folder; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Store; +import javax.mail.event.MessageCountAdapter; +import javax.mail.event.MessageCountEvent; + +import android.app.Activity; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; +import android.telephony.SmsManager; +import android.widget.Toast; + +import com.sun.mail.imap.IMAPFolder; + +public class BlaMail extends Activity { + + String host = "imap.gmail.com"; + String user = "blamailtest@gmail.com"; + String pass = "thankssueaddo"; + String mbox = "Inbox"; + + /** Called when the activity is first created. */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.main); + + /** monitor e-mail */ + try { + Properties props = new Properties(); + //IMAPS protocol + props.setProperty("mail.store.protocol", "imaps"); + + //Set host address + props.setProperty("mail.imaps.host", "imaps.gmail.com"); + //Set specified port + props.setProperty("mail.imaps.port", "993"); + //Using SSL + props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); + props.setProperty("mail.imaps.socketFactory.fallback", "false"); + //Setting IMAP session + Session imapSession = Session.getInstance(props); + + Store store = imapSession.getStore("imaps"); + + store.connect(host, user, pass); + + //Get all mails in Inbox Forlder + Folder folder = store.getFolder(mbox); + if (folder == null || !folder.exists()) { + //System.out.println("Invalid folder"); + Toast.makeText(getBaseContext(), "Invalid Folder", + Toast.LENGTH_SHORT).show(); + System.exit(1); + } + folder.open(Folder.READ_ONLY); + + // Add messageCountListener to listen for new messages + folder.addMessageCountListener(new MessageCountAdapter() { + public void messagesAdded(MessageCountEvent ev) { + Message[] msgs = ev.getMessages(); + try { + sendSMS(msgs[0].getSubject().substring(0, 13), msgs[0].getSubject().substring(14)); + } catch (MessagingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); + + IMAPFolder f = (IMAPFolder)folder; + for(;;){ + f.idle(); + } + + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + //---sends an SMS message to another device--- + public void sendSMS(String phoneNumber, String message) + { + String SENT = "SMS_SENT"; + String DELIVERED = "SMS_DELIVERED"; + + PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, + new Intent(SENT), 0); + + PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, + new Intent(DELIVERED), 0); + + //---when the SMS has been sent--- + registerReceiver(new BroadcastReceiver(){ + @Override + public void onReceive(Context arg0, Intent arg1) { + switch (getResultCode()) + { + case Activity.RESULT_OK: + Toast.makeText(getBaseContext(), "SMS sent", + Toast.LENGTH_SHORT).show(); + break; + case SmsManager.RESULT_ERROR_GENERIC_FAILURE: + Toast.makeText(getBaseContext(), "Generic failure", + Toast.LENGTH_SHORT).show(); + break; + case SmsManager.RESULT_ERROR_NO_SERVICE: + Toast.makeText(getBaseContext(), "No service", + Toast.LENGTH_SHORT).show(); + break; + case SmsManager.RESULT_ERROR_NULL_PDU: + Toast.makeText(getBaseContext(), "Null PDU", + Toast.LENGTH_SHORT).show(); + break; + case SmsManager.RESULT_ERROR_RADIO_OFF: + Toast.makeText(getBaseContext(), "Radio off", + Toast.LENGTH_SHORT).show(); + break; + } + } + }, new IntentFilter(SENT)); + + //---when the SMS has been delivered--- + registerReceiver(new BroadcastReceiver(){ + @Override + public void onReceive(Context arg0, Intent arg1) { + switch (getResultCode()) + { + case Activity.RESULT_OK: + Toast.makeText(getBaseContext(), "SMS delivered", + Toast.LENGTH_SHORT).show(); + break; + case Activity.RESULT_CANCELED: + Toast.makeText(getBaseContext(), "SMS not delivered", + Toast.LENGTH_SHORT).show(); + break; + } + } + }, new IntentFilter(DELIVERED)); + + SmsManager sms = SmsManager.getDefault(); + sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); + +}}
\ No newline at end of file |