在以下代码(IBM Domino 9.0.1 java代理)中,我尝试通过端口143连接没有SSL连接到IMAP存储。
package com.fm;
import javax.mail.internet.*;
import javax.mail.*;
import java.net.ConnectException;
import java.util.*;
public class JSaveImap {
private Store store=null;
private String host;
private String port;
private String user;
private String password;
private javax.mail.Session mailSession=null;
private boolean doDebug;
public JSaveImap() {
System.out.println("Creato oggetto");
}
public void saveMessage(MimeMessage message, String folderName) throws MessagingException
{
//http://stackoverflow.com/questions/11524716/save-draft-message-using-java-mail-api
Folder folder=null;
if (folderName.length()>0) {
folder= store.getFolder(folderName);
} else {
folder= store.getFolder("inbox");
}
if (doDebug) {System.out.println("Folder aperto:" + folder.getFullName());}
folder.open(Folder.READ_WRITE);
//draftMessage.setFlag(Flag.DRAFT, true);
MimeMessage arrMessage[] = {message};
folder.appendMessages(arrMessage);
folder.close(false);
folder=null;
}
public boolean connect(String pHost, String pPort, String pUser, String pPassword,boolean doDebug) {
boolean retVal=false;
this.doDebug=doDebug;
if (doDebug) {System.out.println(this.getClass().getName()+ ".connect");}
if (!(pHost.equalsIgnoreCase(host) && pPort.equalsIgnoreCase(port) && pUser.equalsIgnoreCase(user) && pPassword.equalsIgnoreCase(password))) {
// parametri differenti, resetta connessione precedente
recycle();
host=pHost;
port=pPort;
user=pUser;
password=pPassword;
}
System.out.println("Host: " + host + ", port: " + port + ", user: " + user + ", password: " + password);
try {
System.out.println(this.getClass().getName() + " connettiamo?");
//Properties props = System.getProperties();
Properties props =new Properties(); //Nuove proprietà per non avere conflitti con quelle gia in uso ad altri account IMAP
props.setProperty("mail.store.protocol", "imap");
if (port.length()>0) {
props.setProperty("mail.imap.port", port);
}
props.setProperty("mail.imap.ssl.enable", "false"); //credo che non serva, oltretutto default =false, poi dovrebbe venire parametrico dall'account
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.imap.timeout", "5000");
mailSession = Session.getDefaultInstance(props, null);
if (doDebug) {
System.out.println("Proprietà correnti ************************************************************");
Enumeration keys = props.keys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
String value = (String)props.get(key);
System.out.println(key + ": " + value);
}
}
if ( doDebug) { mailSession.setDebug(true); }
store = mailSession.getStore("imap");
//store=mailSession.getStore(new URLName("imap",host,Integer.parseInt(port),null,user,password));
//store.connect(host, user, password);
store.connect(host, Integer.parseInt(port), user, password);
System.out.println("connesso allo store "+ host + " for " +user);
retVal=true;
} catch(NoSuchProviderException ne) {
System.out.println("Exception in class " + ne.getStackTrace()[0].getClassName() +
" method: "+ ne.getStackTrace()[0].getMethodName()+
", row: " + ne.getStackTrace()[0].getLineNumber() + ": " + ne.toString());
//Questa eccezione non la gestisco perchè è nel caso in cui il provider non supporti
// il protocollo. Lo escludo per ora
ne.printStackTrace();
//throw new MessagingException("Errore nella connessione al provider");
} catch (MessagingException ne) {
//throw new MessagingException ("MessagingException durante connessione:"+e.getMessage(),e);
System.out.println("Exception in class " + ne.getStackTrace()[0].getClassName() +
" method: "+ ne.getStackTrace()[0].getMethodName()+
", row: " + ne.getStackTrace()[0].getLineNumber() + ": " + ne.toString());
ne.printStackTrace();
} catch (Exception ne) {
System.out.println("Exception in class " + ne.getStackTrace()[0].getClassName() +
" method: "+ ne.getStackTrace()[0].getMethodName()+
", row: " + ne.getStackTrace()[0].getLineNumber() + ": " + ne.toString());
ne.printStackTrace();
}
if (doDebug) System.out.println("Connect terminata");
return retVal;
}
public void recycle() {
try {
host="";
port="";
user="";
password="";
//if (folder.isOpen()) { folder.close(true);}
if (store!=null) {
if (store.isConnected()) {store.close();}
}
} catch (Exception ne) {
System.out.println("Exception in class " + ne.getStackTrace()[0].getClassName() +
" method: "+ ne.getStackTrace()[0].getMethodName()+
", row: " + ne.getStackTrace()[0].getLineNumber() + ": " + ne.toString());
}
}
}
为什么我在消息
中获得端口993的异常无法连接到主机,端口:172.17.0.247,993;
输出:
Proprietà correnti ************************************************************
mail.imap.port: 143
mail.imap.ssl.enable: false
mail.imap.timeout: 5000
mail.imap.connectiontimeout: 5000
mail.store.protocol: imap
DEBUG: setDebug: JavaMail version 1.5.4
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: closeFoldersOnStoreFailure
**DEBUG IMAP: trying to connect to host "172.17.0.247", port 143, **
isSSL false
Exception in class com.sun.mail.imap.IMAPStore method: protocolConnect, row: 731: com.sun.mail.util.MailConnectException: Couldn't connect to host,
**port: 172.17.0.247, 993; timeout 5000;**
nested exception is: java.net.ConnectException: Connection refused: connect
Agent Manager: Agent error: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: 172.17.0.247, 993; timeout 5000; nested exception is: java.net.ConnectException: Connection refused: connect
Agent Manager: Agent error: at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:731)
Agent Manager: Agent error: at javax.mail.Service.connect(Service.java:364)
Agent Manager: Agent error: at com.fm.JSaveImap.connect(JSaveImap.java:86)