Guest User

Untitled

a guest
Jan 16th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. try {
  2. m.addAttachment(filePah);
  3.  
  4. if(m.send()) {
  5. Toast.makeText(Comunic.this, "Ok", Toast.LENGTH_LONG).show();
  6. } else {
  7. Toast.makeText(Comunic.this, "Error.", Toast.LENGTH_LONG).show();
  8. }
  9. } catch(Exception e) {
  10. Toast.makeText(Comunic.this, "Error Attachment.", Toast.LENGTH_LONG).show();
  11. }
  12.  
  13. m.addAttachment(filePath);
  14.  
  15. /mnt/sdcard/mnt/sdcard/AAAAAA/readme.txt
  16.  
  17. Mail m = new Mail(username, password);
  18.  
  19. public class Mail extends javax.mail.Authenticator {
  20. private String _user;
  21. private String _pass;
  22.  
  23. private String[] _to;
  24. private String _from;
  25.  
  26. private String _port;
  27. private String _sport;
  28.  
  29. private String _host;
  30.  
  31. private String _subject;
  32. private String _body;
  33.  
  34. private boolean _auth;
  35.  
  36. private boolean _debuggable;
  37.  
  38. private Multipart _multipart;
  39.  
  40.  
  41. public Mail() {
  42. _host = "smtp.gmail.com"; // default smtp server
  43. _port = "465"; // default smtp port
  44. _sport = "465"; // default socketfactory port
  45.  
  46. _user = "";
  47. _pass = "";
  48. _from = ""; // email sent from
  49. _subject = ""; // email subject
  50. _body = ""; // email body
  51.  
  52. _debuggable = false; // debug mode on or off - default off
  53. _auth = true; // smtp authentication - default on
  54.  
  55. _multipart = new MimeMultipart();
  56.  
  57. // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
  58. MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  59. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  60. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  61. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  62. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  63. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  64. CommandMap.setDefaultCommandMap(mc);
  65. }
  66.  
  67.  
  68.  
  69. public Mail(String user, String pass) {
  70. this();
  71.  
  72. _user = user;
  73. _pass = pass;
  74. }
  75.  
  76. public boolean send() throws Exception {
  77. Properties props = _setProperties();
  78.  
  79. if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
  80. Session session = Session.getInstance(props, this);
  81.  
  82. MimeMessage msg = new MimeMessage(session);
  83.  
  84. msg.setFrom(new InternetAddress(_from));
  85.  
  86. InternetAddress[] addressTo = new InternetAddress[_to.length];
  87. for (int i = 0; i < _to.length; i++) {
  88. addressTo[i] = new InternetAddress(_to[i]);
  89. }
  90. msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  91.  
  92. msg.setSubject(_subject);
  93. msg.setSentDate(new Date());
  94.  
  95. // setup message body
  96. BodyPart messageBodyPart = new MimeBodyPart();
  97. messageBodyPart.setText(_body);
  98. _multipart.addBodyPart(messageBodyPart);
  99.  
  100. // Put parts in message
  101. msg.setContent(_multipart);
  102.  
  103. // send email
  104. Transport.send(msg);
  105.  
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111.  
  112.  
  113. public void addAttachment(String filename) throws Exception {
  114. BodyPart messageBodyPart = new MimeBodyPart();
  115. DataSource source = new FileDataSource(filename);
  116. messageBodyPart.setDataHandler(new DataHandler(source));
  117. messageBodyPart.setFileName(filename);
  118.  
  119. _multipart.addBodyPart(messageBodyPart);
  120. }
  121.  
  122. @Override
  123. public PasswordAuthentication getPasswordAuthentication() {
  124. return new PasswordAuthentication(_user, _pass);
  125. }
  126.  
  127. private Properties _setProperties() {
  128. Properties props = new Properties();
  129.  
  130. props.put("mail.smtp.host", _host);
  131.  
  132. if(_debuggable) {
  133. props.put("mail.debug", "true");
  134. }
  135.  
  136. if(_auth) {
  137. props.put("mail.smtp.auth", "true");
  138. }
  139.  
  140. props.put("mail.smtp.port", _port);
  141. props.put("mail.smtp.socketFactory.port", _sport);
  142. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  143. props.put("mail.smtp.socketFactory.fallback", "false");
  144.  
  145. return props;
  146. }
  147.  
  148.  
  149.  
  150. public String getBody() {
  151. return _body;
  152. }
  153.  
  154.  
  155. public String getFrom() {
  156. return _from;
  157. }
  158.  
  159.  
  160. public String[] getTo() {
  161. return _to;
  162. }
  163.  
  164.  
  165. public String getSubject() {
  166. return _subject;
  167. }
  168.  
  169.  
  170. public void setBody(String _body) {
  171. this._body = _body;
  172. }
  173.  
  174.  
  175. public void setFrom(String _from) {
  176. this._from = _from;
  177. }
  178.  
  179.  
  180. public void setTo(String[] _to) {
  181. this._to = _to;
  182. }
  183.  
  184.  
  185. public void setSubject(String _subject) {
  186. this._subject = _subject;
  187. }
  188.  
  189.  
  190.  
  191. }
  192.  
  193. Email(10235): javax.mail.MessagingException: IOException while sending message;
  194. Email(10235): nested exception is:
  195. Email(10235): java.io.FileNotFoundException: /mnt/sdcard/mnt/sdcard/AAAAAA/readme.txt (No such file or directory)
  196. Email(10235): at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:676)
  197. Email(10235): at javax.mail.Transport.send0(Transport.java:189)
  198. Email(10235): at javax.mail.Transport.send(Transport.java:118)
  199. Email(10235): at my.app.Mail.send(Mail.java:108)
  200.  
  201. private void openFile() {
  202. Intent i = new Intent(Intent.ACTION_GET_CONTENT);
  203. i.setType("file/*");
  204. startActivityForResult(i, FILE_REQ_CODE);
  205. }
Add Comment
Please, Sign In to add comment