Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. package com.mygaruda.android.mail;
  2.  
  3. import javax.activation.DataHandler;
  4. import javax.activation.DataSource;
  5. import javax.activation.FileDataSource;
  6. import javax.mail.BodyPart;
  7. import javax.mail.Message;
  8. import javax.mail.Multipart;
  9. import javax.mail.PasswordAuthentication;
  10. import javax.mail.Session;
  11. import javax.mail.Transport;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeBodyPart;
  14. import javax.mail.internet.MimeMessage;
  15. import java.io.ByteArrayInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.security.Security;
  20. import java.util.Date;
  21. import java.util.Properties;
  22. import javax.mail.internet.MimeMultipart;
  23. import java.io.File;
  24.  
  25. public class GMailSender extends javax.mail.Authenticator {
  26. private String mailhost = "smtp.gmail.com";
  27. private String user;
  28. private String password;
  29. //private String filename="/sdcard/Winter.jpg";
  30. //private String filename;
  31. private String directory;
  32. private Session session;
  33. private Multipart _multipart = new MimeMultipart();
  34.  
  35. static {
  36. Security.addProvider(new JSSEProvider());
  37. }
  38.  
  39. public GMailSender(String user, String password) {
  40. this.user = user;
  41. this.password = password;
  42.  
  43. Properties props = new Properties();
  44. props.setProperty("mail.transport.protocol", "smtp");
  45. props.setProperty("mail.host", mailhost);
  46. props.put("mail.smtp.auth", "true");
  47. props.put("mail.smtp.port", "465");
  48. props.put("mail.smtp.socketFactory.port", "465");
  49. props.put("mail.smtp.socketFactory.class",
  50. "javax.net.ssl.SSLSocketFactory");
  51. props.put("mail.smtp.socketFactory.fallback", "false");
  52. props.setProperty("mail.smtp.quitwait", "false");
  53.  
  54. //session = Session.getDefaultInstance(props, this);
  55. session = Session.getInstance(props, this);
  56. }
  57.  
  58. protected PasswordAuthentication getPasswordAuthentication() {
  59. return new PasswordAuthentication(user, password);
  60. }
  61.  
  62. public synchronized void sendMail(String subject, String body, String sender, String recipients, String dir) throws Exception {
  63. this.directory=dir;
  64. System.out.println("The directory of the picture is: "+directory);
  65. MimeMessage message = new MimeMessage(session);
  66. //DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  67. message.setSender(new InternetAddress(sender));
  68. message.setSubject(subject);
  69. message.setSentDate(new Date());
  70. //message.setDataHandler(handler);
  71. if (recipients.indexOf(',') > 0)
  72. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
  73. else
  74. message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  75.  
  76. BodyPart messageBodyPart = new MimeBodyPart();
  77. messageBodyPart.setText(body);
  78. _multipart.addBodyPart(messageBodyPart);
  79.  
  80. // Adding attachments
  81. //filename = directory+"/Winter.jpg";
  82. BodyPart attachments = new MimeBodyPart();
  83. DataSource source = new FileDataSource(directory);
  84. messageBodyPart.setDataHandler(new DataHandler(source));
  85. messageBodyPart.setFileName(directory);
  86.  
  87. message.setContent(_multipart);
  88. //attachments.setContent("sdcard/Pictures/iphone.png", "image/*");
  89.  
  90. _multipart.addBodyPart(attachments);
  91. message.setContent(_multipart);
  92.  
  93. Transport.send(message);
  94. }
  95.  
  96.  
  97. public class ByteArrayDataSource implements DataSource {
  98. private byte[] data;
  99. private String type;
  100.  
  101. public ByteArrayDataSource(byte[] data, String type) {
  102. super();
  103. this.data = data;
  104. this.type = type;
  105. }
  106.  
  107. public ByteArrayDataSource(byte[] data) {
  108. super();
  109. this.data = data;
  110. }
  111.  
  112. public void setType(String type) {
  113. this.type = type;
  114. }
  115.  
  116. public String getContentType() {
  117. if (type == null)
  118. return "application/octet-stream";
  119. else
  120. return type;
  121. }
  122.  
  123. public InputStream getInputStream() throws IOException {
  124. return new ByteArrayInputStream(data);
  125. }
  126.  
  127. public String getName() {
  128. return "ByteArrayDataSource";
  129. }
  130.  
  131. public OutputStream getOutputStream() throws IOException {
  132. throw new IOException("Not Supported");
  133. }
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement