Java Mail GUI桌面程序

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.mail.SimpleEmail;

public class Em extends JFrame{
	//7个标签
	JLabel jl1,jl2,jl3,jl4,jl5,jl6,jl7;
	//5个文本框
	JTextField jtf1,jtf2,jtf3,jtf4,jtf5;
	//1个密码框
	JPasswordField jpf;
	//1个文本域
	JTextArea jta;
	//滚动条
	JScrollPane jsp;
	//按钮
	JButton jb1;
	public Em(){
		jl1=new JLabel("邮箱服务器地址:");
		jl1.setBounds(20,20,160,25);
		jtf1=new JTextField();
		jtf1.setBounds(120,20,200,25);
		jl2=new JLabel("收  件  人  邮  箱:");
		jl2.setBounds(20,60,160,25);
		jtf2=new JTextField();
		jtf2.setBounds(120,60,200,25);
		jl3=new JLabel("发  送  人  邮  箱:");
		jl3.setBounds(20,100,160,25);
		jtf3=new JTextField();
		jtf3.setBounds(120,100,200,25);
		jl4=new JLabel("用户姓名:");
		jl4.setBounds(20,140,80,25);
		jtf4=new JTextField();
		jtf4.setBounds(80,140,80,25);
		jl5=new JLabel("用户密码:");
		jl5.setBounds(180,140,80,25);
		jpf=new JPasswordField();
		jpf.setBounds(240,140,80,25);
		jl6=new JLabel("邮   件   标   题:");
		jl6.setBounds(20,180,160,25);
		jtf5=new JTextField();
		jtf5.setBounds(120,180,200,25);
		jl7=new JLabel("内容:");
		jl7.setBounds(20, 220, 80, 25);
		jta=new JTextArea();
		jsp=new JScrollPane(jta);
		jsp.setBounds(20, 260, 300, 120);
		jb1=new JButton("邮件发送");
		jb1.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				do_but(e);
			}
		});
		jb1.setBounds(120,400,120,30);
		this.add(jl1);
		this.add(jtf1);
		this.add(jl2);
		this.add(jtf2);
		this.add(jl3);
		this.add(jtf3);
		this.add(jl4);
		this.add(jtf4);
		this.add(jl5);
		this.add(jpf);
		this.add(jl6);
		this.add(jtf5);
		this.add(jl7);
		this.add(jsp);
		this.add(jb1);
		this.setLayout(null);
		//标题
		this.setTitle("力天邮件发送");
		//大小及位置 
		this.setBounds(100,100,350,500);
		//关闭
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//可见
		this.setVisible(true);
	}
	public void do_but(ActionEvent e){
		SimpleEmail em=new SimpleEmail();
		//邮件设置编码
		em.setCharset("gbk");
		//服务地址
		String hostName=this.jtf1.getText().trim();
		//收件人
		String toEmail=this.jtf2.getText().trim();
		//发件人
		String fromEmail=this.jtf3.getText().trim();
		//用户名
		String uname=this.jtf4.getText().trim();
		//用户密码
		String upwd=this.jpf.getText().trim();
		//邮件标题
		String title=this.jtf5.getText().trim();
		//邮件内容
		String content=this.jta.getText().trim();
		try {
			//服务邮件地址
			em.setHostName(hostName);
			//收件人
			em.addTo(toEmail);
			//发件人
			em.setFrom(fromEmail);
			//用户名与密码
			em.setAuthentication(uname, upwd);
			//邮件标题
			em.setSubject(title);
			//邮件内容
			em.setMsg(content);
			//开始发送
			em.send();
			JOptionPane.showMessageDialog(null,"发送成功!");
		} catch (Exception e2) {
			e2.printStackTrace();
		}
	}
	public static void main(String[] args) {
		new Em();
	}

}

 

你可能感兴趣的:(java mail 邮箱)