⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagepanel.java

📁 一个java实现的邮件服务器系统 使用邮件客户端工具
💻 JAVA
字号:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.Serializable;
import java.util.*;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

public class MessagePanel extends JPanel {

	private javax.swing.JLabel jLabel1 = null;	//提示信息
	private javax.swing.JLabel jLabel2 = null;	//提示信息
	private javax.swing.JLabel jLabel3 = null;	//提示信息
	private javax.swing.JLabel jLabel5 = null;	//提示信息
	
	private javax.swing.JTextField textInTo	= null;	//目标地址
	private javax.swing.JTextField textInCopyTo	= null;	//抄送地址
	private javax.swing.JTextField textInSubject	= null;	//邮件标题
	private javax.swing.JTextField textInEnclosure	= null;	//邮件附件
	
	private javax.swing.JButton jbtnSend	= null;	//发送邮件
	private javax.swing.JButton jbtnSave	= null;	//保存邮件
	private javax.swing.JButton jbtnClear	= null;	//清除编辑
	private javax.swing.JButton jbtnBrowse	= null;	//浏览文件
	
	private javax.swing.JScrollPane jScrollPane = null;
	private javax.swing.JTextArea jtaInText 	= null;
	private javax.swing.JProgressBar jprgbSend	= null;

	public ViewPanel recievePanel;
	
	Vector attachVector = new Vector();	//附件列表

	chooseFile Jfc = new chooseFile();
	
	/*构造函数*/
	public MessagePanel() {
		try {
			//初始化函数
			initialize();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/*初始化函数*/
	private void initialize() {
		this.setLayout(null);
		this.add(getJLabel1(), null);
		this.add(getJLabel2(), null);
		this.add(getTextInTo(), null);
		this.add(getTextInCopyTo(), null);
		this.add(getJLabel3(), null);
		this.add(getTextInSubject(), null);
		this.add(getJbtnSend(), null);
		this.add(getJbtnSave(), null);
		this.add(getJbtnClear(), null);
		this.add(getJbtnBrowse(), null);
		this.add(getTextInEnclosure(), null);
		this.add(getJLabel5(), null);
		this.add(getJScrollPane(), null);
		this.add(getJprgbSend(), null);
		
		this.setSize(366, 385);	//设置尺寸
		this.setName("MessagePanel");	//设置标题
		this.setToolTipText("Save to the Box");
	}
	
	/**/
	private javax.swing.JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new javax.swing.JLabel();
			jLabel1.setBounds(20, 23, 74, 23);
			jLabel1.setText("To :");
			jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel1.setName("labelTo");
			jLabel1.setHorizontalTextPosition(
				javax.swing.SwingConstants.LEADING);
		}
		return jLabel1;
	}
	
