Guest User

Untitled

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