📄 checkmail.java
字号:
package email;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FromStringTerm;
import javax.mail.search.OrTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CheckMail implements CheckMailInf{
private UserConfig userConfig = null;
private PerformHelper ph = null ;
public CheckMail(){}
private String SMTP_AUTH_USER="janmyzheung";
private String SMTP_AUTH_PWD="19841001";
/** private Authenticator auth=new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(SMTP_AUTH_USER,SMTP_AUTH_PWD);
}
};**/
private Map map = new HashMap();
//需在Spring BEAN配置文件里填充
public void setMap(Map sprMap){
this.map = sprMap ;
}
public void setUserConfig(UserConfig userConfig){
this.userConfig = userConfig ;
}
public void setPh(PerformHelper ph){
this.ph = ph ;
}
public HttpSession validator(HttpServletRequest request,HttpServletResponse response) throws MessagingException{
HttpSession httpSession = request.getSession();
final String user_count = request.getParameter("user");
final String user_pwd = request.getParameter("password");
String pop_host = request.getParameter("host");
String smtp_host = (String)map.get(pop_host);
String from = pop_host.replace("pop3.",user_count+"@");
Properties props = new Properties();
props.put("mail.smtp.host",smtp_host);
props.put("mail.smtp.auth","true");//认证
PopupAuthenticator popupAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popupAuthenticator.performCheck(user_count,user_pwd);
Session mailSession=Session.getInstance(props,popupAuthenticator);
//将后继需要使用到的资料存入httpSession
UserConfig userConfig =
ph.obtainUserConfigInstance(user_count, user_pwd, pop_host, smtp_host, from);
httpSession.setAttribute("mailSession",mailSession) ;
httpSession.setAttribute("userConfig",userConfig) ;
httpSession.setAttribute("user_count",user_count);
mailSession.setDebug(true);
Store store=mailSession.getStore("pop3");
store.connect(pop_host,user_count,user_pwd);
if(store.isConnected()){
store.close();
return httpSession ;
}else{
return null ;
}
}/****
public static void main(String[] args){
String user = "pop3.163.com" ;
user = user.replace("pop3.","user@");
System.out.println(user);
}****/
public String sendMail(HttpServletRequest req,HttpServletResponse res) {
res.setContentType("text/html");
return "" ;
}
public MyInboxCollection obtainMyInboses(UserConfig userConfig) throws Exception{
//this.userConfig = userConfig ;
//获得一个session对象,并设置其属性为true
Properties props = new Properties();
props.put("mail.smtp.host",userConfig.getSmtp_host());
props.put("mail.smtp.auth","true");//认证
//props.setProperty("mail.pop3.socketFactory.fallback", "false");
PopupAuthenticator popupAuthenticator = new PopupAuthenticator();
PasswordAuthentication pop = popupAuthenticator.performCheck(userConfig.getUser_count(),userConfig.getUser_pwd());
Session mailSession=Session.getInstance(props,popupAuthenticator);
//輸出調試信息
mailSession.setDebug(true);
//创建一个Store对象,并根据得到的三个参数连接到邮件服务器中的用户邮箱
Store store=mailSession.getStore("pop3");
MyInboxCollection mc = null;
store.connect(userConfig.getPop_host(),userConfig.getUser_count(),userConfig.getUser_pwd());
if(store.isConnected()){
Folder inbox=store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg[]=inbox.getMessages();
//System.out.println("邮件数:"+inbox.getMessageCount());
//得到邮箱中的e-mail总数
int count=inbox.getMessageCount();
mc = new MyInboxCollection();
//设置过滤规则,对接收的e-mail进行过滤,
SearchTerm st=new OrTerm(new SubjectTerm("惊喜"),new FromStringTerm("webmaster@hudax.com"));
Message[] filtermsg=inbox.search(st);
mc.setFilterCount(filtermsg.length);
//对被过滤出的e-mail设置删除标志
for(int i=0;i<filtermsg.length;i++)
{
Message ms=filtermsg[i];
if(ms!=null)
{
//得到被过滤出的e-mail的标题
String filterTitle=ms.getSubject();
//设置删除标记
ms.setFlag(Flags.Flag.DELETED,true);
}
}
//過濾後得到的收件箱中的所有郵件
mc.setMailCount(count-mc.getFilterCount());
//将过滤后剩余的e-mail放在发件箱中,列表显示出来
int receindex = 0 ;
for(int i=1;i<=count;i++){
Message m = inbox.getMessage(i);
//System.out.println("mail subject:"+m.getSubject());
//Message m = msg[i] ;
receindex++;
//如果不是待删除的e-mail就显示出来
if(!m.isSet(Flags.Flag.DELETED)){
ph.listForMyInbox(ph.obtainMyInbox(m.getSubject(), i, receindex));
}
//System.out.println("邮件总数:"+ph.obtainMyInboxes().size());
}
mc.setMyInbosex(ph.obtainMyInboxes());
ph.removeMyInboxes();
//System.out.println("MC邮件总数:"+mc.getMyInbosex().size());
//关闭收件箱和到邮件服务器的连接
inbox.close(true);
store.close();
}
return mc ;
}
/*
@从BodyPart中提取使用ISO-8859-1编码的文件名
@因为BodyPart.getFilename()过程已经对文件名作了一次编码,有时不能直接使用
*/
public String getISOFileName(Part body){
//设置一个标志,判断文件名从Content-Disposition中获取还是从Content-Type中获取
boolean flag=true;
if(body==null){
return null;
}
String[] cdis;
try{
cdis=body.getHeader("Content-Disposition");
}
catch(Exception e){
return null;
}
if(cdis==null){
flag=false;
}
if(!flag){
try{
cdis=body.getHeader("Content-Type");
}
catch(Exception e){
return null;
}
}
if(cdis==null){
return null;
}
if(cdis[0]==null){
return null;
}
//从Content-Disposition中获取文件名
if(flag){
int pos=cdis[0].indexOf("filename=");
if(pos<0){
return null;
}
//如果文件名带引号
if(cdis[0].charAt(cdis[0].length()-1)=='"'){
return cdis[0].substring(pos+10,cdis[0].length()-1);
}
return cdis[0].substring(pos+9,cdis[0].length());
}
else{
int pos=cdis[0].indexOf("name=");
if(pos<0){
return null;
}
//如果文件名带引号
if(cdis[0].charAt(cdis[0].length()-1)=='"'){
return cdis[0].substring(pos+6,cdis[0].length()-1);
}
return cdis[0].substring(pos+5,cdis[0].length());
}
}
}
class PopupAuthenticator extends Authenticator{
String username=null;
String password=null;
public PopupAuthenticator(){
}
public PasswordAuthentication performCheck(String user,String pass){
this.username = user;
this.password = pass;
return getPasswordAuthentication();
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -