Advertisement
Guest User

Untitled

a guest
May 10th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. /*
  2. * @title MUDC mail manager
  3. * @author Christopher Poynton
  4. * @date 08/12/08
  5. */
  6.  
  7. import javax.swing.*;
  8. import java.lang.*;
  9. import java.util.*;
  10. import java.io.*;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import javax.swing.event.*;
  14. import javax.mail.*;
  15. import javax.mail.internet.*;
  16.  
  17. public class MailMngr extends JPanel
  18. {
  19. //Declarations
  20. private static final String SMTP_HOST_NAME = "smtp.gmail.com";
  21. private static final int SMTP_PORT = 995;
  22. private static final String SMTP_AUTH_USER = "xxxxxxxxxxxxxxxx";
  23. private static final String SMTP_AUTH_PWD = "xxxxxxx";
  24. private static final String subject = "";
  25. private static final String fromAddress = "Chris Poynton <xxxxxx@gmail.com>";
  26. private static final String toAddress = "MailServ <xxxxxxxxxxxxxxxx>";
  27.  
  28.  
  29. JFrame frame = new JFrame("MUDC Mail Manager");
  30. JPanel tainer1 = new JPanel();
  31. JPanel tainer2 = new JPanel();
  32. JPanel tainer3 = new JPanel();
  33. JLabel mailListLbl = new JLabel("Which mailing list?");
  34. JLabel subscribesLbl = new JLabel("Enter subscribes here:");
  35. JLabel unsubscribesLbl = new JLabel("Enter unsubscribes here:");
  36. JList mailList = new JList();
  37. JTextField subscribesTF = new JTextField();
  38. JTextField unsubscribesTF = new JTextField();
  39. JButton submit = new JButton("Submit");
  40. String subscribes = subscribesTF.getText();
  41. String unsubscribes = unsubscribesTF.getText();
  42. String listValue = "mudc-members";
  43. String message = "";
  44.  
  45. //Constructor
  46. public MailMngr()
  47. {
  48. setSize(250,500);
  49. setVisible(true);
  50. }
  51.  
  52. //Display
  53. public void showGUI()
  54. {
  55. frame.setLayout(new BorderLayout());
  56. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  57. tainer1.setLayout(new BorderLayout());
  58. tainer2.setLayout(new BorderLayout());
  59. tainer3.setLayout(new BorderLayout());
  60. String[] options = {"mudc-com","mudc-members"};
  61. mailList.setListData(options);
  62. mailList.addListSelectionListener(new ListListener());
  63. submit.addActionListener(new SubmitListener());
  64.  
  65. //Layout
  66. frame.add(tainer1, BorderLayout.WEST);
  67. frame.add(tainer2, BorderLayout.EAST);
  68. frame.add(tainer3, BorderLayout.SOUTH);
  69. tainer1.add(mailListLbl, BorderLayout.NORTH);
  70. tainer1.add(subscribesLbl, BorderLayout.CENTER);
  71. tainer1.add(unsubscribesLbl, BorderLayout.SOUTH);
  72. tainer2.add(mailList, BorderLayout.NORTH);
  73. tainer2.add(subscribesTF, BorderLayout.CENTER);
  74. tainer2.add(unsubscribesTF, BorderLayout.SOUTH);
  75. tainer3.add(submit, BorderLayout.CENTER);
  76. frame.pack();
  77. frame.setVisible(true);
  78. }
  79.  
  80. private String messageReformat(String message)
  81. {
  82. int i = 0;
  83. this.message = message;
  84. subscribes.replaceAll(";","/n subscribe "+listValue+" ");
  85. unsubscribes.replaceAll(";","/n unsubscribe "+listValue+" ");
  86. message = subscribes+unsubscribes;
  87.  
  88. if (i == message.length()-1)
  89. {
  90. message += "/n exit";
  91. }
  92. return message;
  93. }
  94.  
  95. public static void main(String[] args)
  96. {
  97. MailMngr smtpmailer = new MailMngr();
  98. smtpmailer.showGUI();
  99. }
  100.  
  101. public void sendMail(String t, String s, String m, String f) throws MessagingException
  102. {
  103. //Email
  104. Properties prop = new Properties();
  105. Authenticator auth = new SMTPAuthenticator();
  106. Session sesh = Session.getDefaultInstance(prop, auth);
  107. Message msg = new MimeMessage(sesh);
  108. InternetAddress addressFrom = new InternetAddress(f);
  109. InternetAddress addressTo = new InternetAddress(t);
  110.  
  111. prop.put("mail.smtp.host", SMTP_HOST_NAME);
  112. prop.put("mail.smtp.auth", "true");
  113. prop.put("mail.smtp.port", Integer.toString(SMTP_PORT));
  114. sesh.setDebug(false);
  115.  
  116. msg.setFrom(addressFrom);
  117. msg.setRecipient(Message.RecipientType.TO, addressTo);
  118. msg.setSubject(subject);
  119. msg.setContent(this.messageReformat(message), "text/plain");
  120. Transport.send(msg);
  121. }
  122.  
  123. class SubmitListener implements ActionListener
  124. {
  125. public void actionPerformed(ActionEvent e)
  126. {
  127. //System.out.print("This is reading");
  128. MailMngr smtpmailer = new MailMngr();
  129. try {
  130. smtpmailer.sendMail(toAddress, subject, message, fromAddress);
  131. }
  132. catch (Exception er)
  133. {
  134. er.printStackTrace();
  135. }
  136. }
  137. }
  138.  
  139. class ListListener implements ListSelectionListener
  140. {
  141. public void valueChanged(ListSelectionEvent e)
  142. {
  143. if (e.getValueIsAdjusting()==false)
  144. {
  145. if (mailList.getSelectedIndex()==0)
  146. {
  147. listValue = "mudc-com";
  148. }
  149. }
  150. }
  151. }
  152.  
  153. private class SMTPAuthenticator extends javax.mail.Authenticator
  154. {
  155. public PasswordAuthentication getPasswordAuthentication()
  156. {
  157. String username = SMTP_AUTH_USER;
  158. String password = SMTP_AUTH_PWD;
  159. return new PasswordAuthentication(username, password);
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement