Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions ImapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ public ImapClient(string host, string username, string password, AuthMethods met
AuthMethod = method;
Login(username, password);
}

public ImapClient(string host, string username, string authUsername, string authPassword, AuthMethods method = AuthMethods.Plain, int port = 143, bool secure = false, bool skipSslValidation = false)
{
Connect(host, port, secure, skipSslValidation);
AuthMethod = method;
Login(username, authUsername, authPassword);
}
public enum AuthMethods {
Login,
CRAMMD5,
SaslOAuth
SaslOAuth,
Plain
}

public virtual AuthMethods AuthMethod { get; set; }
Expand Down Expand Up @@ -500,7 +506,7 @@ public virtual Mailbox[] ListSuscribesMailboxes(string reference, string pattern
return x.ToArray();
}

internal override void OnLogin(string login, string password) {
internal override void OnLogin(string login, string password, string authUsername = "") {
string command = String.Empty;
string result = String.Empty;
string tag = GetTag();
Expand All @@ -527,6 +533,19 @@ internal override void OnLogin(string login, string password) {
result = SendCommandGetResponse(command);
break;

case AuthMethods.Plain:
command = tag + "AUTHENTICATE PLAIN";
result = SendCommandGetResponse(command);
// Null character
char nullChar = '\0';
// Set string for authentication
string authenticationString = String.Format("{0}{3}{1}{3}{2}", login, authUsername, password, nullChar);
// String must be converted to base64
byte[] toEncodeAsBytes = ASCIIEncoding.ASCII.GetBytes(authenticationString);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
result = SendCommandGetResponse(returnValue);
break;

case AuthMethods.SaslOAuth:
string sasl = "user=" + login + "\x01" + "auth=Bearer " + password + "\x01" + "\x01";
string base64 = Convert.ToBase64String(Encoding.GetBytes(sasl));
Expand Down
2 changes: 1 addition & 1 deletion Pop3Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public Pop3Client(string host, string username, string password, int port = 110,
Login(username, password);
}

internal override void OnLogin(string username, string password) {
internal override void OnLogin(string username, string password, string authUsername = "") {
SendCommandCheckOK("USER " + username);
SendCommandCheckOK("PASS " + password);
}
Expand Down
13 changes: 12 additions & 1 deletion TextClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public TextClient() {
Encoding = System.Text.Encoding.GetEncoding(1252);
}

internal abstract void OnLogin(string username, string password);
internal abstract void OnLogin(string username, string password, string authPassword = "");
internal abstract void OnLogout();
internal abstract void CheckResultOK(string result);

Expand All @@ -45,6 +45,17 @@ public virtual void Login(string username, string password) {
IsAuthenticated = true;
}

public virtual void Login(string username, string authUsername, string authPassword)
{
if (!IsConnected)
{
throw new Exception("You must connect first!");
}
IsAuthenticated = false;
OnLogin(username, authPassword, authUsername);
IsAuthenticated = true;
}

public virtual void Logout() {
IsAuthenticated = false;
OnLogout();
Expand Down