JAVAMAIL实现SMTP身份认证
类别: JSP教程
现在大多数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() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1.setLayout(xYLayout1);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel1.setText("密码:");
jButton1.setFont(new java.awt.Font("Dialog", 0, 12));
jButton1.setDoubleBuffered(false);
jButton1.setText("确定");
jButton1.addActionListener(new Option_jButton1_actionAdapter(this));
jButton2.setFont(new java.awt.Font("Dialog", 0, 12));
jButton2.setText("取消");
jButton2.addActionListener(new Option_jButton2_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel2.setHorizontalTextPosition(SwingConstants.TRAILING);
jLabel2.setIcon(forget);
jLabel2.setText("");
jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel3.setText("服务器需要身份认证, 请输入密码");
panel1.setToolTipText("");
jLabel4.setText(" ");
jLabel5.setText(" ");
getContentPane().add(panel1, BorderLayout.CENTER);
panel1.add(jLabel4, new XYConstraints(189, 61, -1, -1));
panel1.add(jLabel2, new XYConstraints(15, 1, 67, 19));
panel1.add(jLabel3, new XYConstraints(15, 22, -1, -1));
panel1.add(jPasswordField1, new XYConstraints(48, 46, 140, -1));
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){
System.out.println(e.getMessage());
}
}
}
可以自己写个UI界面的SendMail()主类,再加入发送附件和群发邮件的功能,那么将是一个完整的邮件发送客户端程序。
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() {
this(null, "", false);
}
private void jbInit() throws Exception {
panel1.setLayout(xYLayout1);
jLabel1.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel1.setText("密码:");
jButton1.setFont(new java.awt.Font("Dialog", 0, 12));
jButton1.setDoubleBuffered(false);
jButton1.setText("确定");
jButton1.addActionListener(new Option_jButton1_actionAdapter(this));
jButton2.setFont(new java.awt.Font("Dialog", 0, 12));
jButton2.setText("取消");
jButton2.addActionListener(new Option_jButton2_actionAdapter(this));
jLabel2.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel2.setHorizontalTextPosition(SwingConstants.TRAILING);
jLabel2.setIcon(forget);
jLabel2.setText("");
jLabel3.setFont(new java.awt.Font("Dialog", 0, 12));
jLabel3.setText("服务器需要身份认证, 请输入密码");
panel1.setToolTipText("");
jLabel4.setText(" ");
jLabel5.setText(" ");
getContentPane().add(panel1, BorderLayout.CENTER);
panel1.add(jLabel4, new XYConstraints(189, 61, -1, -1));
panel1.add(jLabel2, new XYConstraints(15, 1, 67, 19));
panel1.add(jLabel3, new XYConstraints(15, 22, -1, -1));
panel1.add(jPasswordField1, new XYConstraints(48, 46, 140, -1));
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){
System.out.println(e.getMessage());
}
}
}
可以自己写个UI界面的SendMail()主类,再加入发送附件和群发邮件的功能,那么将是一个完整的邮件发送客户端程序。
- 上一篇: 用JAVAMAIL显示复合邮件的内容(3)
- 下一篇: DATE类的简单应用
-= 资 源 教 程 =-
文 章 搜 索