mirror of
https://github.com/moparisthebest/davmail
synced 2025-01-07 11:48:02 -05:00
Fixed SMTP AUTH LOGIN for Outlook compatibility (thanks to Jose Antonio)
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@133 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
6084afffb5
commit
b46d329a2c
@ -4,6 +4,7 @@ import davmail.AbstractConnection;
|
|||||||
import davmail.tray.DavGatewayTray;
|
import davmail.tray.DavGatewayTray;
|
||||||
import davmail.exchange.ExchangeSession;
|
import davmail.exchange.ExchangeSession;
|
||||||
import sun.misc.BASE64Decoder;
|
import sun.misc.BASE64Decoder;
|
||||||
|
import sun.misc.BASE64Encoder;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@ -19,6 +20,8 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
protected static final int STARTMAIL = 2;
|
protected static final int STARTMAIL = 2;
|
||||||
protected static final int RECIPIENT = 3;
|
protected static final int RECIPIENT = 3;
|
||||||
protected static final int MAILDATA = 4;
|
protected static final int MAILDATA = 4;
|
||||||
|
protected static final int LOGIN = 5;
|
||||||
|
protected static final int PASSWORD = 6;
|
||||||
|
|
||||||
// Initialize the streams and start the thread
|
// Initialize the streams and start the thread
|
||||||
public SmtpConnection(Socket clientSocket) {
|
public SmtpConnection(Socket clientSocket) {
|
||||||
@ -43,7 +46,16 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
if (tokens.hasMoreTokens()) {
|
if (tokens.hasMoreTokens()) {
|
||||||
String command = tokens.nextToken();
|
String command = tokens.nextToken();
|
||||||
|
|
||||||
if ("QUIT".equalsIgnoreCase(command)) {
|
if (state == LOGIN) {
|
||||||
|
// AUTH LOGIN, read userName
|
||||||
|
userName = base64Decode(command);
|
||||||
|
sendClient("334 " + base64Encode("Password:"));
|
||||||
|
state = PASSWORD;
|
||||||
|
} else if (state == PASSWORD) {
|
||||||
|
// AUTH LOGIN, read password
|
||||||
|
password = base64Decode(command);
|
||||||
|
authenticate();
|
||||||
|
} else if ("QUIT".equalsIgnoreCase(command)) {
|
||||||
sendClient("221 Closing connection");
|
sendClient("221 Closing connection");
|
||||||
break;
|
break;
|
||||||
} else if ("EHLO".equals(command)) {
|
} else if ("EHLO".equals(command)) {
|
||||||
@ -59,21 +71,10 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
String authType = tokens.nextToken();
|
String authType = tokens.nextToken();
|
||||||
if ("PLAIN".equals(authType) && tokens.hasMoreElements()) {
|
if ("PLAIN".equals(authType) && tokens.hasMoreElements()) {
|
||||||
decodeCredentials(tokens.nextToken());
|
decodeCredentials(tokens.nextToken());
|
||||||
session = new ExchangeSession();
|
authenticate();
|
||||||
try {
|
} else if ("LOGIN".equals(authType)) {
|
||||||
session.login(userName, password);
|
sendClient("334 " + base64Encode("Username:"));
|
||||||
sendClient("235 OK Authenticated");
|
state = LOGIN;
|
||||||
state = AUTHENTICATED;
|
|
||||||
} catch (Exception e) {
|
|
||||||
String message = e.getMessage();
|
|
||||||
if (message == null) {
|
|
||||||
message = e.toString();
|
|
||||||
}
|
|
||||||
DavGatewayTray.error(message);
|
|
||||||
message = message.replaceAll("\\n", " ");
|
|
||||||
sendClient("554 Authenticated failed " + message);
|
|
||||||
state = INITIAL;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
sendClient("451 Error : unknown authentication type");
|
sendClient("451 Error : unknown authentication type");
|
||||||
}
|
}
|
||||||
@ -136,6 +137,39 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
DavGatewayTray.resetIcon();
|
DavGatewayTray.resetIcon();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create authenticated session with Exchange server
|
||||||
|
* @throws IOException on error
|
||||||
|
*/
|
||||||
|
protected void authenticate() throws IOException {
|
||||||
|
session = new ExchangeSession();
|
||||||
|
try {
|
||||||
|
session.login(userName, password);
|
||||||
|
sendClient("235 OK Authenticated");
|
||||||
|
state = AUTHENTICATED;
|
||||||
|
} catch (Exception e) {
|
||||||
|
String message = e.getMessage();
|
||||||
|
if (message == null) {
|
||||||
|
message = e.toString();
|
||||||
|
}
|
||||||
|
DavGatewayTray.error(message);
|
||||||
|
message = message.replaceAll("\\n", " ");
|
||||||
|
sendClient("554 Authenticated failed " + message);
|
||||||
|
state = INITIAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String base64Encode(String value) {
|
||||||
|
BASE64Encoder encoder = new BASE64Encoder();
|
||||||
|
return encoder.encode(value.getBytes());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String base64Decode(String value) throws IOException {
|
||||||
|
BASE64Decoder decoder = new BASE64Decoder();
|
||||||
|
return new String(decoder.decodeBuffer(value));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decode SMTP credentials
|
* Decode SMTP credentials
|
||||||
*
|
*
|
||||||
@ -143,8 +177,7 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
* @throws java.io.IOException if invalid credentials
|
* @throws java.io.IOException if invalid credentials
|
||||||
*/
|
*/
|
||||||
protected void decodeCredentials(String encodedCredentials) throws IOException {
|
protected void decodeCredentials(String encodedCredentials) throws IOException {
|
||||||
BASE64Decoder decoder = new BASE64Decoder();
|
String decodedCredentials = base64Decode(encodedCredentials);
|
||||||
String decodedCredentials = new String(decoder.decodeBuffer(encodedCredentials));
|
|
||||||
int index = decodedCredentials.indexOf((char) 0, 1);
|
int index = decodedCredentials.indexOf((char) 0, 1);
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
userName = decodedCredentials.substring(1, index);
|
userName = decodedCredentials.substring(1, index);
|
||||||
|
Loading…
Reference in New Issue
Block a user