From 2cb24e089b685569e94bbfe037e9649d589672c6 Mon Sep 17 00:00:00 2001 From: Luke Miller Date: Fri, 29 Oct 2010 19:45:58 +0100 Subject: Added functionality for sending/receiving email. Fixed program hang when monitoring for new email. Added functionality for delivery report via sms. --- .../BlaMail/src/com/blatech/blamail/BlaMail.java | 84 +++++++++++++--- .../src/com/blatech/blamail/GMailSender.java | 106 +++++++++++++++++++++ .../src/com/blatech/blamail/JSSEProvider.java | 51 ++++++++++ 3 files changed, 228 insertions(+), 13 deletions(-) create mode 100644 workspace/BlaMail/src/com/blatech/blamail/GMailSender.java create mode 100644 workspace/BlaMail/src/com/blatech/blamail/JSSEProvider.java (limited to 'workspace/BlaMail/src/com/blatech') 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 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() { + 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; + } + }); + } +} -- cgit v1.2.3