diff options
| author | Luke Miller <Luke@LukesDesktop.(none)> | 2010-10-29 19:45:58 +0100 | 
|---|---|---|
| committer | Luke Miller <Luke@LukesDesktop.(none)> | 2010-10-29 19:45:58 +0100 | 
| commit | 2cb24e089b685569e94bbfe037e9649d589672c6 (patch) | |
| tree | 312b15394992b0bee23dd42e071a5cb7af1098c4 | |
| parent | e0299d13f0b5b763afcc1f6bd24c70d2dcc99532 (diff) | |
Added functionality for sending/receiving email. Fixed program hang when monitoring for new email. Added functionality for delivery report via sms.
| -rw-r--r-- | workspace/BlaMail/.classpath | 2 | ||||
| -rw-r--r-- | workspace/BlaMail/bin/BlaMail.apk | bin | 235057 -> 283981 bytes | |||
| -rw-r--r-- | workspace/BlaMail/bin/classes.dex | bin | 476252 -> 567060 bytes | |||
| -rw-r--r-- | workspace/BlaMail/bin/com/blatech/blamail/BlaMail$1.class | bin | 1306 -> 1463 bytes | |||
| -rw-r--r-- | workspace/BlaMail/bin/com/blatech/blamail/BlaMail$2.class | bin | 1348 -> 1366 bytes | |||
| -rw-r--r-- | workspace/BlaMail/bin/com/blatech/blamail/BlaMail$3.class | bin | 1174 -> 2101 bytes | |||
| -rw-r--r-- | workspace/BlaMail/bin/com/blatech/blamail/BlaMail.class | bin | 3849 -> 2892 bytes | |||
| -rw-r--r-- | workspace/BlaMail/bin/resources.ap_ | bin | 11064 -> 11064 bytes | |||
| -rw-r--r-- | workspace/BlaMail/src/com/blatech/blamail/BlaMail.java | 84 | ||||
| -rw-r--r-- | workspace/BlaMail/src/com/blatech/blamail/GMailSender.java | 106 | ||||
| -rw-r--r-- | workspace/BlaMail/src/com/blatech/blamail/JSSEProvider.java | 51 | 
11 files changed, 230 insertions, 13 deletions
diff --git a/workspace/BlaMail/.classpath b/workspace/BlaMail/.classpath index 75d908e..40dad89 100644 --- a/workspace/BlaMail/.classpath +++ b/workspace/BlaMail/.classpath @@ -4,5 +4,7 @@  	<classpathentry kind="src" path="gen"/>  	<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>  	<classpathentry kind="lib" path="/home/Luke/workspace/BlaMail/lib/mail.jar"/> +	<classpathentry kind="lib" path="/home/Luke/workspace/BlaMail/lib/activation.jar"/> +	<classpathentry kind="lib" path="/home/Luke/workspace/BlaMail/lib/additionnal.jar"/>  	<classpathentry kind="output" path="bin"/>  </classpath> diff --git a/workspace/BlaMail/bin/BlaMail.apk b/workspace/BlaMail/bin/BlaMail.apk Binary files differindex 59acad9..1c09fbb 100644 --- a/workspace/BlaMail/bin/BlaMail.apk +++ b/workspace/BlaMail/bin/BlaMail.apk diff --git a/workspace/BlaMail/bin/classes.dex b/workspace/BlaMail/bin/classes.dex Binary files differindex b94b7e2..1a2bed7 100644 --- a/workspace/BlaMail/bin/classes.dex +++ b/workspace/BlaMail/bin/classes.dex diff --git a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$1.class b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$1.class Binary files differindex 310ff58..62e1e4a 100644 --- a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$1.class +++ b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$1.class diff --git a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$2.class b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$2.class Binary files differindex 55ab08d..7b81c02 100644 --- a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$2.class +++ b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$2.class diff --git a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$3.class b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$3.class Binary files differindex c341144..344cb5e 100644 --- a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$3.class +++ b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail$3.class diff --git a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail.class b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail.class Binary files differindex d94243d..10e33ac 100644 --- a/workspace/BlaMail/bin/com/blatech/blamail/BlaMail.class +++ b/workspace/BlaMail/bin/com/blatech/blamail/BlaMail.class diff --git a/workspace/BlaMail/bin/resources.ap_ b/workspace/BlaMail/bin/resources.ap_ Binary files differindex 246f124..f564292 100644 --- a/workspace/BlaMail/bin/resources.ap_ +++ b/workspace/BlaMail/bin/resources.ap_ 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 diff --git a/workspace/BlaMail/src/com/blatech/blamail/GMailSender.java b/workspace/BlaMail/src/com/blatech/blamail/GMailSender.java new file mode 100644 index 0000000..f36d5e1 --- /dev/null +++ b/workspace/BlaMail/src/com/blatech/blamail/GMailSender.java @@ -0,0 +1,106 @@ +package com.blatech.blamail; + +import javax.activation.DataHandler;    +import javax.activation.DataSource;   +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.Security; +import java.util.Properties; + +import javax.mail.Message; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; + +public class GMailSender extends javax.mail.Authenticator {    +    private String mailhost = "smtp.gmail.com";    +    private String user;    +    private String password;    +    private Session session;    + +    static {    +        Security.addProvider(new JSSEProvider());    +    }   + +    public GMailSender(String user, String password) {    +        this.user = user;    +        this.password = password;    + +        Properties props = new Properties();    +        props.setProperty("mail.transport.protocol", "smtp");    +        props.setProperty("mail.host", mailhost);    +        props.put("mail.smtp.auth", "true");    +        props.put("mail.smtp.port", "465");    +        props.put("mail.smtp.socketFactory.port", "465");    +        props.put("mail.smtp.socketFactory.class",    +                "javax.net.ssl.SSLSocketFactory");    +        props.put("mail.smtp.socketFactory.fallback", "false");    +        props.setProperty("mail.smtp.quitwait", "false");    + +        session = Session.getDefaultInstance(props, this);    +    }    + +    protected PasswordAuthentication getPasswordAuthentication() {    +        return new PasswordAuthentication(user, password);    +    }    + +    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {    +        try{ +        MimeMessage message = new MimeMessage(session);    +        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));    +        message.setSender(new InternetAddress(sender));    +        message.setSubject(subject);    +        message.setDataHandler(handler);    +        if (recipients.indexOf(',') > 0)    +            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));    +        else   +            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));    +        Transport.send(message);    +        }catch(Exception e){ + +        } +    }    + +    public class ByteArrayDataSource implements DataSource {    +        private byte[] data;    +        private String type;    + +        public ByteArrayDataSource(byte[] data, String type) {    +            super();    +            this.data = data;    +            this.type = type;    +        }    + +        public ByteArrayDataSource(byte[] data) {    +            super();    +            this.data = data;    +        }    + +        public void setType(String type) {    +            this.type = type;    +        }    + +        public String getContentType() {    +            if (type == null)    +                return "application/octet-stream";    +            else   +                return type;    +        }    + +        public InputStream getInputStream() throws IOException {    +            return new ByteArrayInputStream(data);    +        }    + +        public String getName() {    +            return "ByteArrayDataSource";    +        }    + +        public OutputStream getOutputStream() throws IOException {    +            throw new IOException("Not Supported");    +        }    +    }    +}   diff --git a/workspace/BlaMail/src/com/blatech/blamail/JSSEProvider.java b/workspace/BlaMail/src/com/blatech/blamail/JSSEProvider.java new file mode 100644 index 0000000..2fe1b37 --- /dev/null +++ b/workspace/BlaMail/src/com/blatech/blamail/JSSEProvider.java @@ -0,0 +1,51 @@ +package com.blatech.blamail; + +/* + *  Licensed to the Apache Software Foundation (ASF) under one or more + *  contributor license agreements.  See the NOTICE file distributed with + *  this work for additional information regarding copyright ownership. + *  The ASF licenses this file to You under the Apache License, Version 2.0 + *  (the "License"); you may not use this file except in compliance with + *  the License.  You may obtain a copy of the License at + * + *     http://www.apache.org/licenses/LICENSE-2.0 + * + *  Unless required by applicable law or agreed to in writing, software + *  distributed under the License is distributed on an "AS IS" BASIS, + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + *  See the License for the specific language governing permissions and + *  limitations under the License. + */ + +/** + * @author Alexander Y. Kleymenov + * @version $Revision$ + */ + + +import java.security.AccessController; +import java.security.Provider; + +public final class JSSEProvider extends Provider { + +    /** +	 *  +	 */ +	private static final long serialVersionUID = 1L; + +	public JSSEProvider() { +        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider"); +        AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() { +            public Void run() { +                put("SSLContext.TLS", +                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl"); +                put("Alg.Alias.SSLContext.TLSv1", "TLS"); +                put("KeyManagerFactory.X509", +                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl"); +                put("TrustManagerFactory.X509", +                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl"); +                return null; +            } +        }); +    } +}  | 
