KahnClifford

MailAPI.java (Subject doesn't work)

Jul 31st, 2020 (edited)
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. package model;
  2.  
  3. /**
  4. * External JARs:
  5. *
  6. * https://mvnrepository.com/artifact/javax.mail/javax.mail-api/1.6.2
  7. * https://mvnrepository.com/artifact/com.sun.activation/jakarta.activation/1.2.1
  8. * Ant Build.
  9. */
  10.  
  11. import java.util.HashMap;
  12. import java.util.Properties;
  13. import javax.mail.Message;
  14. import javax.mail.MessagingException;
  15. import javax.mail.PasswordAuthentication;
  16. import javax.mail.Session;
  17. import javax.mail.Transport;
  18. import javax.mail.internet.InternetAddress;
  19. import javax.mail.internet.MimeMessage;
  20. import javax.mail.Authenticator;
  21. import javax.mail.Folder;
  22. import javax.mail.Store;
  23.  
  24.  
  25. public class MailAPI {
  26.  
  27. private Properties props;
  28. private Session sess;
  29. private MimeMessage mimeMsg;
  30.  
  31. private Store store;
  32. private Folder folder;
  33. private Message[] msgs;
  34.  
  35. private String host;
  36. private String from;
  37. private char[] pwd;
  38. private String to;
  39.  
  40. private String errorMsg;
  41.  
  42. public MailAPI() {
  43. this.errorMsg = "";
  44.  
  45. this.host = "pop.gmail.com";
  46.  
  47. try {
  48. this.loadProperties();
  49. this.loadDefaultSender();
  50. this.loadSession();
  51. this.loadMimeMessage();
  52. } catch (Exception e) {
  53. errorMsg += "API initialization error: "+e.getMessage();
  54. }
  55.  
  56. }
  57.  
  58. public MailAPI(String prmEmail, char[] prmPassword){
  59. this.errorMsg = "";
  60.  
  61. this.host = "pop.gmail.com";
  62. try {
  63. this.loadProperties();
  64. this.setSenderEmail(prmEmail);
  65. this.setSenderPassword(prmPassword);
  66. this.loadSession();
  67. this.loadMimeMessage();
  68. } catch (Exception e) {
  69. errorMsg += "API initialization error: "+e.getMessage();
  70. }
  71. }
  72.  
  73. public String getError(){
  74. return errorMsg;
  75. }
  76.  
  77. public String getTo(){
  78. return this.to;
  79. }
  80.  
  81. public String getFrom(){
  82. return this.from;
  83. }
  84.  
  85. public boolean emptyPassword(){
  86. return pwd.equals(null);
  87. }
  88.  
  89. public void setSenderEmail(String prmEmail){
  90. from = prmEmail;
  91. }
  92.  
  93. public void setSenderPassword(char[] prmPwd){
  94. pwd = prmPwd;
  95. }
  96.  
  97. public void setRecipient(String prmEmail){
  98. to = prmEmail;
  99. }
  100.  
  101. private void loadDefaultSender() throws Exception{
  102. this.from = "null@gmail.com"; // sender email (removed)
  103. this.pwd = new char[]{'n','u','l','l'}; // sender password (removed)
  104. }
  105.  
  106. public void getEmails(){
  107.  
  108. try {
  109.  
  110. store = sess.getStore("pop3s");
  111. store.connect(host, from, String.valueOf(pwd));
  112.  
  113. folder = store.getFolder("INBOX");
  114. folder.open(Folder.READ_WRITE);
  115. msgs = folder.getMessages();
  116.  
  117.  
  118. System.out.println("messages.length--- " + msgs.length);
  119.  
  120. for (int i = 0, n = msgs.length; i < n; i++) {
  121. Message message = msgs[i];
  122. System.out.println("---------------------------------");
  123. System.out.println("Email Number " + (i + 1));
  124. System.out.println("Subject: " + message.getSubject());
  125. System.out.println("From: " + message.getFrom()[0]);
  126. System.out.println("Text: " + message.getContent().toString());
  127.  
  128. }
  129.  
  130. //close the store and folder objects
  131. folder.close(false);
  132. store.close();
  133. sess = null;
  134. msgs = null;
  135.  
  136.  
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. }
  140.  
  141. }
  142.  
  143. public void sendMsg(String prmSub, String prmMsg){
  144. try {
  145. mimeMsg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  146. mimeMsg.setSubject(prmSub); // not working
  147. mimeMsg.setText(prmMsg);
  148.  
  149. Transport.send(mimeMsg);
  150. } catch (MessagingException e) {
  151. throw new RuntimeException(e);
  152. }
  153. }
  154.  
  155. private void loadMimeMessage() throws Exception{
  156. mimeMsg = new MimeMessage(sess);
  157. }
  158.  
  159. private void loadProperties() throws Exception{
  160. props = new Properties();
  161. //for SMTP
  162. props.put("mail.smtp.host", "smtp.gmail.com");
  163. props.put("mail.smtp.socketFactory.port", "465");
  164. props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
  165. props.put("mail.smtp.auth", "true");
  166. props.put("mail.smtp.port", "465");
  167.  
  168. //for POP3
  169. props.put("mail.store.protocol", "pop3");
  170. props.put("mail.pop3.host", "pop.gmail.com");
  171. props.put("mail.pop3.port", "995");
  172. props.put("mail.pop3.starttls.enable", "true");
  173. }
  174.  
  175. private void loadSession() throws Exception{
  176. sess = Session.getDefaultInstance(props,new APIAuth());
  177. }
  178.  
  179. private class APIAuth extends Authenticator{
  180. @Override
  181. protected PasswordAuthentication getPasswordAuthentication() {
  182. return new PasswordAuthentication(from, String.valueOf(pwd));
  183. }
  184. }
  185. }
Add Comment
Please, Sign In to add comment