Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @title MUDC mail manager
- * @author Christopher Poynton
- * @date 08/12/08
- */
- import javax.swing.*;
- import java.lang.*;
- import java.util.*;
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.event.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- public class MailMngr extends JPanel
- {
- //Declarations
- private static final String SMTP_HOST_NAME = "smtp.gmail.com";
- private static final int SMTP_PORT = 995;
- private static final String SMTP_AUTH_USER = "xxxxxxxxxxxxxxxx";
- private static final String SMTP_AUTH_PWD = "xxxxxxx";
- private static final String subject = "";
- private static final String fromAddress = "Chris Poynton <xxxxxx@gmail.com>";
- private static final String toAddress = "MailServ <xxxxxxxxxxxxxxxx>";
- JFrame frame = new JFrame("MUDC Mail Manager");
- JPanel tainer1 = new JPanel();
- JPanel tainer2 = new JPanel();
- JPanel tainer3 = new JPanel();
- JLabel mailListLbl = new JLabel("Which mailing list?");
- JLabel subscribesLbl = new JLabel("Enter subscribes here:");
- JLabel unsubscribesLbl = new JLabel("Enter unsubscribes here:");
- JList mailList = new JList();
- JTextField subscribesTF = new JTextField();
- JTextField unsubscribesTF = new JTextField();
- JButton submit = new JButton("Submit");
- String subscribes = subscribesTF.getText();
- String unsubscribes = unsubscribesTF.getText();
- String listValue = "mudc-members";
- String message = "";
- //Constructor
- public MailMngr()
- {
- setSize(250,500);
- setVisible(true);
- }
- //Display
- public void showGUI()
- {
- frame.setLayout(new BorderLayout());
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- tainer1.setLayout(new BorderLayout());
- tainer2.setLayout(new BorderLayout());
- tainer3.setLayout(new BorderLayout());
- String[] options = {"mudc-com","mudc-members"};
- mailList.setListData(options);
- mailList.addListSelectionListener(new ListListener());
- submit.addActionListener(new SubmitListener());
- //Layout
- frame.add(tainer1, BorderLayout.WEST);
- frame.add(tainer2, BorderLayout.EAST);
- frame.add(tainer3, BorderLayout.SOUTH);
- tainer1.add(mailListLbl, BorderLayout.NORTH);
- tainer1.add(subscribesLbl, BorderLayout.CENTER);
- tainer1.add(unsubscribesLbl, BorderLayout.SOUTH);
- tainer2.add(mailList, BorderLayout.NORTH);
- tainer2.add(subscribesTF, BorderLayout.CENTER);
- tainer2.add(unsubscribesTF, BorderLayout.SOUTH);
- tainer3.add(submit, BorderLayout.CENTER);
- frame.pack();
- frame.setVisible(true);
- }
- private String messageReformat(String message)
- {
- int i = 0;
- this.message = message;
- subscribes.replaceAll(";","/n subscribe "+listValue+" ");
- unsubscribes.replaceAll(";","/n unsubscribe "+listValue+" ");
- message = subscribes+unsubscribes;
- if (i == message.length()-1)
- {
- message += "/n exit";
- }
- return message;
- }
- public static void main(String[] args)
- {
- MailMngr smtpmailer = new MailMngr();
- smtpmailer.showGUI();
- }
- public void sendMail(String t, String s, String m, String f) throws MessagingException
- {
- //Email
- Properties prop = new Properties();
- Authenticator auth = new SMTPAuthenticator();
- Session sesh = Session.getDefaultInstance(prop, auth);
- Message msg = new MimeMessage(sesh);
- InternetAddress addressFrom = new InternetAddress(f);
- InternetAddress addressTo = new InternetAddress(t);
- prop.put("mail.smtp.host", SMTP_HOST_NAME);
- prop.put("mail.smtp.auth", "true");
- prop.put("mail.smtp.port", Integer.toString(SMTP_PORT));
- sesh.setDebug(false);
- msg.setFrom(addressFrom);
- msg.setRecipient(Message.RecipientType.TO, addressTo);
- msg.setSubject(subject);
- msg.setContent(this.messageReformat(message), "text/plain");
- Transport.send(msg);
- }
- class SubmitListener implements ActionListener
- {
- public void actionPerformed(ActionEvent e)
- {
- //System.out.print("This is reading");
- MailMngr smtpmailer = new MailMngr();
- try {
- smtpmailer.sendMail(toAddress, subject, message, fromAddress);
- }
- catch (Exception er)
- {
- er.printStackTrace();
- }
- }
- }
- class ListListener implements ListSelectionListener
- {
- public void valueChanged(ListSelectionEvent e)
- {
- if (e.getValueIsAdjusting()==false)
- {
- if (mailList.getSelectedIndex()==0)
- {
- listValue = "mudc-com";
- }
- }
- }
- }
- private class SMTPAuthenticator extends javax.mail.Authenticator
- {
- public PasswordAuthentication getPasswordAuthentication()
- {
- String username = SMTP_AUTH_USER;
- String password = SMTP_AUTH_PWD;
- return new PasswordAuthentication(username, password);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement