javamail实现smtp身份认证

作者:袖梨 2022-07-02
现在大多数smtp服务器都需要身份认证,若用javamail写一个邮件收发客户端,怎么来实现这种功能呢?下面是一个简单的列子,模拟foxmail对认证功能的实现。
package jmail;
import javax.mail.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import java.awt.*;
public class MailAuthenticator extends Authenticator{
String authenName; //用户名
String authenPass; //密码
public MailAuthenticator(String authenName,String authenPass) {
super();
this.authenName=authenName;
this.authenPass=authenPass;
}
public PasswordAuthentication getPasswordAuthentication(){ /*若服务器需要身份认证,Sission会自动调用这个方法
String temp=null;
if(authenPass.equals("")||authenPass==null){ /*若密码为空*/
Option op=new Option(null,"身份验证",true); /*弹出要求用户输入密码的对话框,Option是自定义的JDialog,包含一个密码域*/
temp=op.showDialog(); /*Option的返回输入的密码*/
authenPass=temp;
}
return new PasswordAuthentication(authenName,authenPass);
}
}
/* 下面是Option 类,jbuilder里写的,无须解释*/
package jmail;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
public class Option extends JDialog {
JPanel panel1 = new JPanel();
JLabel jLabel1 = new JLabel();
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JPasswordField jPasswordField1 = new JPasswordField();
JLabel jLabel2 = new JLabel();
JLabel jLabel3 = new JLabel();
Icon forget=new ImageIcon(".imagesforget.gif");
boolean ok=false;
JLabel jLabel4 = new JLabel();
JLabel jLabel5 = new JLabel();
public Option(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public Option() {

相关文章

精彩推荐