Chinaunix首页 | 论坛 | 博客
  • 博客访问: 543533
  • 博文数量: 855
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5005
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-16 19:08
文章分类

全部博文(855)

文章存档

2011年(1)

2008年(854)

我的朋友

分类:

2008-10-16 19:18:40

    1.首先设置属性

     Properties props = new Properties();
  props.put("mail.smtp.host",host);
  props.put("mail.smtp.auth","true");//必须加上true要不然stmp连接的时候不会认证.

    2.写认证类继承自Authenticator:

     import javax.mail.*;
  import javax.mail.internet.*;
    public class MyAuthenticator extends Authenticator
  {
   String userName=null;
   String password=null;
     public MyAuthenticator()
{
}
     public PasswordAuthentication performCheck(String userName,String password)
   {
    this.userName = userName;
    this.password = password;
    return getPasswordAuthentication();
   }
     protected PasswordAuthentication getPasswordAuthentication()
   {
    return new PasswordAuthentication(userName, password);
   }
  }

    3.在发信的程序中加上

     MyAuthenticator myAuthenticator = new MyAuthenticator();
  PasswordAuthentication my= myAuthenticator.performCheck(userName,password);
   Session mailsession=Session.getInstance(props,myAuthenticator);

 

【责编:landy】

--------------------next---------------------

阅读(144) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~