	/**/
	private javax.swing.JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new javax.swing.JLabel();
			jLabel2.setBounds(20, 87, 74, 23);
			jLabel2.setText("Subject:");
			jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel2.setName("labelSubject");
		}
		return jLabel2;
	}
	
	/**/
	private javax.swing.JLabel getJLabel3() {
		if (jLabel3 == null) {
			jLabel3 = new javax.swing.JLabel();
			jLabel3.setBounds(20, 119, 73, 23);
			jLabel3.setText("Enclosure:");
			jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			jLabel3.setName("labelAttechment");
			jLabel3.setToolTipText("");
		}
		return jLabel3;
	}
	
	/**/
	private javax.swing.JLabel getJLabel5() {
		if (jLabel5 == null) {
			jLabel5 = new javax.swing.JLabel();
			jLabel5.setBounds(20, 55, 73, 23);
			jLabel5.setText("Copy To:");
			jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		}
		return jLabel5;
	}
	
	/**/
	private javax.swing.JTextField getTextInTo() {
		if (textInTo == null) {
			textInTo = new javax.swing.JTextField();
			textInTo.setBounds(129, 23, 177, 24);
			textInTo.setName("textTo");
			textInTo.setForeground(java.awt.SystemColor.textHighlight);
		}
		return textInTo;
	}
	
	/**/
	private javax.swing.JTextField getTextInCopyTo() {
		if (textInCopyTo == null) {
			textInCopyTo = new javax.swing.JTextField();
			textInCopyTo.setBounds(129, 55, 177, 24);
			textInCopyTo.setName("textSubject");
			textInCopyTo.setForeground(java.awt.SystemColor.textHighlight);
		}
		return textInCopyTo;
	}
	
	/**/
	private javax.swing.JTextField getTextInSubject() {
		if (textInSubject == null) {
			textInSubject = new javax.swing.JTextField();
			textInSubject.setBounds(129, 87, 177, 24);
			textInSubject.setName("textAttechment");
			textInSubject.setForeground(java.awt.SystemColor.textHighlight);
		}
		return textInSubject;
	}
	
	/**/
	private javax.swing.JButton getJbtnSend() {
		if (jbtnSend == null) {
			jbtnSend = new javax.swing.JButton();
			jbtnSend.setBounds(236, 323, 70, 24);
			jbtnSend.setName("buttonSend");
			jbtnSend.setText("Send");
			jbtnSend.addActionListener(new sendAction());

		}
		return jbtnSend;
	}
	
	/**/
	private javax.swing.JButton getJbtnSave() {
		if (jbtnSave == null) {
			jbtnSave = new javax.swing.JButton();
			jbtnSave.setBounds(74, 323, 70, 24);
			jbtnSave.setText("Save");
			jbtnSave.setName("buttonSave");
			jbtnSave.setToolTipText("Save to Box");
			jbtnSave.addActionListener(new saveAction());

		}
		return jbtnSave;
	}
	
	/**/
	private javax.swing.JButton getJbtnClear() {
		if (jbtnClear == null) {
			jbtnClear = new javax.swing.JButton();
			jbtnClear.setBounds(155, 323, 70, 24);
			jbtnClear.setText("Clear");
			jbtnClear.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					textInTo.setText("");
					jtaInText.setText("");
					textInSubject.setText("");
					textInEnclosure.setText("");
				}
			});
		}
		return jbtnClear;
	}
	
	/**/
	private javax.swing.JButton getJbtnBrowse() {
		if (jbtnBrowse == null) {
			jbtnBrowse = new javax.swing.JButton();
			jbtnBrowse.setBounds(305, 119, 43, 24);
			jbtnBrowse.setText("...");
			jbtnBrowse.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
			jbtnBrowse.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
			jbtnBrowse.setToolTipText("Browse");
			jbtnBrowse.setName("buttonBrowse");
			jbtnBrowse.addActionListener(new dirAction());
		}
		return jbtnBrowse;
	}
	
	/**/
	private javax.swing.JTextField getTextInEnclosure() {
		if (textInEnclosure == null) {
			textInEnclosure = new javax.swing.JTextField();
			textInEnclosure.setBounds(129, 119, 177, 24);
			textInEnclosure.setForeground(java.awt.SystemColor.textHighlight);
		}
		return textInEnclosure;
	}
	
	/**/
	private javax.swing.JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new javax.swing.JScrollPane();
			jScrollPane.setViewportView(getJtaInText());
			jScrollPane.setBounds(23, 152, 322, 165);
		}
		return jScrollPane;
	}
	
	/**/
	private javax.swing.JTextArea getJtaInText() {
		if (jtaInText == null) {
			jtaInText = new javax.swing.JTextArea();
			jtaInText.setForeground(java.awt.SystemColor.textHighlight);
		}
		return jtaInText;
	}
	
	/**/
	private javax.swing.JProgressBar getJprgbSend() {
		if (jprgbSend == null) {
			jprgbSend = new javax.swing.JProgressBar();
			jprgbSend.setBounds(26, 353, 318, 20);
		}
		return jprgbSend;
	}

	//进度条控制类
	class Wait implements Serializable {
		Timer timer = new Timer(40, new Action());	//定时器
		int value = 5;

		public void Start() {
			timer.start();	//启动定时器
		}

		public void Stop() {
			//停止定时器
			timer.stop();
			
			//恢复进度条
			jprgbSend.setValue(0);
		}
		
		//
		class Action implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				if (value >= 200)
					value = 3;
				jprgbSend.setValue(value);
				value += 1;
			}
		}
	}

	Wait wait = new Wait();

	//
	class sendAction implements ActionListener, Serializable {
		public void actionPerformed(ActionEvent e) {
			wait.Start();	//启动进度条
			//
			(new send()).start();
		}
	}

	//身证验证类
	class PopupAuthenticator extends Authenticator {
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(
				Configuration.getAccount(),	//用户帐户
				Configuration.getPassword()	//用户密码
				);
		}
	}
	
	//保存信件类
	class saveAction implements ActionListener, Serializable {
		public void actionPerformed(ActionEvent e) {
			MessageDS savemessage = new MessageDS();
			savemessage.setAddress(textInTo.getText());
			savemessage.setSubject(textInSubject.getText());
			savemessage.setEnclosure(textInEnclosure.getText());
			savemessage.setRecievedDate(new Date());
			savemessage.setContent(jtaInText.getText());

			//
			recievePanel.saveVector.addElement(savemessage);
			//
			DefaultMutableTreeNode messageNode =
				new DefaultMutableTreeNode(
					recievePanel.saveVector.size()
						+ "."
						+ savemessage.getAddress());
			//
			recievePanel.saveModel.insertNodeInto(
				messageNode,
				recievePanel.saveNode,
				recievePanel.saveNode.getChildCount());
			//
			recievePanel.recieveTree.scrollPathToVisible(
				new TreePath(messageNode.getPath()));

			JOptionPane.showMessageDialog(null, " Save to SAVED-Box");
		}
	}

	//邮件发送线程类
	class send extends Thread implements Serializable {
		public void run() {
			//得到SMTP服务器
			String SMTPServer = Configuration.getSMTP();
			Session mailSession = null;
			
			try {
				Properties props = new Properties();
				props.put("mail.smtp.host", SMTPServer);
				props.put("mail.smtp.auth", "true");

				if (Configuration.getBAuthenticateNeeded())	//如果需要身份验证
				{
					mailSession =
						Session.getInstance(props, new PopupAuthenticator());
				}
				else	//如果不需要身份验证
				{
					mailSession = Session.getInstance(props, null);
				} 

				Message mailMessage = new MimeMessage(mailSession);
				
				//邮件发送日期
				mailMessage.setSentDate(new Date());
				//邮件回复地址
				mailMessage.setFrom(
					new InternetAddress(
						Configuration.getAccount()
							+ "@"
							+ SMTPServer.substring(5)));
				//邮件接收地址
				mailMessage.setRecipient(
					Message.RecipientType.TO,
					new InternetAddress(textInTo.getText()));
				//邮件标题
				mailMessage.setSubject(textInSubject.getText());
				
				//判断是否有附件
				if (attachVector.size() >= 1) //如果有附件
				{	
					Multipart mp = new MimeMultipart();
					MimeBodyPart mbp = new MimeBodyPart();
					
					mbp.setContent(
						(jtaInText.getText()).toString(),
						"text/plain;charset=Gb2312");
						
					mp.addBodyPart(mbp);
					
					//依次准备邮件附件
					for (int i = 0; i < attachVector.size(); i++) 
					{
						MimeBodyPart enclosureMbp = new MimeBodyPart();
						FileDataSource fds =
							new FileDataSource(
								((File) (attachVector.elementAt(i)))
									.toString());
						enclosureMbp.setDataHandler(new DataHandler(fds));
						enclosureMbp.setFileName(fds.getName());
						mp.addBodyPart(enclosureMbp);
					}

					mailMessage.setContent(mp);
				} 
				else //如果没有附件
				{	
					mailMessage.setContent(
						(jtaInText.getText()).toString(),
						"text/plain;charset=Gb2312");
				}

				//发送邮件
				Transport.send(mailMessage);

				//恢复界面
				textInTo.setText("");
				jtaInText.setText("");
				textInSubject.setText("");
				textInEnclosure.setText("");
				
				//清空附件列表
				attachVector.removeAllElements();
				
				wait.Stop();	//停止进度条

				//显示提示信息
				JOptionPane.showMessageDialog(
					null,
					" Mail Sended successfully");

			} catch (Exception e) {
				//停止进度条
				wait.Stop();
				
				//恢复界面
				textInTo.setText("");
				jtaInText.setText("");
				textInSubject.setText("");
				textInEnclosure.setText("");
				
				//清除附件列表
				attachVector.removeAllElements();
				
				e.printStackTrace();
				
				//显示提示信息
				JOptionPane.showMessageDialog(
					null,
					e.getMessage() + "exception here");
			}
		}
	}

	//
	class dirAction implements ActionListener, Serializable {
		public void actionPerformed(ActionEvent e) {
			//
			Jfc.showOpenDialog(new JFrame());
		}
	}

	class chooseFile extends JFileChooser implements Serializable {
		
		public void approveSelection() {
			//
			attachVector.addElement(this.getSelectedFile());

			if (textInEnclosure.getText() == null)
				textInEnclosure.setText("");

			textInEnclosure.setText(
				textInEnclosure.getText()
					+ (this.getSelectedFile()).toString()
					+ ";");

			getTopLevelAncestor().setVisible(false);
		}
		
		public void cancelSelection() {
			getTopLevelAncestor().setVisible(false);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -