`
dreamoftch
  • 浏览: 486483 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java swing发送邮件(界面比较丑。。。)

阅读更多
package com.lubansoft.email.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

public class EmailClient extends JFrame{
	private static final long serialVersionUID = 1L;
	private String subject ;
	private final String FILE_PATH = "userInfo.properties";
	
	//“今日工作”
	private JLabel label_first;
	private JTextArea area_first;
	private JTextArea area_relative_person_first;
	private JTextArea area_time_cost_first;
	
	private JButton send_default_button;
	private JButton send_specify_button;
	private JButton updateUserInfo_button;
	private JScrollPane scrollPane;
	private Container content;
	private GridBagConstraints gbc_la_lineNo = null;
	private final int ROWS = 3;
	private final int JOB_COLS = 20;
	private final int COLS = 8;
	private String username ;
	private String password;
	private String server_host ;
	private String show_name;
	private String recipient;
	private UserInfoFrame userInfoFrame = null; 
	private SimpleDateFormat format = null;
	private EmailActionListener emailActionListener;
	private Date date;
	private JTextField field_xplanner;
	private int width = 1400;
	private int height = 530;
	private String[] top_titles ;
	private String[] bottom_titles ;
	private JPanel panel ;
	private Map<Integer,List<Component>> map= null;
	private List<Component> rowComponents = null;
	private int top_row_count = 2;
	private int bottom_row_count = 2;
	private String[] rowNum;
	
	/**
	 * 
	 * <p>Discription:EmailClient类的构造方法</p>
	 * @coustructor 方法.
	 */
	public EmailClient(){
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setTitle("工作日志邮件");
		content = this.getContentPane();
		initComponent(content);
		this.pack();
		setPositionCenter();
		this.setVisible(true);
	}
	
	private void setPositionCenter() {
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		int width = this.width;
		int height = this.height;
		this.setBounds((d.width - width) / 2, (d.height - height) / 2, width, height);
	}

	private void initComponent(Container content) {
		panel = new JPanel();
		content.setLayout(new BorderLayout());
		GridBagLayout gridBagLayout = new GridBagLayout();
		panel.setLayout(gridBagLayout);
		add(panel, BorderLayout.CENTER);
		/**
		 * 通过循环来添加所有的组件
		 */
		top_titles = new String[]{"今日工作:","系统化工作:"};
		rowNum = new String[]{"一","二","三","四","五","六"};
		for(int i=0;i<top_row_count;i++){
			addCellComponent(panel, rowNum[i], top_titles, i+1);
		}
		bottom_titles = new String[]{"配合事项:","明日计划:"};
		for(int i=0;i<bottom_row_count;i++){
			addCellComponent(panel, rowNum[i], bottom_titles, i+7);
		}
		
		JLabel label_xplanner = new JLabel(" 编辑Xplanner:");
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.EAST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 1;
		gbc_la_lineNo.gridy = 0;
		panel.add(label_xplanner, gbc_la_lineNo);
		
		field_xplanner = new JTextField(JOB_COLS);
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.WEST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 2;
		gbc_la_lineNo.gridy = 0;
		panel.add(field_xplanner, gbc_la_lineNo);
//添加borderlayout.south
		JLabel notice = new JLabel("如需修改发送日期,请先设置日期后在发送(默认为当天时间)");
		send_default_button = new JButton(" 发 送 ");
		send_specify_button = new JButton(" 设置日期");   //" 发送(指定日期)"
		send_default_button.setActionCommand("send_email");
		JPanel south = new JPanel();
		south.setLayout(new FlowLayout());
		south.add(notice);
		south.add(send_default_button);
		south.add(send_specify_button);
		this.add(south,BorderLayout.SOUTH);
		emailActionListener = new EmailActionListener();
		send_default_button.addActionListener(emailActionListener);
		send_specify_button.addActionListener(new CopyOfDateChooserJButton(send_specify_button,this));

		updateUserInfo_button = new JButton(" 修改邮箱信息 "); 
		JButton test = new JButton("增加一行今日工作&系统化工作");
		test.setActionCommand("top_add");
		JButton test2 = new JButton("增加一行配合事项&明日计划");
		test2.setActionCommand("bottom_add");
		test.addActionListener(new EmailActionListener());
		test2.addActionListener(new EmailActionListener());
		updateUserInfo_button.addActionListener(new EmailActionListener());
		updateUserInfo_button.setActionCommand("updateUserInfo");
		south.add(updateUserInfo_button);
		south.add(test);
		south.add(test2);
	}
	/**
	 * 
	 *  Created on 2013-4-25 
	 * <p>Discription:每次添加一行元素</p>
	 * @author:
	 * @param titles标签内容 
	 * @param panel要添加的容器
	 * @param row添加到哪一行
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @return void
	 */
	public void addCellComponent(Container panel,String sequence,String[] titles,int row){
		rowComponents = new ArrayList<Component>();
		label_first = new JLabel(sequence);
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.EAST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 0;
		gbc_la_lineNo.gridy = row;
		panel.add(label_first, gbc_la_lineNo);

		JLabel index = new JLabel(titles[0]);
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.EAST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 1;
		gbc_la_lineNo.gridy = row;
		panel.add(index, gbc_la_lineNo);
		
		area_first = new JTextArea();
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 5, 5, 5);
		constraints.gridx = 2;
		constraints.gridy = row;
		area_first.setColumns(JOB_COLS);
		area_first.setRows(ROWS);
		area_first.setLineWrap(true);
		scrollPane = new JScrollPane(area_first);
		panel.add(scrollPane, constraints);
		rowComponents.add(area_first);
		
		label_first = new JLabel("相关人:");
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.WEST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 3;
		gbc_la_lineNo.gridy = row;
		panel.add(label_first, gbc_la_lineNo);
		
		area_relative_person_first = new JTextArea(ROWS,COLS);
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 5, 5, 5);
		constraints.gridx = 4;
		constraints.gridy = row;
		scrollPane = new JScrollPane(area_relative_person_first);
		panel.add(scrollPane, constraints);
		rowComponents.add(area_relative_person_first);
		
		label_first = new JLabel("用时:");
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.WEST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 5;
		gbc_la_lineNo.gridy = row;
		panel.add(label_first, gbc_la_lineNo);
		
		area_time_cost_first = new JTextArea(ROWS,COLS);
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 5, 5, 5);
		constraints.gridx = 6;
		constraints.gridy = row;
		scrollPane = new JScrollPane(area_time_cost_first);
		panel.add(scrollPane, constraints);
		rowComponents.add(area_time_cost_first);
		
//右边的部分
		index = new JLabel(titles[1]);
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.EAST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 7;
		gbc_la_lineNo.gridy = row;
		panel.add(index, gbc_la_lineNo);
		
		area_first = new JTextArea();
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 5, 5, 5);
		constraints.gridx = 8;
		constraints.gridy = row;
		area_first.setColumns(JOB_COLS);
		area_first.setRows(ROWS);
		area_first.setLineWrap(true);
		scrollPane = new JScrollPane(area_first);
		panel.add(scrollPane, constraints);
		rowComponents.add(area_first);
		
		label_first = new JLabel("相关人:");
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.WEST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 9;
		gbc_la_lineNo.gridy = row;
		panel.add(label_first, gbc_la_lineNo);
		
		area_relative_person_first = new JTextArea(ROWS,COLS);
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 5, 5, 5);
		constraints.gridx = 10;
		constraints.gridy = row;
		scrollPane = new JScrollPane(area_relative_person_first);
		panel.add(scrollPane, constraints);
		rowComponents.add(area_relative_person_first);
		
		label_first = new JLabel("用时:");
		gbc_la_lineNo = new GridBagConstraints();
		gbc_la_lineNo.anchor = GridBagConstraints.WEST;
		gbc_la_lineNo.insets = new Insets(0, 5, 5, 5);
		gbc_la_lineNo.gridx = 11;
		gbc_la_lineNo.gridy = row;
		panel.add(label_first, gbc_la_lineNo);
		
		area_time_cost_first = new JTextArea(ROWS,COLS);
		constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets = new Insets(0, 5, 5, 5);
		constraints.gridx = 12;
		constraints.gridy = row;
		scrollPane = new JScrollPane(area_time_cost_first);
		panel.add(scrollPane, constraints);
		rowComponents.add(area_time_cost_first);
		if(map == null){
			map = new HashMap<Integer, List<Component>>();
		}
		map.put(row, rowComponents);
	}
	
	/**
	 * 加载用户信息配置文件
	 */
	public void loadUserInfo() {
		Properties properties = new Properties();
		FileInputStream in = null;
		File file = null;
		try {
			file = new File(FILE_PATH);
			if(!file.exists()){
				file.createNewFile();
			}
			in = new FileInputStream(file);
			properties.load(in);
			username = properties.getProperty("username");
			password = properties.getProperty("password");
			server_host = properties.getProperty("server_host");
			show_name = properties.getProperty("show_name");
			subject = properties.getProperty("subject");
			recipient = properties.getProperty("recipient");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 *  Created on 2013-4-23 
	 * <p>Discription:往subject后面自动添加日期</p>
	 * @author:
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @return String
	 */
	public String getSubject(String subject,Date date){
		subject += "(";
		format = new SimpleDateFormat("yyyy-MM-dd");
		subject += format.format(date == null ? new Date() : date);
		Calendar calendar = Calendar.getInstance();
		if(date != null){
			calendar.setTime(date);
		}
		int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
		switch(week){
			case 1:
				subject+="星期一";break;
			case 2:
				subject+="星期二";break;
			case 3:
				subject+="星期三";break;
			case 4:
				subject+="星期四";break;
			case 5:
				subject+="星期五";break;
			case 6:
				subject+="星期六";break;
			case 0:
				subject+="星期日";break;
		}
		subject += ")";
		return subject;
	}
	
	public static void main(String[] args) {
		new EmailClient();
	}

	class EmailActionListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				if("send_email".equals(e.getActionCommand())){
					sendEmail("default");
				}else if("updateUserInfo".equals(e.getActionCommand())){
					EmailClient.this.setVisible(false);
					getUserInfoFrame().setVisible(true);
					getUserInfoFrame().initTextValue();
					return ;
				}else if("top_add".equals(e.getActionCommand())){
					if(top_row_count >=6){
						return;
					}
					addCellComponent(panel, rowNum[top_row_count], top_titles, top_row_count+1);
					height += 30;
					EmailClient.this.setSize(width,height);
					setPositionCenter();
					EmailClient.this.validate();
					top_row_count++;
				}
				else if("bottom_add".equals(e.getActionCommand())){
					if(bottom_row_count >=6){
						return;
					}
					addCellComponent(panel, rowNum[bottom_row_count], bottom_titles, bottom_row_count+7);
					EmailClient.this.setSize(EmailClient.this.getWidth(), EmailClient.this.getHeight()+30);
					EmailClient.this.validate();
					bottom_row_count++;
				}
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}
	/**
	 * 
	 *  Created on 2013-4-22 
	 * <p>Discription:点击邮件时调用的方法</p>
	 * @author:
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @return void
	 */
	public void sendEmail(String dateType) throws Exception{
			boolean userInfo_ok = false;
			loadUserInfo();
			if(username != null && !"".equals(username.trim())){
				if(password != null && !"".equals(password.trim())){
					if(server_host != null && !"".equals(server_host.trim())){
						if(subject != null && !"".equals(subject.trim())){
							if(recipient != null && !"".equals(recipient.trim())){
								userInfo_ok = true;
								if(show_name == null || "".equals(show_name.trim())){
									show_name = username;
								}
							}
						}
					}
				}
			}
			if(!userInfo_ok){
				JOptionPane.showMessageDialog(null, "您的邮箱信息不完整,请先填写邮箱信息", null,  JOptionPane.INFORMATION_MESSAGE);
				this.setVisible(false);
				getUserInfoFrame().setVisible(true);
				getUserInfoFrame().initTextValue();
				return ;
			}
			Properties props = new Properties();
			props.setProperty("mail.smtp.auth", "true");
			props.setProperty("mail.transport.protocol", "smtp");
			props.setProperty("mail.host", server_host);
			Session session = Session.getInstance(props,
					new Authenticator()
					{
						protected PasswordAuthentication getPasswordAuthentication()
						{
							return new PasswordAuthentication(username,password);
						}
					}
			);
			session.setDebug(true);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText(show_name) + "\" <" + username + ">"));
			subject = getSubject(subject,date);
			msg.setSubject(subject);		
			msg.setRecipients(RecipientType.TO, InternetAddress.parse(recipient));
			msg.setContent(getHTMLPart(), "text/html;charset=utf-8");
			
//			Transport.send(msg);  //发送邮件
			
			msg.saveChanges();
			
			OutputStream ips = new FileOutputStream("e:/demo3.eml");//保存到本地,查看显示效果
			msg.writeTo(ips);
			ips.close();
			JOptionPane.showMessageDialog(null, "邮件发送成功", null,  JOptionPane.INFORMATION_MESSAGE);
			System.exit(0);			
	}
	/**
	 * 
	 *  Created on 2013-4-19 
	 * <p>Discription:将用户的输入信息插入到html表格中,并返回该html文件的字符串表示</p>
	 * @author:
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @return String
	 */
	public String getHTMLPart(){
		String[][] content = new String[12][6];
		for(int i=0;i<12;i++){
			Arrays.fill(content[i], "&nbsp;");
		}
		Component component = null;
		int index = -1;
		for(Map.Entry<Integer, List<Component>> row: map.entrySet()){
			rowComponents = row.getValue();
			index = row.getKey();
			for(int i = 0;i<rowComponents.size();i++){
				component = rowComponents.get(i);
				if(component instanceof JTextField){
					content[index-1][i] = "".equals(((JTextField)component).getText().trim()) ? "&nbsp;" : ((JTextField)component).getText();
				}else if(component instanceof JTextArea){
					content[index-1][i] = "".equals(((JTextArea)component).getText().trim()) ? "&nbsp;" : ((JTextArea)component).getText();
				}
			}
		}
		return EmailUtils.getHtmlPart(content,field_xplanner.getText());
	}
	
	public String getText(JTextComponent area){
		return area.getText() == null || area.getText().trim().equals("") ? "&nbsp;" :area.getText().trim();
	}
	
	/**
	 * 
	 *  Created on 2013-4-22 
	 * <p>Discription:获得userinfoframe对象</p>
	 * @author:
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @return UserInfoFrame
	 */
	public UserInfoFrame getUserInfoFrame(){
		if(userInfoFrame == null){
			userInfoFrame = new UserInfoFrame();
			return userInfoFrame;
		}else{
			return userInfoFrame;
		}
	}
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	/**
	 * 
	* <p>类名称:UserInfoFrame  </p>
	* <p>类描述:   邮箱信息frame</p>
	* <p>创建人:
	* <p>创建时间:2013-4-22 下午01:47:57   </p>
	* <p>修改人:Administrator   </p>
	* <p>修改时间:2013-4-22 下午01:47:57   </p>
	* <p>修改备注:   </p>
	*<p> @version    </p>
	*
	 */
	class UserInfoFrame extends JFrame{
		private static final long serialVersionUID = 1L;
		private JLabel label_username = new JLabel("    邮箱账户名:");
		private JLabel label_password = new JLabel("        邮箱密码:");
		private JLabel label_server_host = new JLabel("    邮箱服务器:");
		private JLabel label_show_name = new JLabel("        邮箱昵称:");
		private JLabel label_subject= new JLabel("      邮件主题: ");
		private JLabel label_recipient= new JLabel("    收件人邮箱:");
		private JTextField field_recipient = new JTextField(20);
		private JTextField field_subject = new JTextField(20);
		private JTextField field_username = new JTextField(20);
		private JTextField field_password = new JTextField(20);
		private JTextField field_server_host = new JTextField(20);
		private JTextField field_show_name = new JTextField(20);
		private JButton confirm = new JButton(" 完 成 ");
		private JButton reset = new JButton(" 清 空 ");
		private JButton goEmail = new JButton(" 写邮件 ");
		JPanel jPanel = null;
		FlowLayout flow = new FlowLayout();

		
		public UserInfoFrame(){
			this.setDefaultCloseOperation(EXIT_ON_CLOSE);
			this.setTitle("邮箱账户信息");
			content = this.getContentPane();
			initUserComponent(content);
			this.pack();
			Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
			int width = 600;
			int height = 330;
			this.setBounds((d.width - width) / 2, (d.height - height) / 2, width, height);
			this.setVisible(true);
		}

		private void initUserComponent(Container content) {
			loadUserInfo(); //加载邮箱信息
			content.setLayout(new GridLayout(7, 1));
			jPanel = new JPanel(flow);
			jPanel.add(label_subject);
			jPanel.add(field_subject);
			content.add(jPanel);
			jPanel = new JPanel(flow);
			jPanel.add(label_username);
			jPanel.add(field_username);
			content.add(jPanel);
			jPanel = new JPanel(flow);
			jPanel.add(label_recipient);
			jPanel.add(field_recipient);
			content.add(jPanel);
			jPanel = new JPanel(flow);
			jPanel.add(label_password);
			jPanel.add(field_password);
			content.add(jPanel);
			jPanel = new JPanel(flow);
			jPanel.add(label_server_host);
			jPanel.add(field_server_host);
			content.add(jPanel);
			jPanel = new JPanel(flow);
			jPanel.add(label_show_name);
			jPanel.add(field_show_name);
			content.add(jPanel);
			JPanel left = new JPanel();
			left.setLayout(flow);
			left.add(confirm);
			left.add(reset);
			left.add(goEmail);
			confirm.setActionCommand("confirm");
			reset.setActionCommand("reset");
			goEmail.setActionCommand("goEmail");
			goEmail.addActionListener(new EmailActionListener2());
			confirm.addActionListener(new EmailActionListener2());
			reset.addActionListener(new EmailActionListener2());
			content.add(left);
			initTextValue();
		}
		public void initTextValue(){
			field_subject.setText(subject);
			field_username.setText(username);
			field_password.setText(password);
			field_server_host.setText(server_host);
			field_show_name.setText(show_name);
			field_recipient.setText(recipient);
		}
		class EmailActionListener2 implements ActionListener{
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					if("confirm".equals(e.getActionCommand())){
						username = field_username.getText().trim();
						password = field_password.getText().trim();
						server_host = field_server_host.getText().trim();
						show_name =  field_show_name.getText().trim();
						subject =  field_subject.getText().trim();
						recipient = field_recipient.getText().trim();
						if("".equals(show_name)){
							show_name = username;
						}
						if("".equals(username) || "".equals(password) || "".equals(server_host) || "".equals(subject) || "".equals(recipient)){
							JOptionPane.showMessageDialog(null, "请将信息填写完整", "",  JOptionPane.ERROR_MESSAGE);
						}else{
							saveUserInfo();
							JOptionPane.showMessageDialog(null, "保存成功", null,  JOptionPane.INFORMATION_MESSAGE);
							UserInfoFrame.this.setVisible(false);
							EmailClient.this.setVisible(true);
						}
					}else if("reset".equals(e.getActionCommand())){
						reset();
					}else if("goEmail".equals(e.getActionCommand())){
						UserInfoFrame.this.setVisible(false);
						EmailClient.this.setVisible(true);
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}

			/**
			 * 
			 *  Created on 2013-4-22 
			 * <p>Discription:保存用户信息</p>
			 * @author:
			 * @update:[日期YYYY-MM-DD] [更改人姓名]
			 * @return void
			 */
			private void saveUserInfo() {
				FileInputStream in = null;
				FileOutputStream out = null;
				Properties properties = null;
				File file = new File(FILE_PATH);
				try {
					if(file.exists()){
						file.delete();
					}
					file.createNewFile();
					in = new FileInputStream(FILE_PATH);
					properties = new Properties();
					properties.load(in);
					properties.setProperty("username", "".equals(username.trim()) ? properties.getProperty("username") : username);
					properties.setProperty("password", "".equals(password.trim()) ? properties.getProperty("password") : password);
					properties.setProperty("server_host", "".equals(server_host.trim()) ? properties.getProperty("server_host") : server_host);
					properties.setProperty("show_name", "".equals(show_name.trim()) ? properties.getProperty("show_name") : show_name);
					properties.setProperty("subject", "".equals(subject.trim()) ? properties.getProperty("subject") : subject);
					properties.setProperty("recipient", "".equals(recipient.trim()) ? properties.getProperty("recipient") : recipient);
					out = new FileOutputStream(FILE_PATH);
					properties.store(out, "store key value");
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					try {
						in.close();
						out.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			private void reset() {
				field_subject.setText("");
				field_username.setText("");
				field_password.setText("");
				field_server_host.setText("");
				field_show_name.setText("");
				field_recipient.setText("");
			}
		}
	}
	
	
}

 

另外一个类:

package com.lubansoft.email.swing;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class EmailUtils {


	
	public static String getHtmlPart(String[][] content,String xplanner){
		String html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" + 
"<html>" + 
  "<head>" + 
    "<title>Email.html</title>" + 
    "<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">" + 
    "<meta http-equiv=\"description\" content=\"this is my page\">" + 
    "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">" + 
    "<style>" + 
	"td,tr,th,table{text-align:center;border-color:black;border-width:1px;}" + 
	"</style>" + 
  "</head>" + 
  "<body>" + 
  "编辑Xplanner : " + xplanner +
    "<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\" width=\"800px\"  rules=\"all\">" + makeHTML(content)+
    "</table>" + 
  "</body>" + 
"</html>";
		File file = null;
		FileOutputStream out = null;
		ByteArrayInputStream in = null;
		try {
			file = new File("e:email.html");
			if(!file.exists()){
				file.createNewFile();
			}
			out = new FileOutputStream(file);
			in = new ByteArrayInputStream(html.getBytes("utf-8"));
			byte[] b = new byte[1024];
			int i = -1;
			while((i = in.read(b)) != -1){
				out.write(b, 0, i);
			}
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return html;
	}
	/**
	 * 
	 *  Created on 2013-4-25 
	 * <p>Discription:将数组转化为html代码</p>
	 * @author:
	 * @update:[日期YYYY-MM-DD] [更改人姓名]
	 * @return String
	 */
	public static String makeHTML(String[][] content){
		String result = "";
		int length = content.length;
		for(int i=0;i<length;i++){
			if(i==0){
				result += "<tr>" + 
	    		"<th style=\"width:50px;\">序号</th>" + 
				"<th style=\"width:200px;color:red;\">今日工作</th>" + 
				"<th style=\"width:70px;\">相关人</th>" + 
				"<th style=\"width:80px;\">用时</th>" + 
				"<th style=\"width:50px;\">序号</th>" + 
				"<th style=\"width:200px;color:red;\">系统化工作(流程、新技术、学习)</th>" + 
				"<th style=\"width:70px;\">相关人</th>" + 
				"<th style=\"width:80px;\">用时</th>" + 
			"</tr>" ;
			}else if(i == 6){
				result += "<tr>" + 
	    		"<th>序号</th>" + 
				"<th style=\"color:red;\">配合事项</th>" + 
				"<th>相关人</th>" + 
				"<th>用时</th>" + 
				"<th>序号</th>" + 
				"<th style=\"color:red;\">明日计划</th>" + 
				"<th>相关人</th>" + 
				"<th>用时</th>" + 
			"</tr>";
			}
			result += "<tr>" ;
			if(i<6){					
				result += "<td>"+(i+1)+"</td>" ;
			}else{
				result += "<td>"+(i-5)+"</td>" ;
			}
			result += "<td style=\"word-break:break-all;\">" + content[i][0] + "</td>" + 
			"<td style=\"word-break:break-all;\">" + content[i][1] + "</td>" + 
			"<td style=\"word-break:break-all;\">" + content[i][2] + "</td>" ;
			if(i<6){					
				result += "<td>"+(i+1)+"</td>" ;
			}else{
				result += "<td>"+(i-5)+"</td>" ;
			}
			result += "<td style=\"word-break:break-all;\">" + content[i][3] + "</td>" + 
			"<td style=\"word-break:break-all;\">" + content[i][4] + "</td>" + 
			"<td style=\"word-break:break-all;\">" + content[i][5] + "</td>" + 
		"</tr>";
		}
		return result;
	}
	
}

 

分享到:
评论

相关推荐

    javamail开发的简单的邮件收发,SWing做为前端界面

    javamail开发的简单的邮件收发,SWing做为前端界面。能够支持多种邮箱,由于有几种邮箱协议不是很清楚,可能有问题。只需简单修改就能适用另外的邮箱。如有问题,可与本人联系。本人QQ 573270725 内附带一邮件收发...

    java邮件收发客户端

    java实现的邮件收发系统,用swing开发了界面

    基于 Java Swing + 人脸识别编写的学校考试系统源代码Java基础大作业

    利用 Java Mail 实现发送邮件 用户密码采用 MD5 加盐加密 利用百度AI的人脸识别接口整合Java Swing 实现了人脸识别 利用 Spire 类库实现校园卡上的条形码识别 利用 JFreeChart 将学生成绩等导出为图表 利用阿里巴巴...

    Java大作业基于 Java Swing + 人脸识别 编写的学校考试系统源码

    利用 Java Mail 实现发送邮件 用户密码采用 MD5 加盐加密 利用百度AI的人脸识别接口整合Java Swing 实现了人脸识别 利用 Spire 类库实现校园卡上的条形码识别 利用 JFreeChart 将学生成绩等导出为图表 利用...

    javaswing企业快信系统源代码

    javaswing企业快信系统源代码 企业快信的作用是帮助企业解决企业内部、企业与外部沟通难、信息不能及时传播等问题。为此,企业快信系统需要提供有邮件群发、短信群发等功能。通过对多数企业日常业务的考察、分析,并...

    完整版 杭州电子科技大学JAVA语言程序设计 JAVA_08 AWT图形界面编程 JFC Swing (共22页).ppt

    JAVA_11网络程序设计\邮件发送源码.rar JAVA_12 Swing\SwingTest.java JAVA_13 JDBC\Connec.java JAVA_13 JDBC\mysql-5.5.11-win32(1).msi JAVA_13 JDBC\SQLyog832Trial.exe JAVA_13 JDBC\SQLyog832Trial.zip JAVA_...

    基于 Java Swing + 人脸识别 实现的学校考试系统

    2. 利用 Java Mail 实现发送邮件 3. 用户密码采用 MD5 加盐加密 4. 利用百度AI的人脸识别接口整合Java Swing 实现了人脸识别 5. 利用 Spire 类库实现校园卡上的条形码识别 6. 利用 JFreeChart 将学生成绩等导出为...

    java实现的邮件发送桌面程序

    使用的IDE是eclipse,导入的jar包只有javax.mail.jar这一个,在jdk1.7的环境下编写并调试的因为对...在用户界面上,会提示输入用户名和密码,当然由于使用的是163的服务器所以这个版本的程序只能使用网易邮箱的账号使用

    完整版 杭州电子科技大学JAVA语言程序设计 JAVA_03 JAVA语言基础(共57页).ppt

    JAVA_11网络程序设计\邮件发送源码.rar JAVA_12 Swing\SwingTest.java JAVA_13 JDBC\Connec.java JAVA_13 JDBC\mysql-5.5.11-win32(1).msi JAVA_13 JDBC\SQLyog832Trial.exe JAVA_13 JDBC\SQLyog832Trial.zip JAVA_...

    完整版 杭州电子科技大学JAVA语言程序设计 JAVA_13 JDBC(共15页).ppt

    JAVA_11网络程序设计\邮件发送源码.rar JAVA_12 Swing\SwingTest.java JAVA_13 JDBC\Connec.java JAVA_13 JDBC\mysql-5.5.11-win32(1).msi JAVA_13 JDBC\SQLyog832Trial.exe JAVA_13 JDBC\SQLyog832Trial.zip JAVA_...

    网络编程实验:邮件接收和发送

    利用javax.mail开发的带界面的邮件发送程序和邮件接收程序。功能丰富,展示界面合理。界面使用swing开发。使用前请在references.java文件中更改你的邮箱地址和密码(网易邮箱使用smtp和pop3需要授权码)

    java源码包---java 源码 大量 实例

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    完整版 杭州电子科技大学JAVA语言程序设计 JAVA_07 IO输入输出(共30页).ppt

    JAVA_11网络程序设计\邮件发送源码.rar JAVA_12 Swing\SwingTest.java JAVA_13 JDBC\Connec.java JAVA_13 JDBC\mysql-5.5.11-win32(1).msi JAVA_13 JDBC\SQLyog832Trial.exe JAVA_13 JDBC\SQLyog832Trial.zip JAVA_...

    JAVA上百实例源码以及开源项目源代码

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    JAVA上百实例源码以及开源项目

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java源码...

    java源码包4

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java...

    java源码包3

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java...

    java源码包2

     Java 3DMenu 界面源码,有人说用到游戏中不错,其实平时我信编写Java应用程序时候也能用到吧,不一定非要局限于游戏吧,RES、SRC资源都有,都在压缩包内。 Java zip压缩包查看程序源码 1个目标文件 摘要:Java...

    java开源包4

    JCalendarButton是一个简单的java swing日历选择控件。它能够在日期输入框后面弹出一个日历。 网页搜索爬虫 BlueLeech BlueLeech是一个开源程序,它从指定的URL开始,搜索所有可用的链接,以及链接之上的链接。它在...

    java开源包11

    JCalendarButton是一个简单的java swing日历选择控件。它能够在日期输入框后面弹出一个日历。 网页搜索爬虫 BlueLeech BlueLeech是一个开源程序,它从指定的URL开始,搜索所有可用的链接,以及链接之上的链接。它在...

Global site tag (gtag.js) - Google Analytics