Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. long currentTime = System.currentTimeMillis();
  2. long pastThreeHour = currentTime - (AlarmManager.INTERVAL_HOUR * 3);
  3. String[] selectionArgs = { "" + pastThreeHour, "" + currentTime };
  4.  
  5. Cursor cursor = contentResolver.query(uri, null, selection, selectionArgs, "date DESC");
  6.  
  7. if (cursor != null && cursor.getCount() > 0) {
  8. while (cursor.moveToNext()) {
  9.  
  10. String number = cursor.getString(cursor.getColumnIndex("address")); // check for null
  11. String date = cursor.getString(cursor.getColumnIndex("date")); // convert to date its long
  12. String message_text = cursor.getString(cursor.getColumnIndex("body"));
  13. String type = cursor.getString(cursor.getColumnIndex("type")); // check type and get names
  14.  
  15. // send email from here
  16. sendSMSEmail(number, date, message_text, type);
  17. }
  18. }
  19. cursor.close();
  20. }
  21.  
  22. public class GMailSender extends javax.mail.Authenticator
  23. {
  24. private String mailhost = "smtp.gmail.com";
  25. private String user;
  26. private String password;
  27. private Session session;
  28. private Multipart _multipart;
  29.  
  30. static
  31. {
  32. Security.addProvider(new JSSEProvider());
  33. }
  34.  
  35. public GMailSender(String user, String password)
  36. {
  37. this.user = user;
  38. this.password = password;
  39.  
  40. Properties props = new Properties();
  41. props.setProperty("mail.transport.protocol", "smtp");
  42. props.setProperty("mail.host", mailhost);
  43. props.put("mail.smtp.auth", "true");
  44. props.put("mail.smtp.port", "465");
  45. props.put("mail.smtp.socketFactory.port", "465");
  46. props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
  47. props.put("mail.smtp.socketFactory.fallback", "false");
  48. props.setProperty("mail.smtp.quitwait", "false");
  49.  
  50. session = Session.getDefaultInstance(props, this);
  51. _multipart = new MimeMultipart();
  52. }
  53.  
  54. protected PasswordAuthentication getPasswordAuthentication()
  55. {
  56. return new PasswordAuthentication(user, password);
  57. }
  58.  
  59. public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception
  60. {
  61. MimeMessage message = new MimeMessage(session);
  62. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  63. message.setSender(new InternetAddress(sender));
  64. message.setSubject(subject);
  65.  
  66. message.setDataHandler(handler);
  67. message.setContent(_multipart);
  68. if (recipients.indexOf(',') > 0)
  69. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  70. else
  71. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  72. Transport.send(message);
  73.  
  74. }
  75.  
  76. public void addAttachment(String filename) throws Exception
  77. {
  78. BodyPart messageBodyPart = new MimeBodyPart();
  79. DataSource source = new FileDataSource(filename);
  80. messageBodyPart.setDataHandler(new DataHandler(source));
  81. messageBodyPart.setFileName(filename);
  82.  
  83. _multipart.addBodyPart(messageBodyPart);
  84. }
  85.  
  86. public class ByteArrayDataSource implements DataSource
  87. {
  88. private byte[] data;
  89. private String type;
  90.  
  91. public ByteArrayDataSource(byte[] data, String type)
  92. {
  93. super();
  94. this.data = data;
  95. this.type = type;
  96. }
  97.  
  98. public ByteArrayDataSource(byte[] data)
  99. {
  100. super();
  101. this.data = data;
  102. }
  103.  
  104. public void setType(String type)
  105. {
  106. this.type = type;
  107. }
  108.  
  109. public String getContentType()
  110. {
  111. if (type == null)
  112. return "application/octet-stream";
  113. else
  114. return type;
  115. }
  116.  
  117. public InputStream getInputStream() throws IOException
  118. {
  119. return new ByteArrayInputStream(data);
  120. }
  121.  
  122. public String getName()
  123. {
  124. return "ByteArrayDataSource";
  125. }
  126.  
  127. public OutputStream getOutputStream() throws IOException
  128. {
  129. throw new IOException("Not Supported");
  130. }
  131. }
  132. }
  133.  
  134. public final class JSSEProvider extends Provider
  135. {
  136. private static final long serialVersionUID = 1L;
  137.  
  138. public JSSEProvider()
  139. {
  140. super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
  141. AccessController.doPrivileged(new java.security.PrivilegedAction<Void>()
  142. {
  143. public Void run()
  144. {
  145. put("SSLContext.TLS",
  146. "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
  147. put("Alg.Alias.SSLContext.TLSv1", "TLS");
  148. put("KeyManagerFactory.X509",
  149. "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
  150. put("TrustManagerFactory.X509",
  151. "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
  152. return null;
  153. }
  154. });
  155. }
  156. }
  157.  
  158. BackgroundMail bm = new BackgroundMail(context);
  159. bm.setGmailUserName("sendername@gmail.com");
  160. bm.setGmailPassword("sender_email_password");
  161. bm.setMailTo("receiver@gmail.com");
  162. bm.setFormSubject("Subject");
  163. bm.setFormBody("Body");
  164. bm.send();
  165.  
  166. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  167. <uses-permission android:name="android.permission.INTERNET"/>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement