panel1.add(jLabel1, new XYConstraints(15, 48, -1, -1)); panel1.add(jButton1, new XYConstraints(46, 79, 58, 22)); panel1.add(jButton2, new XYConstraints(112, 79, 58, 22)); panel1.add(jLabel5, new XYConstraints(85, 102, -1, 12)); this.getRootPane().setDefaultButton(jButton1); this.setSize(220,130); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); this.setLocation((int)screenSize.getWidth()/2-80,(int)screenSize.getHeight()/2-50); }
void jButton1_actionPerformed(ActionEvent e) { ok=true; this.dispose(); }
void jButton2_actionPerformed(ActionEvent e) { ok=false; this.dispose(); } public String showDialog(){ show(); if(ok)return new String(jPasswordField1.getPassword()); else return null; } }
class Option_jButton1_actionAdapter implements java.awt.event.ActionListener { Option adaptee;
Option_jButton1_actionAdapter(Option adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton1_actionPerformed(e); } }
class Option_jButton2_actionAdapter implements java.awt.event.ActionListener { Option adaptee;
Option_jButton2_actionAdapter(Option adaptee) { this.adaptee = adaptee; } public void actionPerformed(ActionEvent e) { adaptee.jButton2_actionPerformed(e); } } /*下面写一个发送邮件主类,利用上面的MailAuthenticator类来提供身份验证 */ package jmail;
import javax.mail.*; import javax.mail.internet.*; import javax.swing.*; import java.io.*; import javax.activation.*; import java.util.*;
public class SendMail{
public static void main(String[] args) { String host=""; String from=""; String to=""; String name=""; String pass=null; String subject=""; String content=""; MailAuthenticator ma; if(args.length<6){ /*小于6,先不输入密码,发送过程服务器要求认证时就会跳出密码输入框(Option类)*/ System.out.println("enter<host><from><to><name><pass><subject><content>"); System.exit(0); } else { host=args[0]; /*smtp服务器*/ from=args[1]; /*发件人*/ to=args[2]; /收件人*/ name=args[3]; /*smtp认证用户名,一般跟pop3登录相同*/ subject=args[4]; /*邮件主题*/ content=args[5]; /*邮件内容*/ } try{ ma=new MailAuthenticator(name,pass); Properties props=System.getProperties(); props.put("mail.smtp.host",host); props.put("mail.smtp.auth","true"); /*服务器需要认证*/ Session session=Session.getInstance(props,ma); /*Session会自动调用getPasswordAuthentication()方法*/ MimeMessage msg=new MimeMessage(session); msg.setFrom(new InternetAddress(from)); msg.setRecipient(Message.RecipientType.TO,new InternetAddress(to)); msg.setSubject(subject); msg.setText(content); msg.setSentDate(new java.util.Date()); Transport.send(msg); System.out.println("Send Email Success"); }catch(Exception e){ |