Guest User

Untitled

a guest
Mar 30th, 2018
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. package com.example.jawa.pos;
  2.  
  3. import android.app.Activity;
  4. import android.database.Cursor;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.os.AsyncTask;
  7. import android.os.Bundle;
  8. import android.os.Environment;
  9. import android.os.StrictMode;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.Toast;
  14.  
  15. import java.io.File;
  16. import java.io.FileWriter;
  17.  
  18. import au.com.bytecode.opencsv.CSVWriter;
  19.  
  20. /**
  21. * Created by jawa on 10/22/2015.
  22. */
  23. public class DailyReport_CSV_file extends Activity {
  24. Button csv;
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. GMailSender sender = new GMailSender("username@gmail.com", "password");
  29. sender.sendMail("Cold Store", "Daily Report",
  30. "user@gmail.com",
  31. "user@yahoo.com");
  32. // sender.addAttachment("csvcash.csv","Daily Report");
  33. Toast.makeText(DailyReport_CSV_file.this, "Mail Send Successfully", Toast.LENGTH_SHORT).show();
  34. finish();
  35. }
  36. }
  37.  
  38. package com.example.jawa.pos;
  39.  
  40. /**
  41. * Created by jawa on 10/23/2015.
  42. */
  43. import javax.activation.DataHandler;
  44. import javax.activation.DataSource;
  45. import javax.activation.FileDataSource;
  46. import javax.mail.BodyPart;
  47. import javax.mail.Message;
  48. import javax.mail.MessagingException;
  49. import javax.mail.Multipart;
  50. import javax.mail.PasswordAuthentication;
  51. import javax.mail.Session;
  52. import javax.mail.Transport;
  53. import javax.mail.internet.InternetAddress;
  54. import javax.mail.internet.MimeBodyPart;
  55. import javax.mail.internet.MimeMessage;
  56. import javax.mail.internet.MimeMultipart;
  57.  
  58. import java.io.ByteArrayInputStream;
  59. import java.io.IOException;
  60. import java.io.InputStream;
  61. import java.io.OutputStream;
  62. import java.security.Provider;
  63. import java.security.Security;
  64. import java.util.Properties;
  65.  
  66. public class GMailSender extends javax.mail.Authenticator {
  67. private String mailhost = "smtp.gmail.com";
  68. private String user;
  69. private String password;
  70. private Session session;
  71. private Multipart _multipart;
  72.  
  73. static {
  74. Security.addProvider(new com.example.jawa.pos.JSSEProvider());
  75. }
  76.  
  77. public GMailSender(String user, String password) {
  78. this.user = user;
  79. this.password = password;
  80.  
  81. Properties props = new Properties();
  82. props.setProperty("mail.transport.protocol", "smtp");
  83. props.setProperty("mail.host", mailhost);
  84. props.put("mail.smtp.auth", "true");
  85. props.put("mail.smtp.port", "465");
  86. props.put("mail.smtp.socketFactory.port", "465");
  87. props.put("mail.smtp.socketFactory.class",
  88. "javax.net.ssl.SSLSocketFactory");
  89. props.put("mail.smtp.socketFactory.fallback", "false");
  90. props.setProperty("mail.smtp.quitwait", "false");
  91. _multipart = new MimeMultipart();
  92.  
  93. session = Session.getDefaultInstance(props, this);
  94. }
  95.  
  96. protected PasswordAuthentication getPasswordAuthentication() {
  97. return new PasswordAuthentication(user, password);
  98. }
  99. public void addAttachment(String filename,String subject) throws MessagingException {
  100. BodyPart messageBodyPart = new MimeBodyPart();
  101. DataSource source = new FileDataSource(filename);
  102. messageBodyPart.setDataHandler(new DataHandler(source));
  103. messageBodyPart.setFileName(filename);
  104. _multipart.addBodyPart(messageBodyPart);
  105.  
  106. BodyPart messageBodyPart2 = new MimeBodyPart();
  107. messageBodyPart2.setText(subject);
  108.  
  109. _multipart.addBodyPart(messageBodyPart2);
  110. }
  111.  
  112.  
  113. public synchronized void sendMail(String subject, String body, String sender, String recipients) {
  114. try{
  115. MimeMessage message = new MimeMessage(session);
  116. DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  117. message.setSender(new InternetAddress(sender));
  118. message.setSubject(subject);
  119. message.setDataHandler(handler);
  120. message.setContent(_multipart);
  121. if (recipients.indexOf(',') > 0)
  122. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  123. else
  124. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  125. Transport.send(message);
  126. }catch(Exception e){
  127.  
  128. }
  129. }
  130.  
  131.  
  132. public class ByteArrayDataSource implements DataSource {
  133. private byte[] data;
  134. private String type;
  135.  
  136. public ByteArrayDataSource(byte[] data, String type) {
  137.  
  138.  
  139. super();
  140. this.data = data;
  141. this.type = type;
  142. }
  143.  
  144. public ByteArrayDataSource(byte[] data) {
  145. super();
  146. this.data = data;
  147. }
  148.  
  149. public void setType(String type) {
  150. this.type = type;
  151. }
  152.  
  153. public String getContentType() {
  154. if (type == null)
  155. return "application/octet-stream";
  156. else
  157. return type;
  158. }
  159.  
  160. public InputStream getInputStream() throws IOException {
  161. return new ByteArrayInputStream(data);
  162. }
  163.  
  164. public String getName() {
  165. return "ByteArrayDataSource";
  166. }
  167.  
  168. public OutputStream getOutputStream() throws IOException {
  169. throw new IOException("Not Supported");
  170. }
  171. }
  172.  
  173. <uses-permission android:name="android.permission.INTERNET" />
  174.  
  175. private class SendEmail extends AsyncTask<String, Integer, Long> {
  176. protected Long doInBackground(String... body) {
  177. try{
  178. GMailSender sender = new GMailSender("username@gmail.com", "password");
  179. sender.sendMail("Cold Store", "Daily Report",
  180. "user@gmail.com",
  181. "user@yahoo.com");
  182. }catch (Exception e){
  183. Log.e("Mail Error", e.getMessage());
  184. }
  185.  
  186. return null;
  187. }
  188.  
  189. protected void onProgressUpdate(Integer... progress) {
  190. }
  191.  
  192. protected void onPostExecute(Long result) {
  193. Toast.makeText(this, "Mail Send Successfully", Toast.LENGTH_SHORT).show();
  194. }
  195. }
  196.  
  197. new SendEmail().execute("body");
Add Comment
Please, Sign In to add comment