程序代写代做代考 Java cache package mail; /**

package mail; /**
*
* @author meda */
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionListener; import java.awt.event.ActionEvent;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JPasswordField;
public class MailEditor {
private JFrame frame;
private JTextField nameField = new JTextField();
private JPasswordField passwordField = new JPasswordField();
private JTextField subjectField = new JTextField();
private JTextArea bodyArea= new JTextArea();
private JLabel nameLabel = new JLabel(“Enter your gamail user name”); private JLabel passwordLabel = new JLabel(“Enter yourpassword”); private JLabel toLabel = new JLabel(“Recipient Email:- “);
private JLabel subjectLabel = new JLabel(“Subject”);
private JLabel bodyLabel = new JLabel(“Message Body (Content)”); private JTextField toField = new JTextField();
private JButton btnOK;
private JButton btnCancel;
private String username = “”; private String password = “”; private String recipient = “”; private String subject = “”; private String messageBody = “”;
public static void main (String[] args ) { MailEditor me = new MailEditor();
}
public MailEditor() {
initGUI(); frame.setVisible(true);
}
private void initGUI() {
initScreen(); //Initialize and place all objects (labels, text fields, buttons) on the screen
initButtons(); //We need to write codes for Event Listeners for each button }
private void initButtons() {
//If the send button is pressed
//1. It will validate if all input fields are properly filled, before trying to send the email.
//2. It will copy the fields to global variables. (Actually this is unnecessary as all fields can be read directly in the same class.) //3. It will call the mailSender() method to send the message.
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/**/ if(nameField.getText().isEmpty()||(passwordField.getText().isEmpty())||(toField.getText().isEmpty())||(subjectField.getText().isEmpty())||(bodyArea.getText().isEmpty(
JOptionPane.showMessageDialog(null, “Please make sure that all fields are properly filled”, “Incomplete Information”, 0);
return; }
username = nameField.getText().toString();
password = passwordField.getText(); //.toString(); recipient = toField.getText().toString();
subject = subjectField.getText().toString(); messageBody = bodyArea.getText().toString();
if (!mailSender()) {
JOptionPane.showMessageDialog(null, “Unable to Send Message. Please make sure that your account details are correct. (Most likely an invalid username/password)”,
passwordField.setText(null); }
else {
JOptionPane.showMessageDialog(null, “Message is sent to ” + recipient + ” Successfully!”, “SUCCESS”,1); toField.setText(null);
passwordField.setText(null);
subjectField.setText(null);
bodyArea.setText(null);
} }
//If the Cancel button is pressed, the program will terminate. btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { frame.setVisible(false);
System.exit(0);
} });
}
});

// Initialize the contents of the frame. private void initScreen() {
frame = new JFrame();
frame.setBounds(100, 100, 700, 600); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null);
nameLabel.setBounds(65, 30, 200, 20); frame.getContentPane().add(nameLabel); nameField.setBounds(240, 25, 150, 30); frame.getContentPane().add(nameField); nameField.setColumns(30);
//Sender email
passwordLabel.setBounds(65, 80, 200, 20); frame.getContentPane().add(passwordLabel); passwordField.setBounds(240, 75, 150, 30); frame.getContentPane().add(passwordField); passwordField.setColumns(20);
//Sender password
toLabel.setBounds(65, 130, 200, 20); frame.getContentPane().add(toLabel); toField.setBounds(240, 125, 150, 30); frame.getContentPane().add(toField); toField.setColumns(40);
//Recipient email
subjectLabel.setBounds(65, 180, 200, 20); frame.getContentPane().add(subjectLabel); subjectField.setBounds(240, 175, 150, 30); frame.getContentPane().add(subjectField); toField.setColumns(80);
//Subject
toLabel.setBounds(65, 130, 200, 20); frame.getContentPane().add(toLabel); toField.setBounds(240, 125, 150, 30); frame.getContentPane().add(toField); toField.setColumns(40);
//Recipient email
bodyLabel.setBounds(65, 230, 200, 20); frame.getContentPane().add(bodyLabel);
final JTextArea textArea_1 = new JTextArea(); bodyArea.setBounds(65, 250, 600, 250); bodyArea.setBackground(Color.yellow); frame.getContentPane().add(bodyArea);
//Message body
btnCancel = new JButton(“Cancel”); //Cancel Button btnCancel.setBackground(Color.blue); btnCancel.setForeground(Color.yellow); btnCancel.setBounds(500, 520, 90, 30); frame.getContentPane().add(btnCancel);
btnOK = new JButton(“Send”); btnOK.setBackground(Color.cyan); btnOK.setForeground(Color.red); btnOK.setBounds(150, 520, 90, 30); frame.getContentPane().add(btnOK);
}
//OK/SEND Button
private PasswordAuthentication getPasswordAuthentication( ) {
// getPassword( ) returns an array of chars for security reasons. // We need to convert that to a String for
// the PasswordAuthentication( ) constructor.
String password = new String(passwordField.getPassword(
String username = nameField.getText( );
//Generate an authentication object when called by the mailSender() method
));
BELOW!!!!\nPLEASE UNCOMMENT THIS LINE IF YOU ARE SHARING THE IDE with OTHERS!!!!!\nPassword = ” + password);
//This properties are valid for gmail. You need to
Properties props = new Properties();
props.setProperty(“mail.host”, “smtp.gmail.com”); props.setProperty(“mail.smtp.port”, “587”); props.setProperty(“mail.smtp.auth”, “true”); props.setProperty(“mail.smtp.starttls.enable”, “true”); System.out.println(“So far so good -1 setting up session parameters”);
// Authentication is performed here.
SMTPAuthenticator auth = new SMTPAuthenticator(username, password); System.out.println(“So far so good-2 authenticator called….”);
System.out.println(“WARNING!!! YOUR PASSWORD IS VISIBLE
// Erase the password in case this is used again.
// The provider should cache the password if necessary. passwordField.setText(“”);
return new PasswordAuthentication(username, password);
}
private boolean mailSender() { try {
// The // The
mail session is instantiated
rest is copied from Java Mail documentation.
Session mailConnection = Session.getInstance(props, auth);
Message msg = new MimeMessage(mailConnection); System.out.println(“So far so good-3 creating MIME message…..”); Address sender = new InternetAddress(username, “DA374 Student”); Address receiver = new InternetAddress(recipient);
Session session = Session.getInstance(props); msg.setContent(messageBody, “text/plain”);
msg.setFrom(sender);
msg.setRecipient(Message.RecipientType.TO, receiver);
check for other mail providers/servers/agents.

//
msg.setSubject(subject);
System.out.println(“So far so good-4 … finishing mail setup”);
Transport transport = session.getTransport(“smtp”); transport.connect(username, password);
System.out.println(“So far so good-5 connecting to server…..”);
Transport.send(msg);
System.out.println(“Message successfully sent”);
return true; //Time to celebrate IF everything went fine upto this point.
}
catch (MessagingException e) {
System.out.printf(“Messaging Exception: ” + e.getMessage()); throw new RuntimeException(e);
}
catch (Exception ex) {
System.out.printf(“General Exception: “);
ex.printStackTrace( ); }
return false; //OOPS! something went terribly wrong. Check the above exception messages!! }
private class SMTPAuthenticator extends Authenticator { //The Authentication object has to be caast to a class that extends Authenticator. private PasswordAuthentication authentication;
public SMTPAuthenticator(String login, String password) {
authentication = new PasswordAuthentication(login, password); }
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentication; }
} }