·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » JAVAMAIL实现SMTP身份认证

JAVAMAIL实现SMTP身份认证

类别: JSP教程  评论数:0 总得分:0
现在大多数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()主类,再加入发送附件和群发邮件的功能,那么将是一个完整的邮件发送客户端程序。 
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1