Guest User

Untitled

a guest
Oct 30th, 2017
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.62 KB | None | 0 0
  1. public void generateExcelAndSendEmail(){
  2. Map<String,ArrayList> map = new HashMap<>();
  3. ArrayList<String> StudentID = new ArrayList<String>();
  4. ArrayList<String> Subject = new ArrayList<>();
  5. ArrayList<String> conducted = new ArrayList<String>();
  6. ArrayList<String> totalattended = new ArrayList<>();
  7. ArrayList<String> percentage = new ArrayList<String>();
  8. ArrayList<String> TeacherID = new ArrayList<>();
  9. StudentID = getIntent().getStringArrayListExtra("StudentID");
  10. Subject = getIntent().getStringArrayListExtra("Subject");
  11. conducted = getIntent().getStringArrayListExtra("conducted");
  12. totalattended = getIntent().getStringArrayListExtra("totalattended");
  13. percentage = getIntent().getStringArrayListExtra("percentage");
  14. TeacherID = getIntent().getStringArrayListExtra("TeacherID");
  15.  
  16. File sd = Environment.getExternalStorageDirectory();
  17. final String xlfile = "ExportedData.xls";
  18.  
  19. File directory = new File(sd.getAbsolutePath());
  20. //create directory if not exist
  21. if (!directory.isDirectory()) {
  22. directory.mkdirs();
  23. }
  24. File file = new File(directory, xlfile);
  25. try {
  26.  
  27. //file path
  28.  
  29. WorkbookSettings wbSettings = new WorkbookSettings();
  30. wbSettings.setLocale(new Locale("en", "EN"));
  31. WritableWorkbook workbook;
  32. workbook = Workbook.createWorkbook(file, wbSettings);
  33. //Excel sheet name. 0 represents first sheet
  34. WritableSheet sheet = workbook.createSheet("Attendance", 0);
  35. // column and row
  36. sheet.addCell(new Label(0, 0, "UserName"));
  37. sheet.addCell(new Label(1, 0, "PhoneNumber"));
  38.  
  39.  
  40. Set<String> studs = new HashSet<>(StudentID);
  41. for(int i=0;i<studs.size();i++){
  42.  
  43. for(int j=0;j<StudentID.size();j++){
  44.  
  45. }
  46. }
  47.  
  48. workbook.write();
  49. workbook.close();
  50.  
  51.  
  52. } catch(Exception e){
  53. e.printStackTrace();
  54. }
  55.  
  56.  
  57. try {
  58. final String[] attach=new String[1];
  59. attach[0]=file.getAbsolutePath();
  60. Thread t = new Thread(new Runnable() {
  61. @Override
  62. public void run() {
  63. try {
  64.  
  65. GMailSender sender = new GMailSender("myemail@gmail.com", "mypassword");
  66. sender.sendMail("This is Subject",
  67. "This is Body",
  68. "someEmail@gmail.com",
  69. "someEmail@gmail.com");
  70.  
  71. } catch (Exception e) {
  72. Log.d("email error",e.getMessage());
  73. e.printStackTrace();
  74. }
  75. }
  76. });
  77.  
  78. } catch (Exception e) {
  79. Log.d("email error",e.getMessage());
  80. e.printStackTrace();
  81. }
  82.  
  83. // Toast.makeText(getApplicationContext(),"Email sent successfully",Toast.LENGTH_LONG).show();
  84. }
  85.  
  86. import android.util.Log;
  87. import java.io.ByteArrayInputStream;
  88. import java.io.IOException;
  89. import java.io.InputStream;
  90. import java.io.OutputStream;
  91. import java.security.Security;
  92. import java.util.Properties;
  93.  
  94. import javax.activation.DataHandler;
  95. import javax.activation.DataSource;
  96. import javax.mail.Message;
  97. import javax.mail.PasswordAuthentication;
  98. import javax.mail.Session;
  99. import javax.mail.Transport;
  100. import javax.mail.internet.InternetAddress;
  101. import javax.mail.internet.MimeMessage;
  102.  
  103. public class GMailSender extends javax.mail.Authenticator {
  104. private String mailhost = "smtp.gmail.com";
  105. private String user;
  106. private String password;
  107. private Session session;
  108.  
  109. static {
  110. Security.addProvider(new com.alleviate.attendence.emailHelper.JSSEProvider());
  111. }
  112.  
  113. public GMailSender(String user, String password) {
  114. this.user = user;
  115. this.password = password;
  116.  
  117. Properties props = new Properties();
  118. props.setProperty("mail.transport.protocol", "smtp");
  119. props.setProperty("mail.host", mailhost);
  120. props.put("mail.smtp.auth", "true");
  121. props.put("mail.smtp.port", "465");
  122. props.put("mail.smtp.socketFactory.port", "465");
  123. props.put("mail.smtp.socketFactory.class",
  124. "javax.net.ssl.SSLSocketFactory");
  125. props.put("mail.smtp.socketFactory.fallback", "false");
  126. props.setProperty("mail.smtp.quitwait", "false");
  127.  
  128. session = Session.getDefaultInstance(props, this);
  129. }
  130.  
  131. protected PasswordAuthentication getPasswordAuthentication() {
  132. return new PasswordAuthentication(user, password);
  133. }
  134.  
  135. public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {
  136. try{
  137. MimeMessage message = new MimeMessage(session);
  138. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  139. message.setSender(new InternetAddress(sender));
  140. message.setSubject(subject);
  141. message.setDataHandler(handler);
  142. if (recipients.indexOf(',') > 0)
  143. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  144. else
  145. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  146. Transport.send(message);
  147. }catch(Exception e){
  148. Log.d("email error",e.getMessage());
  149. }
  150. }
  151.  
  152. public class ByteArrayDataSource implements DataSource {
  153. private byte[] data;
  154. private String type;
  155.  
  156. public ByteArrayDataSource(byte[] data, String type) {
  157. super();
  158. this.data = data;
  159. this.type = type;
  160. }
  161.  
  162. public ByteArrayDataSource(byte[] data) {
  163. super();
  164. this.data = data;
  165. }
  166.  
  167. public void setType(String type) {
  168. this.type = type;
  169. }
  170.  
  171. public String getContentType() {
  172. if (type == null)
  173. return "application/octet-stream";
  174. else
  175. return type;
  176. }
  177.  
  178. public InputStream getInputStream() throws IOException {
  179. return new ByteArrayInputStream(data);
  180. }
  181.  
  182. public String getName() {
  183. return "ByteArrayDataSource";
  184. }
  185.  
  186. public OutputStream getOutputStream() throws IOException {
  187. throw new IOException("Not Supported");
  188. }
  189. }
  190. }
  191.  
  192. import java.security.AccessController;
  193. import java.security.Provider;
  194.  
  195. public final class JSSEProvider extends Provider {
  196.  
  197. public JSSEProvider() {
  198. super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
  199. AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
  200. public Void run() {
  201. put("SSLContext.TLS",
  202. "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
  203. put("Alg.Alias.SSLContext.TLSv1", "TLS");
  204. put("KeyManagerFactory.X509",
  205. "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
  206. put("TrustManagerFactory.X509",
  207. "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
  208. return null;
  209. }
  210. });
  211. }
  212. }
Add Comment
Please, Sign In to add comment