欢迎来到天天文库
浏览记录
ID:1318874
大小:95.50 KB
页数:10页
时间:2017-11-10
《网络编程技术第九次课javamail(2)代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1)第一个程序:MailClientAuthFromMydomain.java////////////////////////////////带用户验证的读取邮件程序//admin@mydomain.com//////////////////////////////classEmaiAuthenticatorextendsAuthenticator{privateStringm_username;privateStringm_userpass;EmaiAuthenticator(Stringusername,Stringuserpass){super();m
2、_username=username;m_userpass=userpass;}publicPasswordAuthenticationgetPasswordAuthentication(){returnnewPasswordAuthentication(m_username,m_userpass);}}publicclassMailClientAuthFromMydomain{EmaiAuthenticatorma;//验证类EmaiAuthenticator对象protectedSessionsession;//邮件会话protectedStoresto
3、re;privateStringreceiveHost="localhost";//接收邮件服务器主机名privateStringreceiveProtocol="imap";//接收邮件协议privateStringusername="admin";//邮箱用户名privateStringpassword="1234";//邮箱密码publicvoidinit()throwsException{Propertiesprops=newProperties();props.put("mail.store.protocol",receiveProtocol);p
4、rops.put("mail.imap.class","com.sun.mail.imap.IMAPStore");props.put("mail.smtp.auth",true);//在程序中设置需要身份验证的property//设置JavaMail属性ma=newEmaiAuthenticator(username,password);//实例化身份验证类对象session=Session.getDefaultInstance(props,(Authenticator)ma);//在session对象中注册PasswordAuthentication对象
5、,以便验证用户名和密码store=session.getStore(receiveProtocol);//接收邮件服务器上注册用户的存储空间,参数为接收邮件协议imapstore.connect(receiveHost,null,null);//连接邮件服务器时,将用户名和口令都设置为null。10}publicvoidclose()throwsException{store.close();}publicvoidreceiveMessage()throwsException{browseMessagesFromFolder("inbox");}public
6、voidbrowseMessagesFromFolder(StringfolderName)throwsException{Folderfolder=store.getFolder(folderName);if(folder==null)thrownewException(folderName+"邮件夹不存在!");browseMessagesFromFolder(folder);}publicvoidbrowseMessagesFromFolder(Folderfolder)throwsException{folder.open(Folder.READ_O
7、NLY);System.out.println("Youhave"+folder.getMessageCount()+"messagesininbox!");System.out.println("Youhave"+folder.getUnreadMessageCount()+"unreadmessagesininbox!");Message[]messages=folder.getMessages();for(inti=0;i8、-----");System.out.println
8、-----");System.out.println
此文档下载收益归作者所有