Guest User

Untitled

a guest
Oct 1st, 2018
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  2. Properties pr = new Properties();
  3. pr.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
  4. pr.setProperty("mail.pop3.socketFactory.fallback", "false");
  5. pr.setProperty("mail.pop3.port", "995");
  6. pr.setProperty("mail.pop3.socketFactory.port", "995");
  7. pr.put("mail.pop3.host", "pop.gmail.com");
  8. pr.setProperty("mail.store.protocol", "pop3");
  9.  
  10. try {
  11. Session session = Session.getDefaultInstance(pr, null);
  12. Store store = session.getStore();
  13. store.connect("username", "password");
  14. if(store.isConnected()==true)
  15. {
  16. System.out.println("Connection is OK");
  17. }
  18. // Folder inbox = store.getFolder("INBOX");
  19. Folder inbox = store.getDefaultFolder().getFolder("INBOX");
  20. inbox.open(Folder.READ_ONLY);
  21. Message messages[] = inbox.getMessages();
  22. for(Message message:messages)
  23. {
  24. // System.out.println(message.getSentDate());
  25. message.writeTo(System.out);
  26. }
  27.  
  28. Mail m = new Mail(your email_id), your id password ));
  29. String[] toArr = { send email id };
  30. m.setTo(toArr);
  31. m.setFrom(your email_id);
  32. m.setSubject("Out of subject");
  33. m.setBody("Out Of msg ");
  34. try {
  35. m.send();
  36. // Log.v("TAg", "send Mail");
  37. } catch (Exception e) {
  38. // Log.e("MailApp", "Could not send email", e);
  39. }
  40.  
  41. public class Mail extends javax.mail.Authenticator {
  42. private String _user;
  43. private String _pass;
  44.  
  45. private String[] _to;
  46. private String _from;
  47.  
  48. private String _port;
  49. private String _sport;
  50.  
  51. private String _host;
  52.  
  53. private String _subject;
  54. private String _body;
  55.  
  56. private boolean _auth;
  57.  
  58. private boolean _debuggable;
  59.  
  60. private Multipart _multipart;
  61.  
  62. public Mail() {
  63. _host = "smtp.gmail.com"; // default smtp server
  64. _port = "465"; // default smtp port
  65. _sport = "465"; // default socketfactory port
  66.  
  67. _user = ""; // username
  68. _pass = ""; // password
  69. _from = ""; // email sent from
  70. _subject = ""; // email subject
  71. _body = ""; // email body
  72.  
  73. _debuggable = false; // debug mode on or off - default off
  74. _auth = true; // smtp authentication - default on
  75.  
  76. _multipart = new MimeMultipart();
  77.  
  78. // There is something wrong with MailCap, javamail can not find a
  79. // handler for the multipart/mixed part, so this bit needs to be added.
  80. MailcapCommandMap mc = (MailcapCommandMap) CommandMap
  81. .getDefaultCommandMap();
  82. mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
  83. mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
  84. mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
  85. mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
  86. mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
  87. CommandMap.setDefaultCommandMap(mc);
  88. }
  89.  
  90. public Mail(String user, String pass) {
  91. this();
  92.  
  93. _user = user;
  94. _pass = pass;
  95. }
  96.  
  97. public void setTo(String[] toArr) {
  98. _to = toArr;
  99. }
  100.  
  101. public void setFrom(String from) {
  102. _from = from;
  103. }
  104.  
  105. public void setSubject(String subject) {
  106. _subject = subject;
  107. }
  108.  
  109. /** method will be check email validation **/
  110. public boolean send() throws Exception {
  111. Properties props = _setProperties();
  112.  
  113. if (!_user.equals("") && !_pass.equals("") && _to.length > 0
  114. && !_from.equals("") && !_subject.equals("")
  115. && !_body.equals("")) {
  116. Session session = Session.getInstance(props, this);
  117.  
  118. MimeMessage msg = new MimeMessage(session);
  119.  
  120. msg.setFrom(new InternetAddress(_from));
  121.  
  122. InternetAddress[] addressTo = new InternetAddress[_to.length];
  123. for (int i = 0; i < _to.length; i++) {
  124. addressTo[i] = new InternetAddress(_to[i]);
  125. }
  126. msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
  127.  
  128. msg.setSubject(_subject);
  129. msg.setSentDate(new Date());
  130.  
  131. // setup message body
  132. BodyPart messageBodyPart = new MimeBodyPart();
  133. messageBodyPart.setText(_body);
  134. _multipart.addBodyPart(messageBodyPart);
  135.  
  136. // Put parts in message
  137. msg.setContent(_multipart);
  138.  
  139. // send email
  140. Transport.send(msg);
  141.  
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }
  147.  
  148. /** This method(addAttachment) will allow attach the file in mail **/
  149. public void addAttachment(String filename) throws Exception {
  150. BodyPart messageBodyPart = new MimeBodyPart();
  151. DataSource source = new FileDataSource(filename);
  152. messageBodyPart.setDataHandler(new DataHandler(source));
  153. messageBodyPart.setFileName(filename);
  154.  
  155. _multipart.addBodyPart(messageBodyPart);
  156. }
  157.  
  158. public PasswordAuthentication getPasswordAuthentication() {
  159. return new PasswordAuthentication(_user, _pass);
  160. }
  161.  
  162. private Properties _setProperties() {
  163. Properties props = new Properties();
  164.  
  165. props.put("mail.smtp.host", _host);
  166.  
  167. if (_debuggable) {
  168. props.put("mail.debug", "true");
  169. }
  170.  
  171. if (_auth) {
  172. props.put("mail.smtp.auth", "true");
  173. }
  174.  
  175. props.put("mail.smtp.port", _port);
  176. props.put("mail.smtp.socketFactory.port", _sport);
  177. props.put("mail.smtp.socketFactory.class",
  178. "javax.net.ssl.SSLSocketFactory");
  179. props.put("mail.smtp.socketFactory.fallback", "false");
  180.  
  181. return props;
  182. }
  183.  
  184. // the getters and setters
  185. public String getBody() {
  186. return _body;
  187. }
  188.  
  189. public void setBody(String _body) {
  190. this._body = _body;
  191. }
  192.  
  193. // more of the getters and setters …..
  194. }
Add Comment
Please, Sign In to add comment