package mail;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
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.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MailServer extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JPanel fP = null;
private JButton sB = null;
private JButton rB = null;
private JPanel sP = null;
private JButton rpBn = null;
private JTextField rcTF = null;
private JButton wBn = null;
private JTextField wTF = null;
private JButton sBn = null;
private JTextField sTF = null;
private JButton myMailBoxBn = null;
private JTextField myAddressTF = null;
private JButton myPassBn = null;
private JPasswordField myPassTF = null;
private JScrollPane contentSP = null;
private JTextArea contenTA = null;
private String myEmail = null; // @jve:decl-index=0:
private String password = "XXXX"; // @jve:decl-index=0:
private String host = "smtp.gmail.com"; // @jve:decl-index=0:
private String port = "465"; // @jve:decl-index=0:
private String recieverEmail = null; //@jve:decl-index
// =0:
private String subject = "Testing";
private String content = null;
MimeMessage msg = null;
/**
* This method initializes fP
*
* @return javax.swing.JPanel
*/
private JPanel getFP() {
if (fP == null) {
FlowLayout flowLayout = new FlowLayout();
flowLayout.setAlignment(java.awt.FlowLayout.CENTER);
fP = new JPanel();
fP.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
fP.setLayout(flowLayout);
fP.setEnabled(false);
fP.add(getSB(), null);
fP.add(getRB(), null);
}
return fP;
}
/**
* This method initializes sB
*
* @return javax.swing.JButton
*/
private JButton getSB() {
if (sB == null) {
sB = new JButton();
sB.setText("发送");
sB.setActionCommand("send");
sB.addActionListener(new cBn());
sB.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return sB;
}
/**
* This method initializes rB
*
* @return javax.swing.JButton
*/
private JButton getRB() {
if (rB == null) {
rB = new JButton();
rB.setText("接收");
rB.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return rB;
}
/**
* This method initializes sP
*
* @return javax.swing.JPanel
*/
private JPanel getSP() {
if (sP == null) {
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.insets = new Insets(0, 0, 10, 0);
GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
gridBagConstraints10.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints10.fill = GridBagConstraints.VERTICAL;
gridBagConstraints10.insets = new Insets(0, 0, 10, 0);
gridBagConstraints10.weightx = 1.0;
GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
gridBagConstraints9.insets = new Insets(0, 0, 8, 0);
GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
gridBagConstraints8.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints8.fill = GridBagConstraints.VERTICAL;
gridBagConstraints8.gridy = 3;
gridBagConstraints8.weightx = 1.0;
gridBagConstraints8.insets = new Insets(0, 0, 8, 0);
gridBagConstraints8.gridx = 1;
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
gridBagConstraints7.gridy = 2;
gridBagConstraints7.weightx = 1.0;
gridBagConstraints7.insets = new Insets(0, 0, 8, 0);
gridBagConstraints7.gridx = 1;
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.ipadx = 24;
gridBagConstraints6.insets = new Insets(0, 0, 8, 0);
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.gridwidth = GridBagConstraints.REMAINDER;
gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
gridBagConstraints5.insets = new Insets(0, 0, 8, 0);
gridBagConstraints5.weightx = 1.0;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.ipadx = 24;
gridBagConstraints4.insets = new Insets(0, 0, 8, 0);
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.insets = new Insets(0, 0, 8, 0);
gridBagConstraints3.ipadx = 12;
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.insets = new Insets(0, 0, 8, 0);
gridBagConstraints2.gridwidth = GridBagConstraints.REMAINDER;
sP = new JPanel();
sP.setLayout(new GridBagLayout());
sP.setBorder(BorderFactory.createLineBorder(Color.gray, 0));
sP.add(getRpBn(), gridBagConstraints3);
sP.add(getRcTF(), gridBagConstraints2);
sP.add(getWBn(), gridBagConstraints4);
sP.add(getWTF(), gridBagConstraints5);
sP.add(getSBn(), gridBagConstraints6);
sP.add(getSTF(), gridBagConstraints7);
sP.add(getMyMailBoxBn(), gridBagConstraints9);
sP.add(getMyAddressTF(), gridBagConstraints8);
sP.add(getMyPassBn(), gridBagConstraints11);
sP.add(getMyPassTF(), gridBagConstraints10);
}
return sP;
}
/**
* This method initializes rpBn
*
* @return javax.swing.JButton
*/
private JButton getRpBn() {
if (rpBn == null) {
rpBn = new JButton();
rpBn.setText("收件人:");
rpBn.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return rpBn;
}
/**
* This method initializes rcTF
*
* @return javax.swing.JTextField
*/
private JTextField getRcTF() {
if (rcTF == null) {
rcTF = new JTextField();
rcTF.setColumns(50);
}
return rcTF;
}
/**
* This method initializes wBn
*
* @return javax.swing.JButton
*/
private JButton getWBn() {
if (wBn == null) {
wBn = new JButton();
wBn.setText("抄送:");
wBn.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return wBn;
}
/**
* This method initializes wTF
*
* @return javax.swing.JTextField
*/
private JTextField getWTF() {
if (wTF == null) {
wTF = new JTextField();
wTF.setColumns(50);
}
return wTF;
}
/**
* This method initializes sBn
*
* @return javax.swing.JButton
*/
private JButton getSBn() {
if (sBn == null) {
sBn = new JButton();
sBn.setText("主题:");
sBn.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return sBn;
}
/**
* This method initializes sTF
*
* @return javax.swing.JTextField
*/
private JTextField getSTF() {
if (sTF == null) {
sTF = new JTextField();
sTF.setColumns(50);
}
return sTF;
}
/**
* This method initializes myMailBoxBn
*
* @return javax.swing.JButton
*/
private JButton getMyMailBoxBn() {
if (myMailBoxBn == null) {
myMailBoxBn = new JButton();
myMailBoxBn.setText("我的邮箱:");
myMailBoxBn.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return myMailBoxBn;
}
/**
* This method initializes myAddressTF
*
* @return javax.swing.JTextField
*/
private JTextField getMyAddressTF() {
if (myAddressTF == null) {
myAddressTF = new JTextField();
myAddressTF.setColumns(50);
}
return myAddressTF;
}
/**
* This method initializes myPassBn
*
* @return javax.swing.JButton
*/
private JButton getMyPassBn() {
if (myPassBn == null) {
myPassBn = new JButton();
myPassBn.setText("邮箱密码:");
myPassBn.setFont(new Font("/u65b0/u5b8b/u4f53", Font.PLAIN, 12));
}
return myPassBn;
}
/**
* This method initializes myPassTF
*
* @return javax.swing.JPasswordField
*/
private JPasswordField getMyPassTF() {
if (myPassTF == null) {
myPassTF = new JPasswordField();
myPassTF.setColumns(50);
myPassTF.setEchoChar('*');
}
return myPassTF;
}
/**
* This method initializes contentSP
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getContentSP() {
if (contentSP == null) {
contentSP = new JScrollPane();
contentSP.setViewportView(getContenTA());
}
return contentSP;
}
/**
* This method initializes contenTA
*
* @return javax.swing.JTextArea
*/
private JTextArea getContenTA() {
if (contenTA == null) {
contenTA = new JTextArea();
contenTA.setText("");
}
return contenTA;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MailServer thisClass = new MailServer();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public MailServer() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(800, 500);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
gridBagConstraints41.fill = GridBagConstraints.BOTH;
gridBagConstraints41.gridy = 6;
gridBagConstraints41.weightx = 1.0;
gridBagConstraints41.weighty = 1.0;
gridBagConstraints41.gridx = 0;
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridheight = 5;
gridBagConstraints1.insets = new Insets(0, 0, 15, 0);
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.ipadx = 0;
gridBagConstraints.gridheight = 1;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.insets = new Insets(0, 0, 10, 0);
gridBagConstraints.gridwidth = GridBagConstraints.REMAINDER;
jContentPane = new JPanel();
jContentPane.setLayout(new GridBagLayout());
jContentPane.setBorder(BorderFactory
.createLineBorder(Color.gray, 1));
jContentPane.add(getFP(), gridBagConstraints);
jContentPane.add(getSP(), gridBagConstraints1);
jContentPane.add(getContentSP(), gridBagConstraints41);
}
return jContentPane;
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(myEmail, password);
}
}
private class cBn implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
myEmail = myAddressTF.getText();
content = contenTA.getText();
//password = myPassTF.getPassword().toString();
password = new String(myPassTF.getPassword());
recieverEmail = rcTF.getText();
System.out.println(myPassTF.getPassword());
System.out.println(password);
System.out.println("cBn");
// TODO Auto-generated method stub
if (e.getActionCommand() == "send"){
try {
Properties props = new Properties();
props.put("mail.smtp.user", myEmail);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", port);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// session.setDebug(true);
msg = new MimeMessage(session);
msg.setText(content);
msg.setSubject(subject);
// System.out.println("mail");
msg.setFrom(new InternetAddress(myEmail));
System.out.println("mail");
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
recieverEmail));
System.out.println("mail");
} catch (Exception mex) {
mex.printStackTrace();
}
Transport.send(msg);
} catch (MessagingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
} // @jve:decl-index=0:visual-constraint="27,22"