`
dreamoftch
  • 浏览: 496011 次
  • 性别: 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;
	}
	
}

 

分享到:
评论

相关推荐

    java swing截图发送 电子邮件

    本篇文章将深入探讨如何在Java Swing应用中实现截图并将其通过电子邮件发送的功能。 首先,我们需要了解Java Swing的基本组件和事件处理。`Robot`类是Java AWT库的一部分,可以用来模拟用户的键盘和鼠标操作,包括...

    java Swing 邮件发送(群发)

    通过上述知识点,你将能够利用Java Swing创建一个用户友好的邮件发送界面,并结合JavaMail API实现邮件的群发功能。确保在实际项目中遵循最佳实践,如安全地存储敏感信息,处理可能出现的异常,以及提供良好的用户...

    基于Java 实现电子邮件收发系统,用JavaSwing来设计图形化界面

    JavaMail API提供了一系列的类和接口,如`Session`用于配置邮件会话,`MimeMessage`用于创建和操作MIME格式的邮件,以及`Transport`用于发送邮件等。 描述中提到的“用JavaSwing来设计图形化界面”意味着这个项目...

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

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

    java的swt界面邮件发送软件

    Java的SWT界面邮件发送软件是一款使用Java编程语言开发的应用程序,它利用了SWT(Standard Widget Toolkit)库来创建用户界面,同时实现了邮件发送功能。SWT是Eclipse项目的一部分,提供了一套与操作系统原生GUI控件...

    java邮件发送客户端

    为了实现图形化用户界面,开发者通常会使用Java Swing或JavaFX库来创建窗口、按钮、输入框等组件,让用户能够直观地填写邮件信息并触发发送操作。此外,还可以集成其他功能,如邮件模板、联系人管理、日历同步等,以...

    精选_基于java.swing实现的发送邮件的小程序_源码打包

    【标题】"精选_基于java.swing实现的发送邮件的小程序_源码打包"指的是一个使用Java编程语言,特别是Swing库开发的桌面应用程序,它的主要功能是发送电子邮件。Swing是Java SE的一部分,提供了一组丰富的组件和工具...

    Java实现e-mail邮件客户端(仅支持发送邮件)

    本项目“Java实现e-mail邮件客户端”专注于发送邮件功能,它利用Java的图形用户界面(GUI)设计,让用户能够方便地输入邮件信息并发送。 首先,我们需要了解JavaMail API的核心组件: 1. **Properties对象**:存储...

    swing发邮件客户端

    - Simple Mail Transfer Protocol是用于发送邮件的网络协议。Swing客户端通过SMTP连接邮件服务器,完成邮件发送。 4. **MIME类型**: - MIME定义了多种非ASCII字符集和非文本数据的表示方式,使得邮件可以包含...

    java邮件收发系统

    在Java邮件收发系统中,Swing可以用来设计和实现类似于Outlook的用户界面,包括邮件列表、撰写窗口、收件箱、草稿箱、已发送邮件等视图。 为了实现邮件的发送,开发者需要配置JavaMail Session,提供SMTP服务器的...

    java实现简单邮件程序

    通过这个示例,读者可以学习到如何利用Java进行网络编程,尤其是如何与SMTP服务器交互来发送邮件。 #### 二、项目背景与目的 本项目来源于《编程蜘蛛、机器人和聚合器》一书,作者为Jeff Heaton。本书中的示例代码...

    javaswing企业快信系统源代码

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

    Java发送普通邮件

    JavaMail API是一个开源的Java库,它允许开发者通过SMTP(简单邮件传输协议)或其他邮件协议发送邮件。在提供的压缩包中,包含了两个重要的jar文件——javax.mail-1.6.2.jar和activation-1.1.jar,它们分别是...

    java 发送邮件项目源码

    Java发送邮件项目源码主要涉及了JavaMail API的使用,SMTP(Simple Mail Transfer Protocol)协议以及DNS(Domain Name System)服务。在这个项目中,开发者创建了一个用户友好的图形化界面,用户可以通过该界面来...

    java版电子邮件系统

    主要包括`javax.mail`和`javax.mail.internet`两个主要包,它们涵盖了SMTP(简单邮件传输协议)用于发送邮件,IMAP(因特网消息访问协议)和POP3(邮局协议)用于接收邮件。 2. MIME(多用途互联网邮件扩展):MIME...

    简单的Java可视化邮件发送Demo

    这个"简单的Java可视化邮件发送Demo"显然演示了如何利用JavaMail API来创建一个简单的邮件客户端,允许用户通过图形界面(GUI)发送带有附件的邮件。 首先,JavaMail API 包含了 javax.mail 和 javax.mail.internet...

    java邮件收发客户端

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

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

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

    Java开发的邮件代理服务器

    这可能涉及Java Swing或JavaFX库来构建图形界面,或者使用Spring Boot和Thymeleaf等技术开发Web应用。 8. 性能优化:使用Java的缓存机制(如Guava Cache)可以缓存频繁访问的数据,减少不必要的网络请求。此外,...

Global site tag (gtag.js) - Google Analytics