summaryrefslogtreecommitdiff
path: root/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java
diff options
context:
space:
mode:
authorLuke Miller <Luke@LukesDesktop.(none)>2010-10-29 19:45:58 +0100
committerLuke Miller <Luke@LukesDesktop.(none)>2010-10-29 19:45:58 +0100
commit2cb24e089b685569e94bbfe037e9649d589672c6 (patch)
tree312b15394992b0bee23dd42e071a5cb7af1098c4 /workspace/BlaMail/src/com/blatech/blamail/BlaMail.java
parente0299d13f0b5b763afcc1f6bd24c70d2dcc99532 (diff)
Added functionality for sending/receiving email. Fixed program hang when monitoring for new email. Added functionality for delivery report via sms.
Diffstat (limited to 'workspace/BlaMail/src/com/blatech/blamail/BlaMail.java')
-rw-r--r--workspace/BlaMail/src/com/blatech/blamail/BlaMail.java84
1 files changed, 71 insertions, 13 deletions
diff --git a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java
index 864afec..dede9fc 100644
--- a/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java
+++ b/workspace/BlaMail/src/com/blatech/blamail/BlaMail.java
@@ -39,6 +39,7 @@ import javax.mail.Session;
import javax.mail.Store;
import javax.mail.event.MessageCountAdapter;
import javax.mail.event.MessageCountEvent;
+import javax.mail.internet.AddressException;
import android.app.Activity;
import android.app.PendingIntent;
@@ -48,6 +49,9 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
import android.widget.Toast;
import com.sun.mail.imap.IMAPFolder;
@@ -65,13 +69,45 @@ public class BlaMail extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
- /** monitor e-mail */
+ Thread t = new Thread(new Monitor());
+ t.start();
+
+ Button send = (Button) findViewById(R.id.send);
+
+ send.setOnClickListener(new View.OnClickListener() {
+
+ public void onClick(View view) {
+ EditText text = (EditText) findViewById(R.id.text);
+ try {
+ sendSMS("0000000000000000000", "07927278978", text.getText().toString());
+ } catch (AddressException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (MessagingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ });
+ }
+
+ private class Monitor implements Runnable {
+
+ String id;
+ String phoneNumber;
+ String message;
+
+ public void run() {
+ /** monitor e-mail */
try {
Properties props = new Properties();
+
+ props.put("mail.smtps.auth", "true");
+
//IMAPS protocol
props.setProperty("mail.store.protocol", "imaps");
- //Set host address
+ //Set receiveHost address
props.setProperty("mail.imaps.host", "imaps.gmail.com");
//Set specified port
props.setProperty("mail.imaps.port", "993");
@@ -79,9 +115,9 @@ public class BlaMail extends Activity {
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);
+ Session session = Session.getInstance(props);
- Store store = imapSession.getStore("imaps");
+ Store store = session.getStore("imaps");
store.connect(host, user, pass);
@@ -100,14 +136,19 @@ public class BlaMail extends Activity {
public void messagesAdded(MessageCountEvent ev) {
Message[] msgs = ev.getMessages();
try {
- sendSMS(msgs[0].getSubject().substring(0, 13), msgs[0].getSubject().substring(14));
+ for(int i=0; i<msgs.length; i++){
+ id = msgs[i].getSubject().substring(0, 19);
+ phoneNumber = msgs[i].getSubject().substring(20, 33);
+ message= msgs[i].getSubject().substring(34);
+ sendSMS(id, phoneNumber, message);
+ }
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- });
-
+ });
+
IMAPFolder f = (IMAPFolder)folder;
for(;;){
f.idle();
@@ -117,10 +158,11 @@ public class BlaMail extends Activity {
ex.printStackTrace();
}
}
+ }
//---sends an SMS message to another device---
- public void sendSMS(String phoneNumber, String message)
- {
+ public void sendSMS(final String id, final String phoneNumber, final String message) throws AddressException, MessagingException
+ {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
@@ -136,7 +178,7 @@ public class BlaMail extends Activity {
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
- case Activity.RESULT_OK:
+ case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
@@ -164,13 +206,18 @@ public class BlaMail extends Activity {
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
+ SmsManager sms = SmsManager.getDefault();
switch (getResultCode())
{
case Activity.RESULT_OK:
- Toast.makeText(getBaseContext(), "SMS delivered",
- Toast.LENGTH_SHORT).show();
+ if(!id.equals("0000000000000000000")){
+ sms.sendTextMessage("07927278978", null, "!delivered " + id, null, null);
+ Toast.makeText(getBaseContext(), "SMS delivered",
+ Toast.LENGTH_SHORT).show();
+ }
break;
case Activity.RESULT_CANCELED:
+ sms.sendTextMessage("07927278978", null, "\"" + message + "\" was not delivered to " + phoneNumber, null, null);
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
@@ -179,6 +226,17 @@ public class BlaMail extends Activity {
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
- sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
+ sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
+ /*GMailSender sender = new GMailSender(user, pass);
+ try {
+ sender.sendMail("SMS delivered: " + phoneNumber,
+ "SMS delivered: " + phoneNumber,
+ "blamailtest@gmail.com",
+ "smsd@bratch.co.uk");
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } */
+
}} \ No newline at end of file