有些时候我们只需要验证邮箱的帐号密码是否正确。而不需要发邮件。用下面的代码吧。?
jar包是jmail.jar?
Java代码??
class="star" src="/Upload/Images/2013061911/40B102E0EF997EA6.png">
- package?com.cn.mail.test;??
- ??
- import?java.util.Properties;??
- ??
- import?javax.mail.MessagingException;??
- import?javax.mail.Session;??
- import?javax.mail.URLName;??
- ??
- import?com.sun.mail.smtp.SMTPTransport;??
- ??
- public?class?EmailLogin?extends?SMTPTransport?{??
- ??????
- ??????
- ??
- ??
- ????public?EmailLogin(Session?session,?URLName?urlname)?{??
- ????????super(session,?urlname);??
- ????}??
- ?????
- ?
- ??
- ????public?static?void?main(String[]?args)?{??
- ??????????
- ??????????
- ????????URLName?urlName=new?URLName("smtp","smtp.163.com",25,"","xxx@163.com","password");??
- ????????Session?session=Session.getInstance(new?Properties());??
- ????????EmailLogin?login=new?EmailLogin(session,urlName);??
- ????????System.out.println("登陆:"+login.login());??
- ??????????
- ??????????
- ??????????
- ????}??
- ??
- ??????
- ??????
- ????public?boolean?login(){??
- ??????
- ????????boolean?login=false;??
- ????????try?{??
- ????????????login=??this.protocolConnect(url.getHost(),?url.getPort(),?url.getUsername(),url.getPassword());??
- ????????}?catch?(MessagingException?e)?{??
- ??????????????
- ????????????login=false;??
- ????????}??
- ??
- ????????return?login;??
- ????}??
- ??????
- ??????
- ??
- } ?