Guest User

Untitled

a guest
Sep 11th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. Email not sent | Using JavaMail API for Android
  2. public void onClick(View v){
  3. Runnable runnable = new Runnable(){
  4.  
  5. @Override
  6. public void run() {
  7. Mail m = new Mail("MY Gmail Address", "My password");
  8.  
  9. String[] toArr = {"sender@gmail.com"};
  10. m.setTo(toArr);
  11. m.setFrom("wooo@wooo.com");
  12. m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
  13. m.setBody("Email body.");
  14.  
  15. try {
  16. m.addAttachment("/sdcard/filelocation");
  17.  
  18. if(m.send()) {
  19. Toast.makeText(MyActivity.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
  20. } else {
  21. Toast.makeText(MyActivity.this, "Email was not sent.", Toast.LENGTH_LONG).show();
  22. }
  23. } catch(Exception e) {
  24. //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
  25. Log.e("MailApp", "Could not send email", e);
  26. }
  27.  
  28.  
  29. }
  30.  
  31. };
  32. new Thread(runnable).start();
  33. }
  34.  
  35. private String _to;
  36. private String _from;
  37.  
  38. private String _port;
  39. private String _sport;
  40.  
  41. private String _host;
  42.  
  43. private String _subject;
  44. private String _body;
  45.  
  46. private boolean _auth;
  47.  
  48. private boolean _debuggable;
  49.  
  50. private Multipart _multipart;
  51.  
  52.  
  53. public Mail() {
  54. _host = "smtp.gmail.com"; // default smtp server
  55. _port = "465"; // default smtp port
  56. _sport = "465"; // default socketfactory port
  57.  
  58. _user = "My mail id"; // username
  59. _pass = "My Password"; // password
  60. _from = ""; // email sent from
  61. _subject = "Hi"; // email subject
  62. _body = "how are you"; // email body
  63.  
  64. _debuggable = false; // debug mode on or off - default off
  65. _auth = true; // smtp authentication - default on
  66.  
  67. _multipart = new MimeMultipart();
  68.  
  69. // There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
  70. MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
  71. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  72. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  73. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  74. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  75. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  76. CommandMap.setDefaultCommandMap(mc);
  77. }
  78.  
  79. public void setFrom(String string) {
  80. // TODO Auto-generated method stub
  81. _from="from email address";
  82.  
  83.  
  84. }
  85.  
  86. public void setSubject(String string) {
  87. // TODO Auto-generated method stub
  88. _subject="Hi";
  89. }
  90.  
  91. public void setTo(String[] toArr) {
  92. // TODO Auto-generated method stub
  93. _to = "to email address";
  94. }
  95.  
  96. public Mail(String user, String pass) {
  97. this();
  98.  
  99. _user = user;
  100. _pass = pass;
  101. }
  102.  
  103. public boolean send() throws Exception {
  104. Properties props = _setProperties();
  105.  
  106. if(!_user.equals("") && !_pass.equals("") && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {
  107. Session session = Session.getInstance(props);
  108.  
  109. MimeMessage msg = new MimeMessage(session);
  110.  
  111. msg.setFrom(new InternetAddress(_from));
  112.  
  113. //InternetAddress[] addressTo = new InternetAddress[_to.toString()];
  114. /*for (int i = 0; i < _to.length; i++) {
  115. addressTo[i] = new InternetAddress(_to[i]);
  116. }*/
  117. msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(_to));
  118.  
  119. msg.setSubject(_subject);
  120.  
  121.  
  122. // setup message body
  123. BodyPart messageBodyPart = new MimeBodyPart();
  124. messageBodyPart.setText(_body);
  125. _multipart.addBodyPart(messageBodyPart);
  126.  
  127. // Put parts in message
  128. msg.setContent(_multipart);
  129.  
  130. // send email
  131. Transport.send(msg);
  132.  
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138.  
  139. public void addAttachment(String filename) throws Exception {
  140. BodyPart messageBodyPart = new MimeBodyPart();
  141. DataSource source = new FileDataSource(filename);
  142. messageBodyPart.setDataHandler(new DataHandler(source));
  143. messageBodyPart.setFileName(filename);
  144.  
  145. _multipart.addBodyPart(messageBodyPart);
  146. }
  147.  
  148.  
  149. public PasswordAuthentication getPasswordAuthentication() {
  150. return new PasswordAuthentication(_user, _pass);
  151. }
  152.  
  153. private Properties _setProperties() {
  154. Properties props = new Properties();
  155.  
  156. props.put("mail.smtp.host", _host);
  157.  
  158. if(_debuggable) {
  159. props.put("mail.debug", "true");
  160. }
  161.  
  162. if(_auth) {
  163. props.put("mail.smtp.auth", "true");
  164. }
  165.  
  166. props.put("mail.smtp.port", _port);
  167. props.put("mail.smtp.socketFactory.port", _sport);
  168. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  169. props.put("mail.smtp.socketFactory.fallback", "false");
  170.  
  171. return props;
  172. }
  173.  
  174. // the getters and setters
  175. public String getBody() {
  176. return _body;
  177. }
  178.  
  179. public void setBody(String _body) {
  180. this._body = _body;
  181. }
  182.  
  183. 06-12 13:26:42.523: E/MailApp(8579): Could not send email
  184. 06-12 13:26:42.523: E/MailApp(8579): java.lang.NullPointerException
  185. 06-12 13:26:42.523: E/MailApp(8579): at com.MyApp.MyActivity$Mail.send(MyActivity.java:280)
  186. 06-12 13:26:42.523: E/MailApp(8579): at com.MyApp.MActivity$1.run(MActivity.java:146)
  187. 06-12 13:26:42.523: E/MailApp(8579): at java.lang.Thread.run(Thread.java:1019)
  188.  
  189. SMTPAuthenticator auth = new SMTPAuthenticator();
  190. session = Session.getDefaultInstance(props,auth);
  191.  
  192.  
  193. private class SMTPAuthenticator extends javax.mail.Authenticator {
  194. public PasswordAuthentication getPasswordAuthentication() {
  195. String username = "your_username";
  196. String password = "your_password";
  197. return new PasswordAuthentication(username, password);
  198. }
  199. }
Add Comment
Please, Sign In to add comment